2 - Numpy - Tutorial - Ipynb - Colaboratory
2 - Numpy - Tutorial - Ipynb - Colaboratory
2 - Numpy - Tutorial - Ipynb - Colaboratory
ipynb - Colaboratory
NumPy
Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working
with these arrays. If you are already familiar with MATLAB, you might find this tutorial useful to get started with Numpy.
It is implemented in C and Fortran so when calculations are vectorized (formulated with vectors and matrices), performance is very good.
import numpy as np
Arrays
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank
of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
We can initialize numpy arrays from nested Python lists, and access elements using square brackets:
[[1 2 3]
[4 5 6]]
print(b.shape)
print(b[0, 0], b[0, 1], b[1, 0])
(2, 3)
1 2 4
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 1/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
[[0. 0.]
[0. 0.]]
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
[[7 7]
[7 7]]
[[1. 0.]
[0. 1.]]
[[0.42464997 0.65992556]
[0.52983439 0.58744048]]
Another useful routine is linspace for creating linearly spaced values in an interval. For instance, to create 10 values in [0, 1] , we can use
np.linspace(0, 1, 10)
x = np.array([[2,3,4], [5,6,7]])
np.copy(x) + np.array([[8,9,10]])
import copy
h = x.copy() + np.array([[8,9,10]])
x, h
(array([[2, 3, 4],
[5, 6, 7]]),
array([[10, 12, 14],
[13, 15, 17]]))
Array indexing
Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension
of the array.
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 2/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
import numpy as np
# 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):
# [[2 3]
# [6 7]]
b = a[0,0]
print(b)
c = a[:2, 1:3]
print(c)
1
[[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 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])
77
77
You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array. Note that this is
quite different from the way that MATLAB handles array slicing:
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 3/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
[[ 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:
[5 6 7 8] (4,)
[[5 6 7 8]] (1, 4)
[[5 6 7 8]] (1, 4)
[ 2 6 10] (3,)
[[ 2]
[ 6]
[10]] (3, 1)
Integer array indexing: When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In
contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. Here is an example:
[[1 2]
[3 4]
[5 6]]
[1 4 5]
[1 4 5]
# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]])
[2 2]
[2 2]
One useful trick with integer array indexing is selecting or mutating one element from each row of a matrix:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 4/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
[ 1 6 7 11]
[[21 2 3]
[ 4 5 26]
[27 8 9]
[10 31 12]]
Boolean array indexing: Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select
the elements of an array that satisfy some condition. Here is an example:
import numpy as np
print(bool_idx)
[[False False]
[ True True]
[ True False]]
[3 4 5]
[3 4 5]
For brevity we have left out a lot of details about numpy array indexing; if you want to know more you should read the documentation.
Datatypes
Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct
arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument
to explicitly specify the datatype. Here is an example:
Array math
Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy
module:
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 5/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
x = np.array([[1,2],[3,4]], dtype=np.float32)
y = np.array([[5,6],[7,8]], dtype=np.float32)
[[ 6. 8.]
[10. 12.]]
[[ 6. 8.]
[10. 12.]]
[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]
[[ 5. 12.]
[21. 32.]]
[[ 5. 12.]
[21. 32.]]
[[0.2 0.33333334]
[0.42857143 0.5 ]]
[[0.2 0.33333334]
[0.42857143 0.5 ]]
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 6/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
[[1. 1.4142135]
[1.7320508 2. ]]
Note that unlike MATLAB, * is elementwise multiplication, not matrix multiplication. We instead use the dot function to compute inner products
of vectors, to multiply a vector by a matrix, and to multiply matrices. dot is available both as a function in the numpy module and as an instance
method of array objects:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9, 10])
w = np.array([11, 12])
219
219
For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors.
You can also use the @ operator which is equivalent to numpy's dot operator.
print(x*y)
print(x @ y)
print(np.dot(x,y))
[[ 5 12]
[21 32]]
[[19 22]
[43 50]]
[[19 22]
[43 50]]
# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))
print(x @ v)
[29 67]
[29 67]
[29 67]
[[19 22]
[43 50]]
[[19 22]
[43 50]]
[[19 22]
[43 50]]
Numpy provides many useful functions for performing computations on arrays; one of the most useful is sum :
x = np.array([[1,2],[3,4]])
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 7/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
print(np.sum(x)) # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"
10
[4 6]
[3 7]
print(np.min(x))
print(np.max(x))
print(np.mean(x))
print(np.std(x))
1
4
2.5
1.118033988749895
You can find the full list of mathematical functions provided by numpy in the documentation.
Broadcasting
Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible.
Array Manipulation
Apart from computing mathematical functions using arrays, we frequently need to reshape or otherwise manipulate data in arrays. The
simplest example of this type of operation is transposing a matrix; to transpose a matrix, simply use the T attribute of an array object
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 8/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
x = np.array([[3,9,1],[1,2,3], [9,3,2], [6,9,1]])
print(np.transpose(x))
print(x.T) #x must be numpy array
x
[[3 1 9 6]
[9 2 3 9]
[1 3 2 1]]
[[3 1 9 6]
[9 2 3 9]
[1 3 2 1]]
array([[3, 9, 1],
[1, 2, 3],
[9, 3, 2],
[6, 9, 1]])
#append an array
print(np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9]), "\n")
[1 2 3 4 5 6 7 8 9]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
#Reshaping Array
print(np.reshape(x,(3,4)))
print(np.ravel(x))
#Resize array
'''
np.resize does not consider axes separately, i.e. it does not apply interpolation/extrapolation. It fills the return array with the requi
of elements, iterating over a in C-order, disregarding axes (and cycling back from the start if the new shape is larger).
This functionality is therefore not suitable to resize images, or data where each axis represents a separate and distinct entity.
'''
np.resize(x, (4,5))
[[3 9 1 1]
[2 3 9 3]
[2 6 9 1]]
[3 9 1 1 2 3 9 3 2 6 9 1]
array([[3, 9, 1, 1, 2],
[3, 9, 3, 2, 6],
[9, 1, 3, 9, 1],
[1, 2, 3, 9, 3]])
#Array Concatenation
#The arrays must have the same shape, except in the dimension corresponding to axis.
a = np.array([[1, 2,5,2], [3, 4,5,2], [1,2,3,4]]) #3x4
b = np.array([[5,6,8,9],[5,2,3,7]]) #2x4
[[1 2 5 2]
[3 4 5 2]
[1 2 3 4]
[5 6 8 9]
[5 2 3 7]]
[[1 2 5 2 3 4 5 6 8 9]
[5 2 1 2 3 4 5 2 3 7]]
[1 2 5 2 3 4 5 2 1 2 3 4 5 6 8 9 5 2 3 7]
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 9/10
11/6/23, 2:41 PM 2_Numpy_tutorial.ipynb - Colaboratory
print(np.hstack((a,b)))
print(np.vstack((a,b)))
[[3 5]
[5 7]
[7 9]]
[[3]
[5]
[7]
[5]
[7]
[9]]
https://colab.research.google.com/drive/1T6vnjSxuZ9ew_mMfus-Oz3A3vtfb1zLM#scrollTo=hPtrZWuDNb6b&printMode=true 10/10