Monday , May 20 2024
Combine & Split an Array

Combine & Split an Array

10 – Combine & Split an Array
In [2]:
import numpy as np
In [3]:
arr1 = np.array([[1,2,3,4], [1,2,3,4]])
arr2 = np.array([[5,6,7,8], [5,6,7,8]])

np.concatenate((a, b), axis=0)

In [4]:
arr1
Out[4]:
array([[1, 2, 3, 4],
       [1, 2, 3, 4]])
In [5]:
arr2
Out[5]:
array([[5, 6, 7, 8],
       [5, 6, 7, 8]])
In [6]:
# concat along the row
cat = np.concatenate((arr1, arr2), axis=0)        
print(cat)
[[1 2 3 4]
 [1 2 3 4]
 [5 6 7 8]
 [5 6 7 8]]
In [7]:
# concat along the column
cat = np.concatenate((arr1, arr2), axis=1)    
print(cat)
[[1 2 3 4 5 6 7 8]
 [1 2 3 4 5 6 7 8]]

Stacking: np.hstack() and n.vstack()

Stacking is done using the np.hstack() and np.vstack() methods. For horizontal stacking, the number of rows should be the same, while for vertical stacking, the number of columns should be the same.

In [8]:
# stack arrays vertically
cat = np.vstack((arr1, arr2))
print(cat)
[[1 2 3 4]
 [1 2 3 4]
 [5 6 7 8]
 [5 6 7 8]]
In [9]:
# stack arrays vertically
cat = np.r_[arr1, arr2]
print(cat)
[[1 2 3 4]
 [1 2 3 4]
 [5 6 7 8]
 [5 6 7 8]]

np.hstack((a, b))

np.c_[a, b]

In [10]:
# stack arrays horizontally
cat = np.hstack((arr1, arr2))
print(cat)
[[1 2 3 4 5 6 7 8]
 [1 2 3 4 5 6 7 8]]
In [11]:
# stack arrays horizontally
cat = np.c_[arr1, arr2]
print(cat)
[[1 2 3 4 5 6 7 8]
 [1 2 3 4 5 6 7 8]]

split an array

In [12]:
arr = np.random.rand(6,6)
In [13]:
# split the array vertically into n evenly spaced chunks
arr1 = np.vsplit(arr, 2)
print(arr1)
[array([[0.23544729, 0.29376922, 0.61694114, 0.12710509, 0.46889931,
        0.24898821],
       [0.41942543, 0.76146659, 0.87118521, 0.78777727, 0.07654391,
        0.66503539],
       [0.09633103, 0.80046244, 0.46380349, 0.72348891, 0.95805048,
        0.2057745 ]]), array([[0.00776719, 0.91848221, 0.5663478 , 0.3140263 , 0.76468701,
        0.39014069],
       [0.34323757, 0.33723483, 0.62378567, 0.04786313, 0.35984524,
        0.61933045],
       [0.95174483, 0.72648794, 0.16862658, 0.01313353, 0.19875992,
        0.70763246]])]
In [15]:
# split the array horizontally into n evenly spaced chunks
arr2 = np.hsplit(arr, 2)
print(arr2)
[array([[0.23544729, 0.29376922, 0.61694114],
       [0.41942543, 0.76146659, 0.87118521],
       [0.09633103, 0.80046244, 0.46380349],
       [0.00776719, 0.91848221, 0.5663478 ],
       [0.34323757, 0.33723483, 0.62378567],
       [0.95174483, 0.72648794, 0.16862658]]), array([[0.12710509, 0.46889931, 0.24898821],
       [0.78777727, 0.07654391, 0.66503539],
       [0.72348891, 0.95805048, 0.2057745 ],
       [0.3140263 , 0.76468701, 0.39014069],
       [0.04786313, 0.35984524, 0.61933045],
       [0.01313353, 0.19875992, 0.70763246]])]

About Machine Learning

Check Also

Groupby in Pandas - Data Science Tutorials

Groupby in Pandas – Data Science Tutorials

14- Groupby Groupby in Pandas¶Pandas groupby is used for grouping the data according to the …

Leave a Reply

Your email address will not be published. Required fields are marked *