Slice of an array is a view into the same underlying data structure, thus modifying it will also modify the original.
x[0,1]# => 2x_slice[0,0]=10x[0,1]# => 10
You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array.
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]])# Rank 1 view of the second row of xrow_rank_1 = x[1,:]# Rank 2 view of the second row of xrow_rank_2 = x[1:2,:]
Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric data types that you can use to construct arrays.
x = np.array([1, 2])x.dtype # => dtype('int64')x = np.array([1.0, 2.0])x.dtype # => dtype('float64')# We can force a particular datatypex = np.array([1.2, 2.3], dtype=np.int64)x.dtype # => dtype('int64')
Suppose that we want to add a constant vector to each row of a matrix
# We begin by doing it with the inefficient way...x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])v = np.array([1, 1, 1])# Create an empty matrix that has x.shape, and then iterate throgh every row and perform additionout = np.empty_like(x)for i inrange(3): y[i,:]= x[i,:]+ v# We can also consider stacking v together and perform matrix element-wise additionvv = np.tile(v, (3, 1))y = x + vv
However, there is an even better way in numpy! We can perform the stacking method without actually creating multiple copies of v.
y = x + v
import numpy as npx = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])v = np.array([1, 1, 1])# This is known as array broadcasting, which is essentially what we did with stacking.x + v
array([[2, 2, 2],
[2, 2, 2],
[2, 2, 2]])
Image Operations
We need to shift gear a little bit and introduce scipy. Scientifc Python library provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images.
from scipy.misc import imread, imsave, imresizeimg =imread('assets/cat.jpg')img_tinted = img * [1.0,0.5,0.9] # Through array broadcastingimg_tinted =imresize(img_tinted, (300, 300))imsave('assets/cat_tinted.jpg', img_tinted)
Plots
Plots are essential in machine learning, it helps us with understanding our data and monitoring the training progress of a model. Thus, matplotlib comes in handy! The most important function in matplotlib is plot which allows you to plot 2D data, but of course there are other types of plot functions.
import numpy as npimport matplotlib.pyplot as plt%matplotlib inline x = np.arange(0, 3* np.pi, 0.1)y = np.sin(x)plt.ylim(-1, 1)plt.plot(x, y)plt.show()
With just a little bit of extra work, we can easily plot multiple lines at once. We can also add title, legend, and axis labels.
x = np.arange(0, 3* np.pi, 0.1)y_sin = np.sin(x)y_cos = np.cos(x)plt.plot(x, y_sin)plt.plot(x, y_cos)plt.ylim(-1, 1)plt.xlabel('x-axis label')plt.ylabel('y-axis label')plt.title('Sine and Cosine')plt.legend(['Sine', 'Cosine'])plt.show()
You can plot different things in the same figure using the subplot function.
x = np.arange(0, 3* np.pi, 0.1)y_sin = np.sin(x)y_cos = np.cos(x)# Set up a subplot grid which has 2 rows and 1 column.fig, axs = plt.subplots(2, 1, figsize=(8, 8))axs[0].plot(x, y_sin)axs[0].set_title('Sine')axs[1].plot(x, y_cos)axs[1].set_title('Cosine')plt.show()
We can also display images in numpy. A slight gotcha with imshow is that it only accepts uint8 data type. We need to explicitly cast the image to uint8 before displaying it.
fig = plt.figure(figsize=(10, 10))# 25 images, display them on a 5 by 5 gridfor i inrange(1, 26): img = np.random.randint(10, size=(10,10)) fig.add_subplot(5, 5, i) plt.imshow(img)plt.show()