NumPy Stacking & Splitting Arrays 📊

Veröffentlicht am: 16 September 2025
auf dem Kanal: CodeVisium
298
3

1. Vertical stacking with vstack()
vstack() stacks arrays vertically (row-wise), adding arrays on top of each other.

Long form
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked = np.vstack((a, b))

One-liner
stacked = __import__('numpy').vstack((__import__('numpy').array([1,2,3]), __import__('numpy').array([4,5,6])))


Here, two 1D arrays become a 2×3 matrix:

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


2. Horizontal stacking with hstack()
hstack() stacks arrays horizontally (column-wise), joining them side by side.

Long form
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked = np.hstack((a, b))

One-liner
stacked = __import__('numpy').hstack((__import__('numpy').array([1,2,3]), __import__('numpy').array([4,5,6])))


This results in [1, 2, 3, 4, 5, 6], a longer 1D array.

3. Column stacking with column_stack()
column_stack() stacks 1D arrays as columns into a 2D array.

Long form
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked = np.column_stack((a, b))

One-liner
stacked = __import__('numpy').column_stack((__import__('numpy').array([1,2,3]), __import__('numpy').array([4,5,6])))


Here, arrays are stacked as columns, producing:

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


4. Splitting arrays with split()
The split() function divides arrays into multiple sub-arrays of equal size.

Long form
import numpy as np
arr = np.arange(9)
split_arr = np.split(arr, 3)

One-liner
split_arr = __import__('numpy').split(__import__('numpy').arange(9),3)


This splits [0,1,2,3,4,5,6,7,8] into three arrays: [0,1,2], [3,4,5], [6,7,8].

5. Splitting arrays with hsplit() and vsplit()
These functions split arrays horizontally (columns) or vertically (rows).

Long form
import numpy as np
matrix = np.arange(16).reshape(4, 4)
h_split = np.hsplit(matrix, 2)
v_split = np.vsplit(matrix, 2)

One-liner
h_split,v_split = __import__('numpy').hsplit(__import__('numpy').arange(16).reshape(4,4),2), __import__('numpy').vsplit(__import__('numpy').arange(16).reshape(4,4),2)


Here, hsplit divides the 4×4 matrix into two 4×2 arrays (columns), while vsplit divides it into two 2×4 arrays (rows).

5 Interview Questions (with Answers):

Q: What is the difference between vstack() and hstack()?
A: vstack() stacks arrays vertically (row-wise), while hstack() stacks them horizontally (column-wise).

Q: When should you use column_stack()?
A: Use it when you want to combine multiple 1D arrays into columns of a 2D array.

Q: What happens if you try to split an array into unequal parts using split()?
A: NumPy raises a ValueError; for unequal splits, use array_split() instead.

Q: What’s the difference between split() and array_split()?
A: split() requires equal division, while array_split() allows unequal division of arrays.

Q: How are hsplit() and vsplit() different from split()?
A: hsplit() and vsplit() specifically split along horizontal (columns) or vertical (rows) axes, whereas split() works on any axis.

Hashtags:
#numpy #Python #DataScience #MachineLearning #ArrayStacking #ArraySplitting #CodingTips #InterviewPrep


Auf dieser Seite können Sie das Online-Video NumPy Stacking & Splitting Arrays 📊 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer CodeVisium 16 September 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 298 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!