arrayWe know how lists work in Python. We also know that lists can hold the data items of various data types. This means that the list storage allocated to elements can vary in size. This factor makes the list access slow, and operations on array could take long time. numpy provides a elagent solution in the form of ndarray, a $n$ - Dimensional collections of elements with same data types. numpy also provides easier way to manipulate arrays, This makes it High Performance Numerical Calculation possible.
numpyFollowing is the standard statement to import numpy. In future examples and library usages, we assume that you have imported the library in this way
In [1]:
import numpy as np
In [2]:
a = np.array([1,2,3,4])
a
Out[2]:
In [3]:
b = np.array([[1],[2],[3]])
b
Out[3]:
In [4]:
c = np.array([1,2,'x'])
c
Out[4]:
Note how int is converted into str. U21 is 32-bit Unicode Encoding. (Actual bits needed to store data is 21)
In [5]:
d = np.array([1.3,1,3])
d
Out[5]:
Note how int is converted to float
In [6]:
x = np.arange(20) # Like range(), but returns ndarry instead
x
Out[6]:
In [7]:
x.shape # (rows,cols)
Out[7]:
In [8]:
x.shape = (4,5) # 4 rows 5 cols
x
Out[8]:
In [9]:
x.size # Total number of elements
Out[9]:
In [10]:
np.random.shuffle(x) # shuffles ndarray in-place
x
Out[10]:
This is how we can shuffle an array. random.shuffle() function takes an ndarray as an argument and sorts it in place. NEVER treat its return value as result!
In [11]:
x[0]
Out[11]:
In [12]:
x[0][2] # Ok, inefficient
Out[12]:
Above method is inefficient access because, it fetches x[0] first and accesses it's element at index 2. Next method computes the address from 2 co-ordinates directly, and fetches the element at one access
In [13]:
x[0,2] # Efficient
Out[13]:
In [14]:
x[0,1:4]
Out[14]:
Above example selects the elements at indices (0,1),(0,2),(0,3). Note that the slices can also be used to select elements from multi-dimensional array
In [15]:
x[1:4,0]
Out[15]:
In [16]:
x > 15
Out[16]:
Note that it returned a boolean array after performing suitable operation. It is called masked array
In [17]:
x [ x > 15 ]
Out[17]:
This method to access array element is called as Access by Masked array
In [18]:
np.sin(x)
Out[18]:
In [19]:
x[np.sin(x) > 0] # elements whose sine is non-negative
Out[19]:
many trigonometrical functions like $sin$,$cos$, calculus related functions like $grad$ are also available
concatenate the arraysconcatenate((a1, a2, ...), axis=0)
Join a sequence of arrays along an existing axis.
Parameters:
a1, a2, ... : sequence of array_like
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
hstack((a1, a2, ...)) combines a1, a2, ... horizontallyvstack((a1, a2, ...)) combines a1, a2, ... verticallydstack((a1, a2, ...)) combines a1, a2, ... depthwise
In [20]:
a = np.array([1,2,3,4])
b = np.array([9,8,7,6])
In [21]:
a
Out[21]:
In [22]:
b
Out[22]:
In [23]:
np.concatenate((a,b),axis=0) # (a,b) is a tuple of arrays
Out[23]:
In [24]:
np.dstack((a,b))
Out[24]:
In [25]:
np.vstack((a,b))
Out[25]:
In [26]:
np.hstack((a,b))
Out[26]:
We will use these functions frequently in upcoming chapters
In [27]:
s = np.sin(x)
s
Out[27]:
In [28]:
np.sum(s) # You understood it, right?!
Out[28]:
In [29]:
np.average(s)
Out[29]:
In [30]:
np.min(x)
Out[30]:
In [31]:
np.max(s)
Out[31]:
At current point, we will stop. This basic understanding of numpy is enough to understand the concepts of Algorithm Analysis in upcoming part.
Interested readers can refer the NumPy Official Tutorial at SciPy