Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



NumPy Array Split

NumPy is a popular Python library used for scientific computing. It provides a powerful N-dimensional array object, which is the core of many scientific computing tasks. One of the most useful features of NumPy is the ability to split arrays into smaller arrays. This is done using the NumPy array split function.

The NumPy array split function allows you to split an array into multiple sub-arrays along a specified axis. This can be useful for a variety of tasks, such as data analysis, machine learning, and image processing.

Let's take a look at how to use the NumPy array split function.

Using the NumPy Array Split Function

The NumPy array split function takes three arguments:

  • The array to split
  • The number of sub-arrays to create
  • The axis along which to split the array

Here's an example:

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Split the array into two sub-arrays along the first axis
sub_arrs = np.array_split(arr, 2, axis=0)

# Print the sub-arrays
print(sub_arrs)

In this example, we create a 2D array with three rows and three columns. We then use the NumPy array split function to split the array into two sub-arrays along the first axis (i.e., the rows). The resulting sub-arrays are printed to the console.

Here's the output:

[array([[1, 2, 3],
        [4, 5, 6]]), 
 array([[7, 8, 9]])]

As you can see, the original array has been split into two sub-arrays, with the first sub-array containing the first two rows of the original array, and the second sub-array containing the third row of the original array.

You can also use the NumPy array split function to split an array into multiple sub-arrays along the second axis (i.e., the columns). Here's an example:

import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Split the array into three sub-arrays along the second axis
sub_arrs = np.array_split(arr, 3, axis=1)

# Print the sub-arrays
print(sub_arrs)

In this example, we create a 2D array with three rows and three columns. We then use the NumPy array split function to split the array into three sub-arrays along the second axis (i.e., the columns). The resulting sub-arrays are printed to the console.

Here's the output:

[array([[1],
        [4],
        [7]]), 
 array([[2],
        [5],
        [8]]), 
 array([[3],
        [6],
        [9]])]

As you can see, the original array has been split into three sub-arrays, with the first sub-array containing the first column of the original array, the second sub-array containing the second column of the original array, and the third sub-array containing the third column of the original array.

Conclusion

The NumPy array split function is a powerful tool for splitting arrays into smaller sub-arrays. It can be used for a variety of tasks, such as data analysis, machine learning, and image processing. By understanding how to use the NumPy array split function, you can take advantage of the full power of the NumPy library.

References

Activity