Joining Arrays
-----------------------------
Joining 1-D arrays
X
Y
concatenate((X,Y), axis = 0)
yields:
[X][Y] as a 1-D array row vector
using axis = 1 generates an error because the .concatenate() method can not produce a new dimension, and axis = 1 requires a 2nd dimension to already exist.
IndexError: axis 1 out of bounds [0, 1)
Also note that Python 1-D arays are defaulted as row vectors when first explicitly constructed. You can make it a column vector by doing:
col_array = np.array([[1], [1]])
print(col_array)
[[1]
[1]]
although that may look a bit ugly with the extra braces, and a check on the .ndim member property of the array class will say 2 dimensions.
The two 1-D arrays being concatenated do NOT need to have the same lengths.
Example:
import numpy as np
# ROW vectors
X = np.array([1, 2, 3])
Y = np.array([4, 5, 6, 7])
XYarray = np.concatenate((X, Y), axis=0)
print(XYarray)
[1 2 3 4 5 6 7]
import numpy as np
# COLUMN vectors
X = np.array([[1], [2], [3]])
Y = np.array([[4], [5], [6], [7]])
XYarray = np.concatenate((X, Y), axis=0)
print(XYarray)
[[1]
[2]
[3]
[4]
[5]
[6]
[7]]
-------------------------------------------------
Joining 2-D arrays
X = [[A], [B]]
Y = [[C], [D]]
concatenate((X,Y), axis = 1)
yields:
[A][C]
[B][D] as a 2-D array
define a submatrix to be any quantity passed in as an argument to .concatenate()
X and Y are each a submatrix.
number of rows = #rows in any one submatrix
number of columns = #submatrices * #columns in a submatrix
important: [A] [C] [B] [D] must all have the same dimensions.
W = [[E], [F]]
Z = [[G], [H]]
-------------------------------------------------
concatenate((W,Z), axis = 0)
yields:
[E]
[F]
[G]
[H] as a 2-D array
define a submatrix to be any quantity passed in as arguments to .concatenate()
W and Z are each a submatrix.
number of rows = #submatrices * #rows in a submatrix
number of columns = #columns in any one submatrix
important: [E] [F] [G] [H] must all have the same dimensions.
################################################################################
Examples:
import numpy as np
X = np.array([[1, 2], [3, 4]])
X = [1 2]
[3 4]
# A = [1, 2] and B = [3, 4] so X = [A, B]
Y = np.array([[5, 6], [7, 8]])
Y= [5 6]
[7 8]
# C = [5, 6] D = [7, 8] so Y = [C, D]
axis1_array = np.concatenate((X, Y), axis=1)
print(axis1_array)
[[1 2 5 6]
[3 4 7 8]]
which looks like:
[A][C]
[B][D] as a 2-D array
Using terms from linear algebra, one can think of an axis=1 concantenate operation on 2-D arrays as taking the transpose of each each subsection of X (A, B) and each subsection of Y (C, D) and joining them side-by-side horizontally.
----------------------------------------------
----------------------------------------------
import numpy as np
W = np.array([[1, 2, 3], [4, 5, 6]])
# E = [1, 2, 3] and F = [4, 5, 6] so W = [E, F]
Z = np.array([[7, 8, 9], [10, 11, 12]])
# G = [7, 8, 9] H = [10, 11, 12] so Z = [G, H]
axis0_array = np.concatenate((W,Z), axis=0)
print(axis0_array)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
which looks like:
[E]
[F]
[G]
[H] as a 2-D array
Using terms from linear algebra, one can think of an axis=0 concatenate operation on 2-D arrays as taking the transpose of each subsection of W (E, F) and each subsection of Z (G, H) and joining them top-to-bottom vertically.
Stacking 1-D arrays
the .stack() method, differs from the .concatenate() method is that it creates a new dimension.
axis = 0 Example:
import numpy as np
X = np.array([1, 2, 3, 4])
Y = np.array([5, 6, 7, 8])
twoDarray_axis0 = np.stack(X, Y)
print(twoDarray_axis0)
The individual row arrays X and Y are connected (left-to-right) into [top-to-bottom] rows like "flat stacked" plates in the cupboard.
In linear algebra terms, if each row is 1 by n, then m rows will yield a m by n matrix.
axis = 1 Example:
import numpy as np
A = np.array([1, 2, 3, 4])
B = np.array([5, 6, 7, 8])
twoarray_axis1 = np.stack((A, B), axis=1)
print(twoarray_axis1)
The individual arrays A and B are connected (left-to-right) into side-by-side [left-to-right] like "side rack stacked" plates in the cupboard.
In linear algebra terms, if each transposed row is now a n by 1 column array, then m rows will yield a n by m matrix.
------------------------
.hstack() stitches together horizontally with array variables glued together reading from left to right in the argument list going left-to-right.
.hstack() increases COLUMNS
arrays passed into .hstack() must have the same dimensions.
INSIGHT:
.hstack() on 1-D arrays does the same thing as .concatenate() with axis=0
.hstack() on 2-D arrays does the same thing as .concatenate() with axis=1
Example:
import numpy as np
C = np.array([[1, 2, 3], [1, 2, 3]] ) # 2 rows and 3 columns
D = np.array([[4, 5, 6], [4, 5, 6]]) # 2 rows and 3 columns
CDarray = np.hstack((C, D))
print(CDarray)
[[1 2 3 4 5 6] # 2 rows and 6 columns
[1 2 3 4 5 6]]
--------------------------------------
.vstack() stitches together vertically with array variables glued together reading from left to right in the argument list going top-to-bottom.
.vstack() increases ROWS
INSIGHT: .vstack() of 1-D row arrays is the same as .stack() of 1-D row arrays with axis=0
arrays passed into .vstack() must have the same dimensions
Example:
import numpy as np
K = np.array([[1, 2, 3], [7, 8 ,9]]) # 2 rows and 3 columns
L = np.array([[4, 5, 6], [7, 8 ,9]]) # 2 rows and 3 columns
KLarray = np.vstack((K, L))
print(KLarray) # 4 rows and 3 columns
[[1 2 3]
[7 8 9]
[4 5 6]
[7 8 9]]
-----------------------------------
.dstack() takes 1-D row arrays, transposes them into 1-D column arrays, and then stitches them side-by-side left-to-right as they appear in the argument list passed into .dstack()
import numpy as np
R = np.array([1, 2, 3])
S = np.array([4, 5, 6])
RSarray = np.dstack((R, S))
print(RSarray)
[[[1 4]
[2 5]
[3 6]]]
notice the 3rd set of square braces; an extra dimension has been added of length 1 (a 3-D array)
------------------------------------
.dstack() takes 2-D arrays, flattens them into 1-D row arrays, transposes them into 1-D column arrays, and then stitches them side-by-side left-to-right as they appear in the argument list passed into .dstack()
Example:
import numpy as np
P = np.array([[1, 2, 3], [1, 2, 3]])
Q = np.array([[4, 5, 6], [11, 22, 33]])
PQarray = np.dstack((P, Q))
print(PQarray)
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 1 11]
[ 2 22]
[ 3 33]]]
notice the 3rd set of square braces; an extra dimension has been added of length 1 (a 3-D array)
No comments:
Post a Comment