{:check ["true"]}
We will be manipulating tensors in Python. The primary library we will focus on will be NumPy.
NumPy uses native libraries for highly efficient storage and accelerated performance.
import numpy as np
import matplotlib.pyplot as pl
np.__version__
Tensor provides a generalization of scalars, vectors, matrices and deeper nested numeric arrays.
A person's income can be described as a scalar.
$$ \mathrm{income} = 60000 $$If we have a group of individuals, we can view their individual incomes as a vector.
$$ \mathrm{team} = [60000, 62000, 62000, 70000] $$
If we know more about a single person, e.g. their age, height, we can represent these numerical attributes as a vector
$$ \mathrm{person} = \left[\begin{array}{l} \$60000 \\ 65 \mathrm{yo} \\ 1.78 \mathrm{m} \\ \end{array} \right]$$
A grayscale image is a 2D matrix. Each entry is a scalar value representing the intensity of that pixel.
$$ \mathrm{image} = \left[\begin{array}{} 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 \\ \end{array}\right]$$
A collection personal records is a 2D matrix with each person as a vector.
$$ \mathrm{persons} = \left[\begin{array}{cc} \$60000 & \$62000\\ 65 \mathrm{yo} & 60\mathrm{yo} \\ 1.78 & 1.82\mathrm{m} \\ \end{array} \right]$$
A color image is a tensor with three axes. For example, a color image of size 5x5 will be a tensor with shape (5, 5, 3)
. The axis with three entries will
store the RGB value of that pixel.
If we monitor the persons over a period of 10 years, then the observation forms a tensor with shape
(10, 3, 2)
. There will be 10 matrices, and each
matrix is 3x2, describing their income, age and height.
Python arrays can be converted to NDArray using array(...)
.
# Converting python list into vectors
np.array([1, 2, 3])
# We can specify the datatype as well.
np.array([1, 2, 3], dtype='float32')
# Higher rank tensors can be constructed
# from nested Python lists.
np.array([
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
])
# We can load common image formats using the
# Python Image Library (PIL), which can then
# be converted to NumPy arrays.
from PIL import Image
im = Image.open('./blackhole.jpg')
im_array = np.array(im)
print("Array size", im_array.shape)
pl.imshow(im_array);
# We can load from a file.
with open('./data.txt') as f:
print(f.read())
file_array = np.loadtxt('./data.txt', delimiter=",")
file_array
We can also create NDArrays from a Python generator.
#
# Let's build a function that constructs
# a generator of Fibonacci numbers.
#
def get_fib(n):
a = 1
b = 1
for i in range(n):
if i <= 1:
yield 1
else:
x = a + b
a,b = b, x
yield x
print(get_fib(10))
#
# We can load the generator into a NDArray
# directly without going through Python list.
#
np.fromiter(get_fib(10), int)
We can construct empty NDArrays using various functions.
np.empty(shape=(2,2), dtype=int)
np.zeros(shape=(2,2))
np.ones(shape=(2,2))
np.eye(5)
np.full(shape=(5,5), fill_value=3.1415)
Here are a few more ways of constructing NDArrays that follow a certain pattern.
Linear sequence
Logarithmic sequence
np.arange(10)
np.linspace(0, 2 * np.pi, 10)
np.logspace(a, b, n)
constructs n
values in the range:
evenly in logspace.
np.logspace(0, 1, 10)
Meshgrid is a pair of matrices that describe spatial coordinates in the XY plane.
xx, yy = np.meshgrid(np.arange(10), np.arange(5))
print('xx ='); print(repr(xx))
print('yy ='); print(repr(yy))