0% found this document useful (0 votes)
5 views

Array Join Numpy

Uploaded by

deveshsingh20666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Array Join Numpy

Uploaded by

deveshsingh20666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

9/27/24, 2:23 PM 4 Numpy Array Join Split

Joining NumPy Arrays Joining means putting contents of two or more arrays in a single array. In SQL we join tables based on a key, whereas in
NumPy we join arrays by axes. We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not
explicitly passed, it is taken as 0.

In [1]:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)

[1 2 3 4 5 6]

In [10]:
#Join two 2-D arrays along rows (axis=1):
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

[[1 2 5 6]
[3 4 7 8]]

In [ ]:
Joining Arrays Using Stack Functions
Stacking is same as concatenation, the only difference is that stacking is done along a new axis.
We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.
We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it i

In [3]:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

[[1 4]
[2 5]
[3 6]]
Stacking Along Columns

localhost:8888/lab/tree/anaconda3/Data Science/Data Science/4 Numpy Array Join Split.ipynb 1/5


9/27/24, 2:23 PM 4 Numpy Array Join Split

In [11]:
#NumPy provides a helper function: vstack() to stack along columns.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)

[[1 2 3]
[4 5 6]]
Stacking Along Height (depth)

In [7]:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)

[[[1 4]
[2 5]
[3 6]]]
NumPy Splitting Array

1. Splitting is reverse operation of Joining.


2. Joining merges multiple arrays into one and Splitting breaks one array into multiple.
3. We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

In [1]:
#Split the array in 3 parts:

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)

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

In [1]:
#If the array has less elements than required, it will adjust from the end accordingly.
#Split the array in 4 parts:
import numpy as np

localhost:8888/lab/tree/anaconda3/Data Science/Data Science/4 Numpy Array Join Split.ipynb 2/5


9/27/24, 2:23 PM 4 Numpy Array Join Split
arr = np.array([1, 2, 3, 4, 5, 6, 7])
newarr = np.array_split(arr, 4)
print(newarr)

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


Note: We also have the method split() available but it will not adjust the elements when elements are less in source array for splitting like in example
above, array_split() worked properly but split() would fail.

In [3]:
'''Split Into Arrays
The return value of the array_split() method is an array containing each of the split as an array.
If you split an array into 3 arrays, you can access them from the result just like any array element:
Example
Access the splitted arrays:'''

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])

[1 2]
[3 4]
[5 6]

In [4]:
'''Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.
Use the array_split() method, pass in the array you want to split and the number of splits you want to do.

Example
Split the 2-D array into three 2-D arrays.'''

import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
print(arr)
newarr = np.array_split(arr, 3)
print(newarr)

[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]

localhost:8888/lab/tree/anaconda3/Data Science/Data Science/4 Numpy Array Join Split.ipynb 3/5


9/27/24, 2:23 PM 4 Numpy Array Join Split
[ 9 10]
[11 12]]
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]

In [5]:
#Split the 2-D array into three 2-D arrays.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
print(arr)
newarr = np.array_split(arr, 3)
print(newarr)

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]
[16 17 18]]
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]

1. The example above returns three 2-D arrays.


2. In addition, you can specify which axis you want to do the split around.
3. The example below also returns three 2-D arrays, but they are split along the row (axis=1).

In [6]:
#Split the 2-D array into three 2-D arrays along rows.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)

[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
localhost:8888/lab/tree/anaconda3/Data Science/Data Science/4 Numpy Array Join Split.ipynb 4/5
9/27/24, 2:23 PM 4 Numpy Array Join Split
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
An alternate solution is using hsplit() opposite of hstack()

In [7]:
#Use the hsplit() method to split the 2-D array into three 2-D arrays along rows.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)

[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]

In [ ]:

In [ ]:

localhost:8888/lab/tree/anaconda3/Data Science/Data Science/4 Numpy Array Join Split.ipynb 5/5

You might also like