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

- Python NumPy split 2d array
- Python NumPy split string
- Python NumPy split columns
- Python NumPy split row
- Python NumPy split function
- Python split numpy array by value
- Python numpy random split
- Python numpy divide element wise
- Python numpy split array into chunks
- Python numpy.ndarray object has no attribute ‘split’
- Python np.log divide by zero

## Python NumPy split

- In this Program, we will discuss
**how to split NumPy array in Python**. - Here we can use the
**split()**function for splitting the input string or integers. In Python, the**split()**function is used to split the array into different shapes and this method always returns a list of substrings after splitting the given string. - This method takes three parameters and the array must be divided into N equal arrays.

**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 specifies the array that we want to split.**indices_or_sections:**This parameter can be an integer and in this case, if the array is not possible to break then it will raise an error and if the value of indices is a one-dimensional integer then the array is splitting as per the given integer.**axis:**By default, its value is**0**and the axis along which to be split.

**Example:**

Let’s take an example and check **how to split the array in Python**.

**Source Code:**

```
import numpy as np
arr1 = np.array([3,8,15,27,15,23,15,26,11,13])
b = np.split(arr1, 5)
print("split array",b)
```

In the above code first, we have created a numpy one-dimensional array and then we want to split the array into **5** different parts by using the **np.split() **method we can solve this problem. Once you will print **‘b’** then the output will display sub-parts of the array.

Here is the implementation of the following given code

## How to split Numpy array in Python

In this example, we will use the **np.split()** method on a NumPy array of strings. By using the **split()** we can easily break the array into four equal parts.

**Source Code:**

```
import numpy as np
arr1 = np.array(['Germany', 'Australia', 'China','Japan'])
new_result= np.split(arr1,4)
print("array split:",new_result)
```

Here is the Screenshot of the following given code

This is how to split the NumPy array in Python.

## Python NumPy split 2d array

- In this section, we will discuss
**how to split numpy two-dimensional array in Python**. - Here we can use the
**split()**method for splitting the 2-dimensional array either row-wise or column-wise. In this example, we have created a simple numpy array and now we want to break the**2-d**array by using**np.split()**.

**Example:**

```
import numpy as np
new_arr = np.array([[34,12,31,75],
[14,17,18,93],
[17,21,43,88]])
b = np.split(new_arr,3)
print(b)
```

You can refer to the below Screenshot

## How to split a 2-dimensional array in Python

By using the random() function we have generated an array** ‘arr1’ **and used the **np.hsplit()** method for splitting the NumPy array.

In Python, this method is used to divide an array into multiple subarrays column-wise along with we have applied the **np.vsplit()** method for splitting the row elements.

**Syntax:**

Here is the Syntax of numpy.hsplit() method

```
numpy.hsplit
(
ary,
indices_or_sections
)
```

Syntax of vsplit() method

```
numpy.vsplit
(
ary,
indices_or_section
)
```

**Source Code:**

```
import numpy as np
arr1 = np.random.randint(2, 10, size=(9,9))
new_result = [np.hsplit(i, 3) for i in np.vsplit(arr1,3)]
print(new_result)
```

In the above code, we have used the combination of the **hsplit()** and **vsplit() **method for splitting the 2- dimensional array.

Here is the Output of the following given code

## Python NumPy split string

- Let us see
**how to split NumPy string by using Python**. - In this example, we will use the
**np.split()**method on a numpy array of strings. To do this task first we will import a numpy library and then create an array by using np.array in which we take the value of the string to it. Now declare a variable**‘final_res’**and assign the**np.split()**method for splitting the array into sub-arrays.

**Example:**

```
import numpy as np
new_array = np.array(['John', 'Micheal', 'William','George','oliva'])
final_res= np.split(new_array,5)
print("array split:",final_res)
```

Here is the execution of the following given code

## Python NumPy split columns

- In this section, we will discuss
**how to split columns in NumPy array by using Python**. - In this example, we are going to use the concept array transpose. In Python, the transpose matrix is moving the elements of the row to the column and the column items to the rows. In simple word, it will reverse the values in an array.

**Syntax:**

Here is the Syntax of array transpose

`ndarray.T`

**Example:**

```
import numpy as np
new_output = np.array([[63,13,15],[18,27,18],[14,94,55]])
col1, col2, col3 = new_output.T
print(col1)
print(col2)
print(col3)
```

In the above code, we have created an array and declared variables named ‘**col1′, ‘col2’, ‘col3’** in which we have assigned the array transpose **arr.T. **Once you will print **‘col1’, ‘col2’, ‘col3’** then the output will display the split elements.

You can refer to the below Screenshot

## How to split column-wise in NumPy array

This is another approach to split the column-wise element in an array by using the **np.hsplit()** method. In Python, this method is used to divide an array into multiple subarrays column-wise.

**Source Code:**

```
import numpy as np
new_arr2 = np.array([[23,21,15],[9,5,18],[80,90,55]])
result = np.hsplit(new_arr2,3)
print("Column-wise splitting:",result)
```

Here is the Screenshot of the following given code

## Python NumPy split row

