Created
February 15, 2016 11:39
-
-
Save aravindhebbali/120f0a7f578eada638ec to your computer and use it in GitHub Desktop.
NumPy for Beginners: Combine/Split NumPy Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# create two dimensional arrays | |
np1 = np.arange(0, 9).reshape(3, 3) | |
np1 | |
np2 = np1 * 5 | |
np2 | |
# horizontal stacking | |
np.hstack((np1, np2)) | |
# vertical stacking | |
np.vstack((np1, np2)) | |
# depth stacking | |
np.dstack((np1, np2)) | |
# create two one dimensional arrays | |
np_1 = np.arange(0, 6) | |
np_2 = np_1 * 3 | |
# column stacking | |
np.column_stack((np_1, np_2)) | |
# concatenate functions | |
# vertical concatenate equivalent to vstack() | |
np.concatenate((np1, np2), axis = 0) | |
# horizontal concatenate equivalent to hstack() | |
np.concatenate((np1, np2), axis = 1) | |
# two dimensional array | |
np_split = np.arange(0, 12).reshape(4, 3) | |
np_split | |
# horizontal splitting: array of 3 columns | |
np.hsplit(np_split, 3) | |
# horizontal splitting: split at specific rows | |
np.hsplit(np_split, [1, 3]) | |
# using the split function | |
np.split(np_split, 3, axis = 1) | |
# vertical splitting: array of 4 rows | |
np.vsplit(np_split, 4) | |
# vertical splitting using the split function | |
np.split(np_split, 4, axis = 0) | |
# vertical split into 2 arrays | |
np.vsplit(np_split, 2) | |
# vertical split into 2 arrays using split function | |
np.split(np_split, 2, axis = 0) | |
# depth stacking | |
np_stack = np.arange(0, 12).reshape(3, 2, 2) | |
np_stack | |
# depth splitting | |
np.dsplit(np_stack, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment