In this Python tutorial, we will learn** how to divide the elements in NumPy array** Python. Also, we will cover these topics.

- Python numpy divide element-wise
- Python numpy divide by zero
- Python numpy divide array by scaler
- Python numpy divide array by vector
- Python np.divide vs /
- Python numpy array divide each element
- Python numpy true_divide
- Python np.split example
- Python numpy random split
- Python numpy split string
- Python np.log divide by zero

**Table of Contents**show

## Python numpy divide

- In this section, we will discuss how to divide the element-wise in NumPy array Python.
- To perform this particular task we are going to use the
**numpy.divide()**function. In Python, this function is used to calculate the division between two numpy arrays and this method provides several parameters that allow the user to specify the algorithm. - We can also use the
**/**operator for dividing each and every item of an array. For example, suppose you have two numpy arrays that contain integer values and we want to divide each value of the first numpy array with the second array by using**numpy.divide()**function and**/**operator.

**Syntax:**

Let’s have a look at the Syntax and understand the working of **numpy.divide()** function

```
numpy.divide
(
x1,
x2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True
)
```

- It consists of a few parameters
**x1:**This parameter indicates the array with dividend values.**x2:**Input array values for calculating the division.**Out:**By default it takes none value and it indicates the result is stored and if it is not provided a none value then a fresh allocated array is returned.

**Example:**

Let’s take an example and check how to divide two arrays in NumPy Python

**Source Code:**

```
import numpy as np
new_val = np.array([12,24,36])
new_val2 = np.array([2,12,4])
result=np.divide(new_val,new_val2)
print("Division of two arrays:",result)
```

In the above code first, we imported the numpy library and then created two arrays in which the dividend array** ‘new_val’** and the divisor array** ‘new_val2’**. After that, we have declared a variable **‘result’ **and assigned **numpy.divide()** function in it. Once you will print ‘result’ then the output will display the result of dividing elements.

Here is the Screenshot of the following given code

Read: Python NumPy Data types

## Python numpy divide element-wise

- In this program, we will learn how to divide element-wise in NumPy array Python by using the
**/**operator. - In Python, the
**/**operator is used to divide one numpy array by another array and the division operator/pass array and constant as operands and store two numpy arrays within a third variable.

**Syntax:**

Here is the syntax of **/** arithmetic operator for division of array

`arr3=arr1/arr2`

**Example:**

Let’s take an example and understand the working of** /** operator

**Source Code:**

```
import numpy as np
new_arr = np.array([49,25,36])
new_arr2 = np.array([7,5,6])
new_output = new_arr/new_arr2
print(new_output)
```

Here is the implementation of the following given code

As you can see in the Screenshot the output displays the **new_array**.

Read: Python NumPy 2d array

## Python numpy divide by zero

- In this section, we will discuss how to divide the numpy array by zero value in Python.
- By using the
**/**operator we can perform this particular task. In Python, the**/**operator is used for dividing the elements from one array from another. In this program first, we created an array that contains only integer values. After that we declare another array**‘new_arr2’**by using the**np.array()**function and assign**0**value in it. - Now use the
**/**operator and store the result into**‘new_output’**and it will display the**‘inf’**value. But we want to get the zero value from the numpy array. To solve this problem we can use the**np.inf()**function and it will help the user to get the zero values.

**Source Code:**

```
import numpy as np
new_arr = np.array([49,25,36])
new_arr2 = np.array([0,0,0])
new_output = new_arr/new_arr2
new_output[new_output == np.inf] = 0
print("Division by zero:",new_output)
```

Here is the Output of the following given code

Read: Python NumPy 3d array

## Python numpy divide array by scaler

- In this section, we will discuss how to divide a numpy array element with a scaler value.
- In this example, we will take an array named
**‘new_val’**that performs the method of dividend and the scaler value is**2**that indicates the divisor. Now we have to pass array and scaler value as an argument in**numpy.divide()**function.

