|
| 1 | +--- |
| 2 | +Title: '.split()' |
| 3 | +Description: 'Divides an array into separate sub-arrays along a specified axis.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Data Structures' |
| 10 | + - 'Functions' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.split()`** function divides an array into multiple sub-arrays along a specified axis. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +numpy.split(array, indices_or_sections, axis=0) |
| 23 | +``` |
| 24 | + |
| 25 | +- `array`: The input array to split. |
| 26 | +- `indices_or_sections`: |
| 27 | + - If an integer, it divides the array into equal-sized sub-arrays. |
| 28 | + - If a list of indices, it splits at the specified positions. |
| 29 | +- `axis` (Optional): The axis along which the array is split (default is `0`, i.e., row-wise). |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +The following example shows splitting arrays in different configurations using the `.split()` function: |
| 34 | + |
| 35 | +```py |
| 36 | +import numpy as np |
| 37 | + |
| 38 | +# Split a 1D array into 3 equal parts |
| 39 | +arr = np.array([11, 22, 33, 44, 55, 66]) |
| 40 | +print(np.split(arr, 3)) |
| 41 | + |
| 42 | +# Split a 2D array into 2 parts along rows (axis=0) |
| 43 | +nd = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) |
| 44 | +print(np.split(nd, 2, axis=0)) |
| 45 | + |
| 46 | +# Split a 2D array at column indices [1, 2] |
| 47 | +print(np.split(nd, [1, 2], axis=1)) |
| 48 | +``` |
| 49 | + |
| 50 | +This produces the following output: |
| 51 | + |
| 52 | +```shell |
| 53 | +[array([11, 22]), array([33, 44]), array([55, 66])] |
| 54 | +[array([[1, 2, 3], |
| 55 | + [4, 5, 6]]), array([[ 7, 8, 9], |
| 56 | + [10, 11, 12]])] |
| 57 | +[array([[ 1], |
| 58 | + [ 4], |
| 59 | + [ 7], |
| 60 | + [10]]), array([[ 2], |
| 61 | + [ 5], |
| 62 | + [ 8], |
| 63 | + [11]]), array([[ 3], |
| 64 | + [ 6], |
| 65 | + [ 9], |
| 66 | + [12]])] |
| 67 | +``` |
| 68 | + |
| 69 | +## Codebyte Example |
| 70 | + |
| 71 | +Run the following example to understand how to split the arrays using the `.split()` function: |
| 72 | + |
| 73 | +```codebyte/python |
| 74 | +import numpy as np |
| 75 | +
|
| 76 | +# Creating a 1D array |
| 77 | +arr = np.array([11, 22, 33, 44, 55, 66]) |
| 78 | +
|
| 79 | +# Splitting into 3 equal parts |
| 80 | +result = np.split(arr, 3) |
| 81 | +
|
| 82 | +# Printing the results |
| 83 | +print("Split into 3 equal parts:") |
| 84 | +print(result) |
| 85 | +
|
| 86 | +# Creating a 2D array |
| 87 | +nd = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) |
| 88 | +
|
| 89 | +# Splitting along rows (axis=0) |
| 90 | +row_split = np.split(nd, 2, axis=0) |
| 91 | +
|
| 92 | +# Splitting along columns (axis=1) |
| 93 | +col_split = np.split(nd, [1, 2], axis=1) |
| 94 | +
|
| 95 | +print("\nRow split:") |
| 96 | +print(row_split) |
| 97 | +
|
| 98 | +print("\nColumn split:") |
| 99 | +print(col_split) |
| 100 | +``` |
0 commit comments