NumPy is a package for scientific computing with Python.NumPy has one main data structure name ndarray, which is N-dimensional array. Each ndarray is associated with only one data type in dtype object.
To use NumPy, we import the module.
import numpy as np
Use arange() method to create a range(start, end, step) where the start is inclusive, the end is exclusive. If it has only one argument, it indicates the arange(end) and the default start is 0. If it has two arguments, it indicates arange(start, end) and the default step is 1.
shape attribute return the shape of the ndarray.
a=np.arange(5)
print(a)
a.shape
b=np.arange(10,32,3).reshape(2,4)
print(b)
b.shape
a=np.arange(0,20,2).reshape(5,2)
print(a)
a.shape
Use linspace(start, end, num) function to generate array similar to arange. However, the third argument is the number of elements rather than the step. The start and end are both inclusive. The generated elements are interpolation between the start and end.
a=np.linspace(1,23,12).reshape(3,4)
print(a)
a.shape
zeros((rows, cols),dtype) function creates a zero matrix size of a tuple (rows , cols).
z=np.zeros((3,4));z
ones((rows, cols),dtype) function creates a one matrix size of a tuple (rows , cols). By default the data type is float, but we can change to integer by adding int in the second argument.
n=np.ones((2,3),int);n
To generate random matrix, use function random.random((rows,cols)).
r=np.random.random((2,3));r
To generate random integer matrix, use function random.randint(start,end,num). The start is inclusive but the end is exclusive. If you want the end to be inclusive, make it end+1.
r=np.random.randint(1,10,12).reshape(4,3);r
Boolean array or matrix can be created by specifying a condition.
r=np.random.randint(1,10,12).reshape(4,3);
print(r)
r<7
Boolean Array is useful for masking or filtering
r=np.random.randint(1,10,12).reshape(4,3)
print(r)
m=(r<7)
print(m)
r[m]
Still another way to create ndarray is directly from array of either tuple or list.
g = np.array([(1, 2, 3), [4, 5, 6], (7, 8, 9)]); g
We can change the data type when we define the ndarray.
g = np.array([(1, 2, 3), [4, 5, 6], (7, 8, 9)],dtype=complex); g
Number of dimension can be more than 2. The following is three dimensional array.
a = np.arange(30).reshape(2, 3, 5)
print(a)
p p p p p
o o o o o
s s s s s
dim 2 0 1 2 3 4
| | | | |
dim 0 ↓ ↓ ↓ ↓ ↓
-> [[[ 0 1 2 3 4] <- dim 1, pos 0
pos 0 [ 5 6 7 8 9] <- dim 1, pos 1
[10 11 12 13 14]] <- dim 1, pos 2
dim 0
-> [[15 16 17 18 19] <- dim 1, pos 0
pos 1 [20 21 22 23 24] <- dim 1, pos 1
[25 26 27 28 29]]] <- dim 1, pos 2
↑ ↑ ↑ ↑ ↑
| | | | |
dim 2 p p p p p
o o o o o
s s s s s
0 1 2 3 4
We use colon notation to extract portions of an array to generate new ones. The colon notation is start:end:step . The start is inclusive while the end is exclusive. When the start is omitted, the default is 0. When the end is omitted, the default is the end of the array. When the step is omitted, the default is 1.
The following are examples in 1D array.
a = np.arange(10, 15)
a
a[2]
a[1:4]
a[:4]
a[:4:2]
a[:]
The following are examples in 2D array.
A = np.arange(12).reshape(4,3)+1
A
A[0:2,0:2]
A[2:,2:] # A[2:5,2:4]
A[[0,2],1:3] # A[0,1:3]; A[2,1:3]
A[[0,3,2],[0,2,1]] # A[0,0],A[3,2],A[2,1]
Use ravel() method to convert a two-dimensional array into a one-dimensional array.
g = np.array([(1, 2, 3), [4, 5, 6], (7, 8, 9)])
print(g)
g.ravel()
To put back to 2D array, use reshape() method.
h=np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
h.reshape(3,3)
Horizontal concatenation using hstack() function.
A = np.ones((3, 4),int)
B = np.zeros((3, 2),int)
C = np.hstack((A, B))
C
Vertical concatenation using vstack() function
A = np.ones((2, 2),int)
B = np.zeros((3, 2),int)
C = np.vstack((A, B))
C
Multiple arrays can be joined using column_stack() and row_stack() functions.
a = np.array([1,3,2])
b = np.array([5,6,4])
c = np.array([7,9,8])
np.column_stack((a,b,c))
np.row_stack((a,b,c))
In this section we will use the following matrix
A = np.arange(24).reshape(4,6)
A
Use split(matrix,location,axis) function to separate matrix or array into several parts.
[B,C,D]=np.split(A,[2,5],axis=1)
B
C
D
To separate a matrix or array into two parts, use vsplit() to separate vertically and hsplit() to separate horizontally.
A = np.arange(24).reshape(4,6)
[B,C]=np.hsplit(A,2)
B
C
[D,E]=np.vsplit(A,2)
D
E
To change the position between rows and column, use transpose operator array.T
A=np.arange(1,24,2).reshape(3,4)
print(A)
print()
print(A.T)
There are several ways to multiply two matrices C = A * B :
Z=np.arange(1,24,2).reshape(3,4)
[A,B]=np.split(Z,2,1)
print('A=',A); print(); print('B=',B)
C=np.dot(A,B.T)
print(C)
C=A.dot(B.T)
print(C)
Z=np.arange(1,24,2).reshape(3,4)
[A,B]=np.split(Z,2,1)
print('A=',A); print(); print('B=',B)
C=A+B
C
D=A-B
D
Z=np.arange(12).reshape(3,4)
Z
Z += 1
Z
Z -= 2
Z
Z *= 2
Z
Universal functions are functions that operate on elements of an array.
and so on
Z=np.arange(6).reshape(2,3)+1
Z
np.log(Z)
np.sin(Z)
np.sqrt(Z)
np.exp(Z)
NumPy array has several statisical methods to aggregate
Z=np.arange(6).reshape(2,3)+1
Z
Z.sum()
Z.mean()
Z.std()
Z.min()
Z.max()
To aggregate with other functions or to apply the aggregation function along sum axis use apply_along_axis(aggrFunc, axis, arr)
The aggregation function can be any function.
np.apply_along_axis(np.mean, axis=0, arr=Z)
np.apply_along_axis(np.sum, axis=1, arr=Z)
last update: August 2017
Cite this tutorial as: Teknomo,K. (2017) Learning Numpy (http://people.revoledu.com/kardi/tutorial/Python/)
See Also: Python for Data Science
Visit www.Revoledu.com for more tutorials in Data Science
Copyright © 2017 Kardi Teknomo
Permission is granted to share this notebook as long as the copyright notice is intact.