Arrays
The shape of an array is a tuple of integers giving the size of the array along each dimension.
import numpy as np
a = np.array([1,2,3]) # a rank 1 array
print(type(a))
<class 'numpy.ndarray'>
print(a,shape)
(3,)
print(a[0],a[1],a[2])
1 2 3
a[0] = 5
print(a)
[5 2 3]
b = np.array([[1,2,3],[4,5,6]]) # create a rank 2 array
print(b.shape)
(2, 3)
print(b[0,0], b[0,1], b[1,0])
1 2 4
Functions to create arrays:
a = np.zeros((2,2))
print(a)
[[0. 0.]
[0. 0.]]
b = np.ones((1,2))
print(b)
[[1. 1.]]
c = np.full((2,2),7)
print(c)
[[7 7]
[7 7]]
d = np.eye(2)
print(d)
[[1. 0.]
[0. 1.]]
e = np.random.random((2,2))
print(e)
[[0.89711571 0.75461668]
[0.46603435 0.80525694]]
Array Indexing
Slicing: similar to Python lists, numpy arrays can be sliced. Must specify a slice for each dimension of the array
import numpy as np
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
#use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2,2):
b = a[:2, 1:3]
print(b)
array([[2, 3],
[6, 7]])
# a slice of an array is a view into the same data, so modifying it
# will modify the original array
print(a[0,1])
b[0,0] = 77
print(a[0,1])
77
Mixing integer indexing with slice indexing will yield an array of lower rank than the original array
import numpy as np
# Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"
# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape) # Prints "[ 2 6 10] (3,)"
print(col_r2, col_r2.shape) # Prints "[[ 2]
# [ 6]
# [10]] (3, 1)"
Integer array indexing: using slicing, the result array view will always be a subarray of the original one. In contrast, integer array indexing can construct arbitrary arrays using the data from another array.
import numpy as np
a = np.array([[1,2], [3, 4], [5, 6]])
print(a[[0,1,2]])
[[1 2]
[3 4]
[5 6]]
print(a[[0, 1, 2], [0, 1, 0]])
[1 4 5]
# The above example of integer array indexing is equivalent to this:
print(np.array([a[0, 0], a[1, 1], a[2, 0]])) # Prints "[1 4 5]"
# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]]) # Prints "[2 2]"
print(a[[0,0]])
[[1 2]
[1 2]]
# Equivalent to the previous integer array indexing example
print(np.array([a[0, 1], a[0, 1]])) # Prints "[2 2]"
import numpy as np
# Create a new array from which we will select elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(a) # prints "array([[ 1, 2, 3],
# [ 4, 5, 6],
# [ 7, 8, 9],
# [10, 11, 12]])"
# Create an array of indices
b = np.array([0, 2, 0, 1])
# Select one element from each row of a using the indices in b
print(a[np.arange(4), b]) # Prints "[ 1 6 7 11]"
# np.arange(4) = array([0, 1, 2, 3]) from each row we select
# the corresponding column: 0, 2, 0, 1
# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10
print(a) # prints "array([[11, 2, 3],
# [ 4, 5, 16],
# [17, 8, 9],
# [10, 21, 12]])
Array Math
Basic mathematical funcitons operate elementwise on arrays, and are available both as operator overloads as functions in the numpy module
import numpy as np
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
# elementwise sum
#[[ 6. 8.]
# [10. 12.]]
print(x + y)
print(np.add(x, y))
#elementwise product
# [[ 5.0 12.0]
# [21.0 32.0]]
print(x * y)
print(np.multiply(x, y))
# Elementwise square root; produces the array
# [[ 1. 1.41421356]
# [ 1.73205081 2. ]]
print(np.sqrt(x))
*
in numpy is elementwise multiplication. We can use dot
to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices.
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
# output 219
print(v.dot(w))
# output array([29, 67])
x.dot(v)
# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
# [43 50]]
print(x.dot(y))
usage of sum
import numpy as np
x = np.array([[1, 2],[3, 4]])
print(np.sum(x)) # compute the sum of all elements; print 10
print(np.sum(x, axis = 0)) # compute the sum of each column; prints [4,6]
print(np.sum(x,axis = 1)) # compute the sum of each row: print ]3,6]
usage of transpose
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(x)
#[[1 2]
# [3 4]]
print(x.T)
#[[1 3]
# [2 4]]