**Source Code:**

```
import numpy as np
new_val = np.arange(2,6).reshape(2,2)
result=np.divide(new_val, 2)
print(result)
```

Here is the execution of the following given code

As you can see in the Screenshot the output displays the new array.

Read: Python NumPy Split + 11 Examples

## Python numpy divide array by vector

- In this Program, we will discuss how to divide a numpy array by vector in Python.
- To perform this particular task we are going to use
**/**operator for dividing the array by vector. First, we will create an array by using the**np.array()**function and assign integer values. After that declare a variable**‘new_vector’**and assign values in it.

**Example:**

```
import numpy as np
new_val = np.array([[11,11,11],[25,25,25],[19,19,19]])
new_vector = np.array([11,25,19])
new_result = new_val / new_vector[:,None]
print(new_result)
```

Here is the Screenshot of the following given code

Read: Python NumPy Normalize

## Python np.divide vs /

- In this section, we will discuss the difference between the numpy divide and Python divide
**/**operator. - In Python, the
**np.divide()**function is used to divide the elements of the first array by the values of the second array and this function is available in the numpy module package and it takes two arrays as an argument. While in the case of**/**operator it is used for dividing the numbers on its left by the value on its right. - The
**/**operator operands and stores two numpy arrays within a third variable. While in case of**numpy.divide()**function always returns the same size as the input array.

**Example:**

```
import numpy as np
new_val = np.array([55,66,77])
new_val2 = np.array([5,11,7])
result=np.divide(new_val,new_val2)
print("Division of two array by using divide function:",result)
val_ele = np.array([36,24,16])
val_ele2 = np.array([3,2,4])
new_output = val_ele/val_ele2
print("Division of arrays by using / operator:",new_output)
```

Here is the execution of the following given code

Read: Python NumPy max with examples

## Python numpy array divide each element

- In this section, we will learn how to divide each element in NumPy array Python.
- Here we can use the
**/**operator to perform this particular task. While using the**/**operand we can also apply**[: None]**that indicates the element-wise division on NumPy array. - In the following example, we will divide
**array new_arr/ result**. First, we will import a numpy library and then create an array by using the**np.array()**function. After that, we will create a variable**‘result’**that indicates the divisior.

**Source** **Code:**

```
import numpy as np
new_arr = np.array([
[99, 99, 99],
[55, 55, 55],
[30, 30, 30]
])
result = np.array([99, 55, 30])
z=new_arr / result[: , None]
print("Divide each element:",z)
```

You can refer to the below Screenshot

As you can see in the Screenshot the output displays the new array.

Read: Python NumPy Random [30 Examples]

## Python numpy true_divide

- In this Program, we will discuss how to use the numpy
**true_divide()**function in Python. - In Python, if we want to divide two numpy arrays of the same size then we can easily use the numpy
**true_divide()**function and this method will help the user to divide elements of the second array by elements of the first array. - This method is available in the NumPy package module and always returns a true division of the input array element-wise.

**Syntax:**

Let’s have a look at the Syntax and understand the working of the **numpy true_divide() **function

```
numpy.true_divide
(
x1,
x2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True
)
```

- It consists of a few parameters
**x1:**This parameter indicates the input array with dividend values.**x2:**This specifies the divisor array and it must be a same shape.**out:**This is an optional parameter and by default it takes none value and if it is not provided a none value a freshly-allocated array is returned.

**Example:**

Let’s take an example and check how to use the** numpy true_divide()** function in Python

**Source Code:**

```
import numpy as np
new_array1 = np.array([50, 36, 72, 49])
print("First array shape:",new_array1.shape)
new_array2 = np.array([10, 3, 9, 7])
print("Second array shape:", new_array2.shape)
#Divide two arrays
new_result = np.true_divide(new_array1, new_array2)
print(new_result)
print("Resultant array shape:",new_result.shape)
```

