The "numpy" is the efficient numerical computation library of python. We start with defining arrays and their basic manipulation. Arrays are like lists except their type is fixed for each element. The common types are int
and float
.
We start with a one dimensional array of size 3.
In [2]:
import numpy as np
a = np.array([1, 2, 3])
print type(a)
print a.shape
In [2]:
print a
Now we define a two-dimensional array of size 2$\times$3
In [3]:
b = np.array([[1, 2, 3], [4, 5, 6]])
print b.shape
In [4]:
print b[:, :]
Any time you need help you can write "numpy.array?", you may initialize a matrix using "numpy.full"
In [5]:
print np.full((2, 3), 7.0)
This initializes a 2$\times$3 with value float 7.0. try other functions like numpy.zeros
, numpy.ones
, and numpy.eye
In [6]:
print np.zeros((3, 3))
In [7]:
print np.eye(4) #gives identity matrix
Using the boolean array indexing you pick up the elements of an array. This type of indexing helps you to select the elements of an array that satisfy a condition. You must be aware that arrays in numpy is different from lists in python. However, we can convert a numeric list to a numpy array using np.array
In [8]:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
print (a>2)
In [9]:
print a[a>2]
In [10]:
myarray=np.eye(10)
print myarray == 1
In [11]:
print myarray[myarray == 1]
In [12]:
print range(1,len(myarray)+1)
Exercise
Produce an identity matrix of size 10 and
replace the diagonal elements with 1 to 10.
In [65]:
myarray=np.eye(10)
myarray[myarray == 1] = range(1, len(myarray)+1)
print myarray
Replace the non-zero elements of the last matrix with increasing powers of 2.
In [66]:
type(myarray)
Out[66]:
In [67]:
vector = (range(1, len(myarray)+1))
(np.array(vector))**2
Out[67]:
In [15]:
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
print x+y
print np.add(x, y)
In [16]:
print x-y
print np.subtract(x, y)
In [17]:
print x*y
print np.multiply(x, y)
In [18]:
print x/y
print np.divide(x, y)
In [19]:
print x.dot(y)
print np.dot(x, y)
In [20]:
print np.sum(x, axis = 1) # on rows
print np.sum(x, axis = 0) # on columns
print np.sum(x, axis = None) # on both
In [21]:
print x.T
print np.sum(x.T, axis = 0) # gives sum over rows (and not columns even with axis=0)
In [22]:
print np.tile(x, (2, 3))
In [23]:
print range(10)
print np.reshape(range(10), (2, 5) )
In [24]:
print np.reshape(range(10),(5,2) )
Now let's use some image libraries and plot some pictures.
In [68]:
import matplotlib.pylab as plt
%matplotlib inline
img = plt.imread('../data/cat.jpg')
print img.shape
plt.imshow(img)
Out[68]:
In [26]:
img_tinted = img * [0.0, 0.0, 1.0] #Red Green Blue
print img_tinted.shape
In [27]:
plt.imshow(img_tinted)
plt.axis("off")
Out[27]:
In [28]:
img_tinted = img * [.27, .72, 0.07]
#img_tinted = img * [0.33, 0.33, 0.33]
print plt.imshow(img_tinted)
In [29]:
print plt.imshow(np.sum(img_tinted, axis = 2))
In [30]:
print plt.imshow(np.sum(img_tinted, axis=2), "gray")
plt.axis("off")
Out[30]: