1.

What happens when the split() method is used for splitting NumPy arrays?

Answer»

1. np.split() : Equally splits arrays into multiple sub-arrays. It raises Value Error when the split cannot be equal.

  • Syntax:
np.split(array, sections, axis=0)

where,


  • array - array that needs to be split

  • sections -
    • If we give an integer X, X equal sub-arrays are obtained after dividing the array. If the split is not possible, ValueError is raised.
      • For example:



 import numpy as np
a = np.arange(8)
split_arr = np.split(a, 2)
split_arr

Output

[array([0, 1, 2, 3]), array([4, 5, 6, 7])]
  • If we give a 1-D sorted array then the entries would represent where the array would be split along the axis. For instance if we provide [2:3] and axis as 0, then the result would be
    [arr[0:2], arr[2:3], arr[3:]]

    • If the provided index exceeds the array dimension along the given axis, then an empty subarray will be returned.

    • For example:


[array([0., 1., 2.]),
array([3.]),
array([4.]),
array([5.]),
array([], dtype=float64),
array([], dtype=float64),
array([], dtype=float64)]

The output would be:

import numpy as np
a = np.arange(6.0)
split_arr = np.split(a, [3, 4, 5, 6, 7,8])
split_arr
  • axis - Along what axis the array has to be split. By default, the value is 0



Discussion

No Comment Found