In the above program, we have taken two arrays ‘**new_array1**‘ and ‘new_array2’ that consist of integer values. After that, we have passed these two numpy arrays as arguments within the numpy **true_divide**() function. Once you will print ‘**new_result**‘ then the output will display the new array with the same shape.

Here is the implementation of the following given code

Read: Python NumPy shape with examples

## Python np.split example

- In this section, we will learn how to divide the array by using the
**numpy.split()**function. - In Python, if you want to split the array into sub-arrays then you can easily use the
**numpy.split()**function along with axis. This method will help the user to return a list of subarrays as views into an array. - In Python, this function is available in the NumPy module package and the most important point is this function does not return in the equal division.

**Syntax:**

Here is the Syntax of numpy.split() function

```
numpy.split
(
ary,
indices_or_sections,
axis=0
)
```

- It consists of a few parameters
**ary:**This parameter indicates the array which we want to be split.**indices_or_sections:**This specifies that array elements must be an integer and the array will be split into N equal arrays along with specified axis .If the array is not equal then it will raise an error.**axis:**By default its value is 0 and it signifies the axis along which to split.

**Example:**

Let’s take an example and understand the working of numpy.split() function in Python

**Source Code:**

```
import numpy as np
new_array = np.array([12,13,14,15,16,17,18,19,20])
result= np.split(new_array, 3)
print("Spliiting arrays:",result)
```

In the above code first, we imported the numpy library and then use the** np.array() **function for creating a NumPy array. After that, we have declared a variable ‘result’ and assigned the **np.split() **function in which we have passed the array along with split **number = 3** that indicates the array will be divided into 3-equal parts.

Here is the execution of the following given code

Read: Python reverse NumPy array

## Python numpy random split

We have already covered this topic on the Python NumPy split post.

## Python numpy split string

- In this section, we will discuss how to split the string in NumPy array Python.
- In Python to split the string, we can easily use the
**numpy.char.split()**function and this method will return a list of words in the input string by the specified separator.

**Syntax:**

Let’s have a look at the Syntax and understand the working of numpy.char.split() function

```
numpy.char.split
(
a,
sep=None,
maxsplit=None
)
```

- It consists of a few parameters\
**a:**This parameter specifies the input array**sep:**By default it takes none value and the separator can be any whitespace string.**maxsplit:**If this parameter is not given then by default it takes none value.

**Example:**

Let’s take an example and check how to split the string in NumPy Python

**Source Code:**

```
import numpy as np
new_arr="John is a good boy"
new_result = np.char.split(new_arr)
print("Split input string into sub-string:",new_result)
```

In the above code, we have taken an input string named **‘new_arr’**. After that, we have declared a variable** ‘new_result’** and assigned **np.char.split()** function and within this method, we have passed the array as an argument. Once you will print ‘new_arr’ then the output will display the splitting input string.

Here is the Screenshot of the following given code

Read: Python NumPy Indexing – Detailed Guide

## Python np.log divide by zero

- In this Program, we will discuss how to divide the
**np.log()**function by zero in Python.

**Example:**

```
import numpy as np
d= np.log(0)/0
print(d)
```

In Python, the logarithm(log) by zero is not defined because you can never divide log0 by dividing zero.

Here is the Screenshot of the following given code

You may also like to read the following related tutorials.

- Python NumPy Filter + 10 Examples
- Python NumPy Delete – Complete Tutorial
- Python NumPy Minimum tutorial
- Python NumPy Stack with Examples

In this Python tutorial, we have learned** how to divide the elements in NumPy array** Python. Also, we have covered these topics.

- Python numpy divide element-wise
- Python numpy divide by zero
- Python numpy divide array by scaler
- Python numpy divide array by vector
- Python numpy divide column
- Python np.divide vs /
- Python numpy array divide each element
- Python numpy true_divide
- Python np.split example
- Python numpy random split
- Python numpy split string
- Python np.log divide by zero

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.