Defines a multi-dimensional array object and associated fast math
functions that operate on it. It also provides simple routines for linear
algebra and fft and sophisticated random-number generation.
Outline
●
Arrays, Multidimensional Arrays
– Creation, Shape, Indexing
– Linear Array, spaced equally
●
Array Slicing, Array Reshaping – ravel(), flatten()
●
Transpose, Resize
●
Zeros, Ones, Eye
●
Matrix Multiplication
●
Array Stacking – Vertical Stacking, Concatenation, Depth Stack,
Column Stacking, Row Stacking
Array
●
Like lists, for numeric operations
●
Array consists of a homogeneous group of
values that are all of the same type
– Can be one dimensional or multidimensional
●
Array
●
Element-wise addition and multiplication
●
Ensures clean code when dealing with
numerical operations
Element-wise Array Addition – Python
list_two = list(range(1, 4))
list_three = list(range(1, 4))
list_sum = []
list_two = [x**2 for x in list_two]
list_three = [x**3 for x in list_three]
for i in range(len(list_two)):
list_sum.append(list_two[i] + list_three[i])
print(list_sum)
Element-wise Array Addition – NumPy
import numpy as np
array_two = np.arange(1, 4)**2 # array [1 4 9]
array_three = np.arange(1, 4)**3
print(array_two + array_three)
#can do arithmetic operations, higher math functions
np.power(array_two, 4)
np.negative(array_two)
np.exp(array_two)
np.log(array_two)
np.sin(array_two)
https://numpy.org/doc/stable/reference/ufuncs.html
Multidimensional Arrays
●
n-dimensional array is homogeneous
●
Defined by: shape, and item it is composed of
●
Every item has the same size
– Eg. int64 array: [1 2 3]
●
Shape of an Array
●
Tuple of rows and columns
●
1D Array: (n, )
– n columns in 1 row
●
2D Array: (n, m)
– n rows, m columns
●
Shape of [1 2 3] is (3,)
Array Creation
import numpy as np
array1 = np.arange(3) # same as (0, 3)
print(array1)
sequence_array = np.arange(1, 30, 3)
print(sequence_array)
[ 1 4 7 10 13 16 19 22 25 28]
Multi-Dimensional Array
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3)
array3 = np.arange(3)
multi_array = np.array([array1, array2, array3])
print(multi_array)
print(multi_array.shape())
Linear Array, spaced equally
linear_array = np.linspace(1, 10, 50)
print(linear_array)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735
2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816
3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898
4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898
5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061
6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143
7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224
8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306
9.81632653 10. ]
Linear Array, spaced equally
linear_array = np.linspace(1, 10, 50, False)
print(linear_array)
[1. 1.18 1.36 1.54 1.72 1.9 2.08 2.26 2.44 2.62 2.8 2.98 3.16 3.34
3.52 3.7 3.88 4.06 4.24 4.42 4.6 4.78 4.96 5.14 5.32 5.5 5.68 5.86
6.04 6.22 6.4 6.58 6.76 6.94 7.12 7.3 7.48 7.66 7.84 8.02 8.2 8.38
8.56 8.74 8.92 9.1 9.28 9.46 9.64 9.82]
Multi-Dimensional Array – Indexing
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3, 6)
array3 = np.arange(6, 9)
multi_array = np.array([array1, array2, array3])
print(multi_array)
print(multi_array[0, 0])
print(multi_array[2, 0])
print(multi_array[3, 0])
Multi-Dimensional Array – dtype
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3, 6)
array3 = np.arange(6, 9)
multi_array = np.array([array1, array2, array3])
print(multi_array, multi_array.dtype)
multi_array1 = np.array([array1, array2, array3], dtype = np.int16)
print(multi_array1.dtype)
multi_array2 = np.array([array1, array2, array3], dtype = np.uint8)
print(multi_array2.dtype)
Array Slicing
# Array Slicing
# [start stop step]
array2 = np.arange(1, 10)
print(f"{array2=}")
print(f"array2 = {array2}")
print(array2)
print(f"{array2[2:7]=}")
print(f"{array2[2:7:2]=}")
print(f"{array2[2:]=}")
print(f"{array2[:7]=}")
Array Reshaping
Array Reshaping
array1 = np.arange(9)
print(array1, array1.shape)
array2 = array1.reshape(3,3)
print(array2, array2.shape)
array3 = array1.reshape(3, 5)
Array Reshaping
array1 = np.arange(9)
print(array1, array1.shape)
array2 = array1.reshape(3,3)
print(array2, array2.shape)
# no. of columns calculated automatically
array3 = array1.reshape(3, -1)
Array Reshaping
●
1D arrays to nD arrays
Array Reshaping
●
1D arrays to nD arrays
array1 = np.arange(18)
print(array1, array1.shape)
array3 = array1.reshape(2, 3, 3)
print(array3, array3.shape)
array3 = array1.reshape(3, 3, 2)
print(array3, array3.shape)
Array Slicing
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
print(array1[1,1,1])
print(array1[1,])
print(array1[1,0:])
print(array1[1, 0:2, 0:3])
print(array1[1, :2, :3])
print(array1[1, ...])
Array Slicing
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
#step size = 2
print(array1[1, :2, :3:2])
Array Slicing – Conditional Selection
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
comparison_operation = array1 > 5
print(comparison_operation)
print(array1[comparison_operation])
print(array1[array1 > 5])
print(array1.max())
print(array1.min())
Array Reshaping - ravel()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# ravel returns a view of array1 from the original
ravelled_array = array1.ravel()
print(ravelled_array)
Array Reshaping - ravel()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# ravel returns a view of array1 from the original
ravelled_array = array1.ravel()
print(ravelled_array)
ravelled_array[2] = 999888777
print(ravelled_array)
print(array1)
Array Reshaping - flatten()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# flatten returns a copy of array1. uses new memory.
flattened_array = array1.flatten()
print(flattened_array)
Array Reshaping - flatten()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# flatten returns a copy of array1. uses new memory.
flattened_array = array1.flatten()
print(flattened_array)
flattened_array[2] = 777888999
print(flattened_array)
print(array1)
Transpose
array2 = np.arange(9)
print(array2)
array2.shape = [3, 3]
print(array2)
print(array2.transpose())
print(array2.T)
Resize
print(np.resize(array2, (6, 6)))
Zeros
z_array = np.zeros((6,))
print(z_array)
z_array1 = np.zeros((6,), dtype=int)
print(z_array1)
z_array2 = np.zeros((2, 3), dtype=int)
print(z_array2)
Ones
o_array = np.ones((6,))
print(o_array)
o_array1 = np.ones((6,), dtype=int)
print(o_array1)
o_array2 = np.ones((2, 3), dtype=int)
print(o_array2)
Eye
# identity matrix
print(np.eye(6))
print(np.eye(6, dtype=int)
# array populated with random numbers
np.random.rand(4, 4)
Matrix Multiplication
matA = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
print(matA)
matB = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3, 2)
print(matB)
Matrix Multiplication
matA = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
matB = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3, 2)
print(matA * matB)
product = np.matmul(matA, matB)
print(product)
Matrix Multiplication
mat_a = np.array([0, 3, 5, 5, 5, 2]).reshape(2, 3)
mat_b = np.array([3, 4, 3, -2, 4, -2]).reshape(3, 2)
mat_ab = mat_a @ mat_b
print(mat_ab)
mat_ab1 = np.matmul(mat_a, mat_b)
print(mat_ab1)
Array Stacking
Array Stacking
A B
Array Stacking
A B
Horizontal Stacking Vertical Stacking
Array Stacking
# Stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
array1_array2 = np.hstack((array1, array2))
print(array1_array2)
Array Stacking
# Stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(2, -1)
print(array1)
print(array2)
array1_array2 = np.hstack((array1, array2))
print(array1_array2)
print(array1_array2.shape)
Array Stacking – Vertical Stacking
# vertical stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(-1, 2)
print(array1)
print(array2)
a1_a2 = np.vstack((array1, array2))
print(a1_a2)
print(a1_a2.shape)
Array Stacking, Concatenation
A B
Horizontal Stacking Vertical Stacking
Concatenate
Axis = 1
Concatenate
Axis = 0
Array Stacking – Concatenation
# concatenation along an axis
# horizontal - 1
# vertical - 0
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
print(np.concatenate((array1, array2), 0))
print(np.concatenate((array1, array2), 1))
Array Stacking – Concatenation
# concatenation along an axis
# horizontal – 1. # vertical - 0
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(-1, 2)
# array2 = np.arange(4, 12).reshape(2, -1)
print(array1)
print(array2)
print(np.concatenate((array1, array2), axis = 0))
#print(np.concatenate((array1, array2), axis = 1))
np.vstack((array1, array2)) == np.concatenate((array1,
array2), axis = 0)
hstack and vstack
●
Horizontal Stacking (hstack)
– Along the second axis (n, m)
– Increases number of columns
●
Vertical Stacking (vstack)
– Along the first axis (n, m)
– Increases number of rows
Depth Stacking
●
Stack along the z-axis
A B
Depth Stacking
●
Stack along the z-axis
A (2,2) A (2,2,1)
B (2,2)
B (2,2,1)
Depth Stacking
●
Stack along the z-axis
A (2,2) A (2,2,1)
C (2,2,2)
B (2,2)
Depth
Stacking
B (2,2,1)
Array Stacking – Depth Stack
# depth stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
# dstack reshapes a1 (2,2) to a1 (2, 2, 1)
# dstack reshapes a2 (2,2) to a2 (2, 2, 1)
# stacks them side by side (2, 2, 2)
depth_stack = np.dstack((array1, array2))
print(depth_stack)
Array Stacking – Depth Stack
# depth stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2, 1)
array2 = np.arange(4, 8).reshape(2, 2, 1)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
depth_stack = np.dstack((array1, array2))
print(depth_stack)
Column Stacking
A (2,2) B (2,2) AB (2,4)
Identical to
hstack
Column
Stacking
AB (4,2)
A (4) B (4)
Column
Stacking
Row Stacking
A (2,2) B (2,2) AB (4,2)
Identical to
vstack
Row
Stacking
A (4) B (4) AB (2,4)
Row
Stacking
Array Stacking – Column Stacking
# column stacking
# horizontal stacking along columns
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
col_stack = np.column_stack((array1, array2))
print(col_stack)
print(col_stack == np.hstack((array1, array2)))
Array Stacking – Column Stacking
# column stacking
# horizontal stacking along columns
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
col_stack = np.column_stack((array1, array2))
print(col_stack)
print(np.hstack((array1, array2)))
#print(col_stack == np.hstack((array1, array2)))
Array Stacking – Row Stacking
# row stacking
# vertical stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
row_stack = np.row_stack((array1, array2))
print(row_stack)
Array Stacking – Row Stacking
# row stacking
# vertical stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
row_stack = np.row_stack((array1, array2))
print(row_stack)
Summary
●
Arrays, Multidimensional Arrays
– Creation, Shape, Indexing
– Linear Array, spaced equally
●
Array Slicing, Array Reshaping – ravel(), flatten()
●
Transpose, Resize
●
Zeros, Ones, Eye
●
Matrix Multiplication
●
Array Stacking – Vertical Stacking, Concatenation, Depth Stack, Column
Stacking, Row Stacking
Defines a multi-dimensional array object and associated fast math
functions that operate on it. It also provides simple routines for linear
algebra and fft and sophisticated random-number generation.
Outline
●
Arrays, Multidimensional Arrays
– Creation, Shape, Indexing
– Linear Array, spaced equally
●
Array Slicing, Array Reshaping – ravel(), flatten()
●
Transpose, Resize
●
Zeros, Ones, Eye
●
Matrix Multiplication
●
Array Stacking – Vertical Stacking, Concatenation, Depth Stack,
Column Stacking, Row Stacking
Array
●
Like lists, for numeric operations
●
Array consists of a homogeneous group of
values that are all of the same type
– Can be one dimensional or multidimensional
●
Array
●
Element-wise addition and multiplication
●
Ensures clean code when dealing with
numerical operations
Element-wise Array Addition – Python
list_two = list(range(1, 4))
list_three = list(range(1, 4))
list_sum = []
list_two = [x**2 for x in list_two]
list_three = [x**3 for x in list_three]
for i in range(len(list_two)):
list_sum.append(list_two[i] + list_three[i])
print(list_sum)
Element-wise Array Addition – NumPy
import numpy as np
array_two = np.arange(1, 4)**2 # array [1 4 9]
array_three = np.arange(1, 4)**3
print(array_two + array_three)
#can do arithmetic operations, higher math functions
np.power(array_two, 4)
np.negative(array_two)
np.exp(array_two)
np.log(array_two)
np.sin(array_two)
https://numpy.org/doc/stable/reference/ufuncs.html
Multidimensional Arrays
●
n-dimensional array is homogeneous
●
Defined by: shape, and item it is composed of
●
Every item has the same size
– Eg. int64 array: [1 2 3]
●
Shape of an Array
●
Tuple of rows and columns
●
1D Array: (n, )
– n columns in 1 row
●
2D Array: (n, m)
– n rows, m columns
●
Shape of [1 2 3] is (3,)
Array Creation
import numpy as np
array1 = np.arange(3) # same as (0, 3)
print(array1)
sequence_array = np.arange(1, 30, 3)
print(sequence_array)
[ 1 4 7 10 13 16 19 22 25 28]
Multi-Dimensional Array
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3)
array3 = np.arange(3)
multi_array = np.array([array1, array2, array3])
print(multi_array)
print(multi_array.shape())
Linear Array, spaced equally
linear_array = np.linspace(1, 10, 50)
print(linear_array)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735
2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816
3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898
4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898
5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061
6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143
7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224
8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306
9.81632653 10. ]
Linear Array, spaced equally
linear_array = np.linspace(1, 10, 50, False)
print(linear_array)
[1. 1.18 1.36 1.54 1.72 1.9 2.08 2.26 2.44 2.62 2.8 2.98 3.16 3.34
3.52 3.7 3.88 4.06 4.24 4.42 4.6 4.78 4.96 5.14 5.32 5.5 5.68 5.86
6.04 6.22 6.4 6.58 6.76 6.94 7.12 7.3 7.48 7.66 7.84 8.02 8.2 8.38
8.56 8.74 8.92 9.1 9.28 9.46 9.64 9.82]
Multi-Dimensional Array – Indexing
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3, 6)
array3 = np.arange(6, 9)
multi_array = np.array([array1, array2, array3])
print(multi_array)
print(multi_array[0, 0])
print(multi_array[2, 0])
print(multi_array[3, 0])
Multi-Dimensional Array – dtype
import numpy as np
array1 = np.arange(3) # same as (0, 3)
array2 = np.arange(3, 6)
array3 = np.arange(6, 9)
multi_array = np.array([array1, array2, array3])
print(multi_array, multi_array.dtype)
multi_array1 = np.array([array1, array2, array3], dtype = np.int16)
print(multi_array1.dtype)
multi_array2 = np.array([array1, array2, array3], dtype = np.uint8)
print(multi_array2.dtype)
Array Slicing
# Array Slicing
# [start stop step]
array2 = np.arange(1, 10)
print(f"{array2=}")
print(f"array2 = {array2}")
print(array2)
print(f"{array2[2:7]=}")
print(f"{array2[2:7:2]=}")
print(f"{array2[2:]=}")
print(f"{array2[:7]=}")
Array Reshaping
Array Reshaping
array1 = np.arange(9)
print(array1, array1.shape)
array2 = array1.reshape(3,3)
print(array2, array2.shape)
array3 = array1.reshape(3, 5)
Array Reshaping
array1 = np.arange(9)
print(array1, array1.shape)
array2 = array1.reshape(3,3)
print(array2, array2.shape)
# no. of columns calculated automatically
array3 = array1.reshape(3, -1)
Array Reshaping
●
1D arrays to nD arrays
Array Reshaping
●
1D arrays to nD arrays
array1 = np.arange(18)
print(array1, array1.shape)
array3 = array1.reshape(2, 3, 3)
print(array3, array3.shape)
array3 = array1.reshape(3, 3, 2)
print(array3, array3.shape)
Array Slicing
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
print(array1[1,1,1])
print(array1[1,])
print(array1[1,0:])
print(array1[1, 0:2, 0:3])
print(array1[1, :2, :3])
print(array1[1, ...])
Array Slicing
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
#step size = 2
print(array1[1, :2, :3:2])
Array Slicing – Conditional Selection
array1 = np.arange(18).reshape(3, 2, 3)
#print(array1)
comparison_operation = array1 > 5
print(comparison_operation)
print(array1[comparison_operation])
print(array1[array1 > 5])
print(array1.max())
print(array1.min())
Array Reshaping - ravel()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# ravel returns a view of array1 from the original
ravelled_array = array1.ravel()
print(ravelled_array)
Array Reshaping - ravel()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# ravel returns a view of array1 from the original
ravelled_array = array1.ravel()
print(ravelled_array)
ravelled_array[2] = 999888777
print(ravelled_array)
print(array1)
Array Reshaping - flatten()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# flatten returns a copy of array1. uses new memory.
flattened_array = array1.flatten()
print(flattened_array)
Array Reshaping - flatten()
array1 = np.arange(9).reshape(3, 3)
print(array1)
# flatten returns a copy of array1. uses new memory.
flattened_array = array1.flatten()
print(flattened_array)
flattened_array[2] = 777888999
print(flattened_array)
print(array1)
Transpose
array2 = np.arange(9)
print(array2)
array2.shape = [3, 3]
print(array2)
print(array2.transpose())
print(array2.T)
Resize
print(np.resize(array2, (6, 6)))
Zeros
z_array = np.zeros((6,))
print(z_array)
z_array1 = np.zeros((6,), dtype=int)
print(z_array1)
z_array2 = np.zeros((2, 3), dtype=int)
print(z_array2)
Ones
o_array = np.ones((6,))
print(o_array)
o_array1 = np.ones((6,), dtype=int)
print(o_array1)
o_array2 = np.ones((2, 3), dtype=int)
print(o_array2)
Eye
# identity matrix
print(np.eye(6))
print(np.eye(6, dtype=int)
# array populated with random numbers
np.random.rand(4, 4)
Matrix Multiplication
matA = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
print(matA)
matB = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3, 2)
print(matB)
Matrix Multiplication
matA = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
matB = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3, 2)
print(matA * matB)
product = np.matmul(matA, matB)
print(product)
Matrix Multiplication
mat_a = np.array([0, 3, 5, 5, 5, 2]).reshape(2, 3)
mat_b = np.array([3, 4, 3, -2, 4, -2]).reshape(3, 2)
mat_ab = mat_a @ mat_b
print(mat_ab)
mat_ab1 = np.matmul(mat_a, mat_b)
print(mat_ab1)
Array Stacking
Array Stacking
A B
Array Stacking
A B
Horizontal Stacking Vertical Stacking
Array Stacking
# Stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
array1_array2 = np.hstack((array1, array2))
print(array1_array2)
Array Stacking
# Stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(2, -1)
print(array1)
print(array2)
array1_array2 = np.hstack((array1, array2))
print(array1_array2)
print(array1_array2.shape)
Array Stacking – Vertical Stacking
# vertical stacking
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(-1, 2)
print(array1)
print(array2)
a1_a2 = np.vstack((array1, array2))
print(a1_a2)
print(a1_a2.shape)
Array Stacking, Concatenation
A B
Horizontal Stacking Vertical Stacking
Concatenate
Axis = 1
Concatenate
Axis = 0
Array Stacking – Concatenation
# concatenation along an axis
# horizontal - 1
# vertical - 0
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
print(np.concatenate((array1, array2), 0))
print(np.concatenate((array1, array2), 1))
Array Stacking – Concatenation
# concatenation along an axis
# horizontal – 1. # vertical - 0
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 12).reshape(-1, 2)
# array2 = np.arange(4, 12).reshape(2, -1)
print(array1)
print(array2)
print(np.concatenate((array1, array2), axis = 0))
#print(np.concatenate((array1, array2), axis = 1))
np.vstack((array1, array2)) == np.concatenate((array1,
array2), axis = 0)
hstack and vstack
●
Horizontal Stacking (hstack)
– Along the second axis (n, m)
– Increases number of columns
●
Vertical Stacking (vstack)
– Along the first axis (n, m)
– Increases number of rows
Depth Stacking
●
Stack along the z-axis
A B
Depth Stacking
●
Stack along the z-axis
A (2,2) A (2,2,1)
B (2,2)
B (2,2,1)
Depth Stacking
●
Stack along the z-axis
A (2,2) A (2,2,1)
C (2,2,2)
B (2,2)
Depth
Stacking
B (2,2,1)
Array Stacking – Depth Stack
# depth stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
# dstack reshapes a1 (2,2) to a1 (2, 2, 1)
# dstack reshapes a2 (2,2) to a2 (2, 2, 1)
# stacks them side by side (2, 2, 2)
depth_stack = np.dstack((array1, array2))
print(depth_stack)
Array Stacking – Depth Stack
# depth stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2, 1)
array2 = np.arange(4, 8).reshape(2, 2, 1)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
depth_stack = np.dstack((array1, array2))
print(depth_stack)
Column Stacking
A (2,2) B (2,2) AB (2,4)
Identical to
hstack
Column
Stacking
AB (4,2)
A (4) B (4)
Column
Stacking
Row Stacking
A (2,2) B (2,2) AB (4,2)
Identical to
vstack
Row
Stacking
A (4) B (4) AB (2,4)
Row
Stacking
Array Stacking – Column Stacking
# column stacking
# horizontal stacking along columns
# make rows, columns identical for a1 and a2
array1 = np.arange(4).reshape(2, 2)
array2 = np.arange(4, 8).reshape(2, 2)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
col_stack = np.column_stack((array1, array2))
print(col_stack)
print(col_stack == np.hstack((array1, array2)))
Array Stacking – Column Stacking
# column stacking
# horizontal stacking along columns
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
col_stack = np.column_stack((array1, array2))
print(col_stack)
print(np.hstack((array1, array2)))
#print(col_stack == np.hstack((array1, array2)))
Array Stacking – Row Stacking
# row stacking
# vertical stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
# stacks them side by side (2, 2, 2)
row_stack = np.row_stack((array1, array2))
print(row_stack)
Array Stacking – Row Stacking
# row stacking
# vertical stacking
# make rows, columns identical for a1 and a2
array1 = np.arange(4)
array2 = np.arange(4, 8)
print(array1)
print(array2)
row_stack = np.row_stack((array1, array2))
print(row_stack)
Summary
●
Arrays, Multidimensional Arrays
– Creation, Shape, Indexing
– Linear Array, spaced equally
●
Array Slicing, Array Reshaping – ravel(), flatten()
●
Transpose, Resize
●
Zeros, Ones, Eye
●
Matrix Multiplication
●
Array Stacking – Vertical Stacking, Concatenation, Depth Stack, Column
Stacking, Row Stacking