from numpy import array
from numpy import empty
from numpy import zeros
from numpy import ones
from numpy import vstack
from numpy import hstackNumPy
Introduction to NumPy Arrays
NumPy N-dimensional Array
my_list = [1.0, 2.0, 3.0]my_array = array(my_list)type(my_array)numpy.ndarray
print(my_array)[1. 2. 3.]
print(my_array.shape, my_array.dtype)(3,) float64
Functions to Create Arrays
Empty
my_array = empty([3,3])
my_arrayarray([[6.94676088e-310, 6.94676088e-310, 0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
[1.23075756e-312, 2.42092166e-322, 6.94676088e-310]])
Zeros
my_array = zeros([2,5])
my_arrayarray([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Ones
my_array = ones([4,2])
my_arrayarray([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]])
Combining Arrays
Vertical Stack
a1 = array([1,2,3])
print(a1)[1 2 3]
a2 = array([4,5,6])
print(a2)[4 5 6]
a3 = vstack((a1,a2))
print(a3)[[1 2 3]
[4 5 6]]
print(a3.shape)(2, 3)
Horizontal Stack
a1 = array([1,2,3])a2 = array([4,5,6])a3 = hstack((a1,a2))
print(a3)[1 2 3 4 5 6]
print(a3.shape)(6,)
Index, Slice and Reshape NumPy Arrays
From List to Arrays
One-Dimensional List to Array
data = [11,12,33,47]
print(data)[11, 12, 33, 47]
type(data)list
data_array = array(data)
print(data_array)[11 12 33 47]
type(data_array)numpy.ndarray
data_array.shape(4,)
Two-Dimensional List of Lists to Array
data = [[11,22],
[33, 44],
[55, 66]]print(data)[[11, 22], [33, 44], [55, 66]]
data_array = array(data)print(data_array)[[11 22]
[33 44]
[55 66]]
type(data_array)numpy.ndarray
data_array.shape(3, 2)
Array Indexing
One-Dimensional Indexing
data = array([11,22,33,44,55])data[0]11
data[4]55
#data[5]data[-1]55
data[-5]11
Two-Dimensional Indexing
data = array([[11,22],
[33, 44],
[55, 66]])data[0,0]11
data[0, ]array([11, 22])
Array Slicing
data [from : to]
One-Dimensional Slicing
data = array([11,22,33,44,55])print(data[:])[11 22 33 44 55]
print(data[0:3])[11 22 33]
print(data[-2:])[44 55]
Two-Dimensional Slicing
Split Input and Output Features
X = [: , : -1] this is the input
y = [: , -1] this is the output
data = array([[11,22,33],
[44,55,66],
[77,88,99]])X, y = data[: , :-1], data[:, -1]print(X)[[11 22]
[44 55]
[77 88]]
print(y)[33 66 99]
Split Train and Test Rows
train = data [ : split , :]
test = data [split : , :]
data = array([[11,22,33],
[44,55,66],
[77,88,99]])split = 2train, test = data[:split, :], data[split:,:]trainarray([[11, 22, 33],
[44, 55, 66]])
testarray([[77, 88, 99]])
Array Reshaping
Data shape
data = array([11,22,33,44])
data.shape(4,)
data = array([[11,22],
[33,44],
[55,66]])
data.shape(3, 2)
data.shape[0]3
data.shape[1]2
Reshape 1D to 2D Array
data = data.reshape( (data.shape[0] , 1) )
data = array([11,22,33,44,55])
data.shape(5,)
data = data.reshape((data.shape[0], 1))
data.shape(5, 1)
Reshape 2D to 3D Array
data = data.reshape( (data.shape[0], data.shape[1], 1) )
data = array([[11,22],
[33,44],
[55,66]])
data.shape(3, 2)
data = data.reshape((data.shape[0], data.shape[1], 1))
data.shape(3, 2, 1)
NumPy Array Broadcasting
Broadcasting in NumPy
Scalar and One-Dimensional Array
a = [a1, a2, a3]
b
c = a + b
c = [a1+b, a2+b, a3+b]
a = array([1,2,3])
print(a)[1 2 3]
b = 2
print(b)2
c = a + b
print(c)[3 4 5]
Scalar and Two-Dimensional Array
a = array([[1,2,3],
[1,2,3]])
print(a)[[1 2 3]
[1 2 3]]
b = 2c = a + bprint(c)[[3 4 5]
[3 4 5]]
One-Dimensional and Two-Dimensional Arrays
a = array([[1,2,3],
[1,2,3]])
print(a)[[1 2 3]
[1 2 3]]
b = array([4,5,6])c = a + bprint(c)[[5 7 9]
[5 7 9]]
Limitations of Broadcasting
A.shape = (2 x 3)
b.shape = (3)
A.shape = (2 x 3)
b.shape = (1 x 3)
A.shape = (2 x 3)
b.shape = (1)
A.shape = (2 x 3)
b.shape = (1 x 1)
A.shape = (2 x 3)
b.shape = (1 x 2)
a = array([[1,2,3],
[1,2,3]])
print(a)[[1 2 3]
[1 2 3]]
b = array([1,2])
print(b)[1 2]
#c = a + b