- In this Program, we will discuss
**how to split row elements by using Python**. - To perform this particular task we are going to use the
**np.vsplit()**method for breaking the row elements in a given array. In Python, this method is used to split an array into different sub-arrays vertically and by default it takes axis=0 but we are not going to mention this parameter in the program.

**Syntax:**

Here is the Syntax of numpy **vsplit()** method

```
numpy.vsplit
(
ary,
indices_or_section
)
```

**Source Code:**

```
import numpy as np
new_array = np.array([[63,13,15],[18,27,18],[14,94,55]])
result = np.vsplit(new_array,3)
print("Row-wise splitting:",result)
```

In the above program, we have just created a simple NumPy array by using the **np.array()** and assigning integer values to it. Now we want to split the elements vertically (row-wise) by using the **np.split()** method.

As you can see in the Screenshot the output has three different numpy array

## Python NumPy split function

- Here we can see
**how to split Python Numpy array by using the split() function**. - We have already used this method in every examples and this method is basically used for splitting or we cabn dividing the array into sub-arrays.

**Syntax:**

Here is the Syntax of the split() function

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

**Example:**

```
import numpy as np
new_val = np.array([17, 45, 32, 15, 18, 93])
result = np.split(new_val, 2)
print(result)
```

Here is the implementation of the following given code

As you can see in the above screenshot the output displays the array into two different parts

## Python split numpy array by value

- In this section, we will discuss how to split the numpy array based on value by using Python.
- In this example, we are going to use the concept of
**np.arange()**function for creating an array. Now use the**np.split()**method for dividing the array depending on a given value.

**Example:**

```
import numpy as np
new_arr = np.arange(12).reshape(4,3)
z=np.array_split(new_arr, np.where(new_arr[:, 0]== 6)[0][0])
print(z)
```

Here is the output of the following given code

## Python numpy random split

- In this Program, we will discuss
**how to split the numpy random elements by using Python**. - To do this task we are goin to use the np.random.shuffle() method and this function will help the user to shuffle the arrays along the first given axis and it will modify the positions of items in a NumPy array and it always return None as it workplace.

**Syntax:**

Here is the Syntax of **np.random.shuffle()** method

`random.shuffle(x)`

**Note:** The x parameter indicates the changeable sequence.

**Source Code:**

```
import numpy
arr = numpy.random.rand(10, 3)
b = numpy.random.shuffle(arr)
m, n = arr[:70,:], arr[70:,:]
print(m, n)
```

In the above code, we used the **np.arange()** function for creating an array, and this function spaced values within the provided limit.

You can refer to the below Screenshot

## Python numpy divide element wise

- Here we can see
**how to divide Numpy array element-wise by using Python**. - In this example, we take two numpy arrays and we want to divide each item of the first numpy array with the second array. To do this task we can use the
**/**operator and this operand is used for the division operator.

**Source Code:**

```
import numpy as np
val1 = np.array([50,55,66])
val2 = np.array([5,5,6])
new_result = val1/val2
print(new_result)
```

In the above code, we use the / operator and store the result inside the **‘new_result’**. Once you will print **‘new_result’** then the output will display the division of val1 and val2.

Here is the execution of the following given code

## Python numpy split array into chunks

- In this section we will discuss
**how to split array into chunks by using Python**. - By using the
**split()**function we can perform this particular task and split the array into three different sub-arrays.

**Example:**

```
import numpy as np
arr1 = np.array([5,6,7,8,9,23])
d = np.array_split(arr1, 3)
print(d)
```

Here is the Screenshot of the following given code

## Python numpy.ndarray object has no attribute ‘split’

- In this section, we will discuss the error display message
**‘numpy.ndarray’**object has no attribute**‘split’**. - First, we have created a numpy array and then use
**np.ndarray.split()**for splitting the array into sub-arrays but in this case, when we run this program the output will display the numpy has no attribute ‘split’.

**Source Code:**

```
import numpy as np
new_arr = np.arange(4)
output = np.ndarray_split(new_arr, 3)
print(output)
```

**Screenshot:**

## Solution

To solve this problem we are going to use the** np.split() **function instead of the **nd.array() **for dividing the NumPy array.

```
import numpy as np
new_arr = np.arange(6)
output = np.split(new_arr, 3)
print(output)
```

## Python np.log divide by zero

- In this Program, we will discuss
**how to divide np.log by zero in Python**. - In this example we have used the
**np.log()**function in which we assign the numpy array ‘arr1’. Now use the np.inf for dividing the given value with zero.

**Example:**

```
import numpy as np
arr1= np.random.randint(8,size=8)
ou_new = np.log(arr1)
ou_new[ou_new==-np.inf]=0
print(ou_new)
```

You can refer to the below Screenshot

You may like the following Python tutorials:

- Python NumPy where with examples
- Python NumPy linspace
- Python NumPy Data types
- Python NumPy 3d array
- Python NumPy concatenate
- Python sort NumPy array
- Python NumPy matrix
- Python NumPy diff

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

- Python NumPy split 2d array
- Python NumPy split string
- Python NumPy split columns
- Python NumPy split row
- Python NumPy split function
- Python split numpy array by value
- Python numpy random split
- Python numpy divide element wise
- Python numpy split array into chunks
- Python numpy.ndarray object has no attribute ‘split’
- Python np.log divide by zero

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.