# NumPy Learning Notes

Programming | 28 March 2017

NumPy is a scientific computing package for Python programming language which is highly used when matrices and vectors are involved for computations. Some of the domains where NumPy is used are Deep Learning, Computer Vision, Machine Learning, Image Processing, Data Analytics etc.

In this page, you will find the syntax and most common how to’s of NumPy in Python. This might be useful for python beginners to find syntax and code quickly online in a single page.

### Contents

Update: As Python2 faces end of life, the below code only supports Python3.

### Check numpy versioncode

1
2
import numpy as np
print(np.__version__)  # prints "1.11.3" for me


### Basic Array Representationcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# create a 3x1 array (datatype="int" chosen by numpy)
a = np.array([1,2,3])

print(a)       # prints "[1,2,3]"
print(a.shape) # prints "[3L,]"
print(type(a)) # prints "<type 'numpy.ndarray'>"

# create a 3x1 array with datatype "float"
b = np.array([1.1,2.2,3.3], dtype="float")

print(b)       # prints "[1.1,2.2,3.3]"
print(b.shape) # prints "[3L,]"
print(type(b)) # prints "<type 'numpy.ndarray'>"

# accessing elements in the numpy array
print(a)       # prints "1"
print(b)       # prints "3.3"
print(type(a)) # prints "<type 'numpy.int32'>"
print(type(b)) # prints "<type 'numpy.float64'>"

# create a 2x2 array
c = np.array([[1,2],[3,4]])

print(c)         # prints "[[1,2],[3,4]]"
print(c.shape)   # prints "(2L, 2L)"


### How to create a numpy array of ones?code

1
2
3
4
5
6
7
8
# create a 2x2 array full of ones (dtype=float64 chosen by numpy)
d = np.ones((2,2))

print(d)             # prints "[[ 1.  1.]
#          [ 1.  1.]]"
print(d.shape)       # prints "(2L, 2L)"
print(type(d))       # prints "<type 'numpy.ndarray'>"
print(type(d)) # prints "<type 'numpy.float64'>"


### How to create a numpy array of zeros?code

1
2
3
4
5
6
7
8
9
# create a 3x3 array full of zeros (dtype=float64 by default)
e = np.zeros((3,3))

print(e)             # prints "[[ 0.  0.  0.]
#          [ 0.  0.  0.]
#          [ 0.  0.  0.]]"
print(e.shape)       # prints "(3L, 3L)"
print(type(e))       # prints "<type 'numpy.ndarray'>"
print(type(e)) # prints "<type 'numpy.float64'>"


### How to create a numpy array of random values?code

1
2
3
4
5
6
# create a 2x2 matrix with random values
f = np.random.random((2,2))

print(f)        # prints "[[ 0.65155439  0.39628659]
#          [ 0.33349215  0.03323669]]" for me
print(f.shape)  # prints (2L, 2L)


### How to create a identity matrix in numpy?code

1
2
3
4
5
6
# create an identity matrix of order 3
g = np.eye(3)

print(g)             # prints "[[ 100.  100.]
#          [ 100.  100.]]"
print(g.shape)       # prints (2L, 2L)


### How to create a numpy array with a identical values?code

1
2
3
4
5
6
# create a 2x2 constant matrix of value "100" in each cell (dtype=float64 by default)
h = np.full((2,2), 100)

print(h)             # prints "[[ 100.  100.]
#          [ 100.  100.]]"
print(h.shape)       # prints (2L, 2L)


### Math operations in numpycode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# create two 2x2 matrices
a = np.array([[2,2],[2,2]])
b = np.array([[2,2],[2,2]])

# element-wise subtraction
sub = a - b
print(sub)               # prints "[[0 0]
print(np.subtract(a,b))  #          [0 0]]"

# element-wise multiplication
mul = a * b
print(mul)                  # prints "[[4 4]
print(np.multiply(a,b))     #          [4 4]]"

# element-wise division
div = a / b
print(div)                # prints "[[1 1]
print(np.divide(a,b))     #          [1 1]]"


### How to perform dot product in numpy?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# create a matrix and a vector
W = np.random.random((3,3))
x = np.array([1,2,3])

print(W)               # prints "[[ 0.3342051   0.87642564  0.35777489]
#          [ 0.24531674  0.36355452  0.39563227]
#          [ 0.83769694  0.7987359   0.97012682]]"
print(x)               # print("[1 2 3]"

print(W.shape)         # prints "(3L, 3L)"
print(x.shape)         # prints "(3L,)"

# take dot product between matrix and vector
inner = np.dot(W,x)

print(inner)           # prints "[ 3.16038104  2.15932259  5.34554919]"
print(inner.shape)     # prints "(3L,)"


### How to concatenate numpy arrays?code

1
2
3
4
5
6
7
8
9
10
11
12
# create two arrays
v1 = np.array([1,2,3])
v2 = np.array([4,5,6])

print(v1.shape)      # prints "(3L,)"
print(v2.shape)      # prints "(3L,)"

# concatenate arrays
concat = np.concatenate((v1, v2))  # note that arrays must be passed as tuples

print(concat)        # prints "[1 2 3 4 5 6]"
print(concat.shape)  # prints "(6L,)"


### How to concatenate numpy arrays along particular axis?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# create two matrices
v3 = np.array([[1,2,3],[4,5,6]])
v4 = np.array([[40,40,40],[50,50,50]])

print(v3.shape)      # prints "(2L,3L)"
print(v4.shape)      # prints "(2L,3L)"

# concatenate arrays along different axis
concat_0 = np.concatenate((v3, v4), axis=0)  # concatenate along axis=1
concat_1 = np.concatenate((v3, v4), axis=1)  # concatenate along axis=1

# vertical stack : same as np.concatenate with axis=0
v_stack  = np.vstack((v3,v4))

# horizontal stack : same as np.concatenate with axis=1
h_stack  = np.hstack((v3,v4))

print(concat_0)        # prints "[[ 1  2  3]
print(v_stack)         #          [ 4  5  6]
#          [40 40 40]
#          [50 50 50]]"
print(concat_0.shape)  # prints "(4L, 3L)"

print(concat_1)        # prints "[[ 1  2  3 40 40 40]
print(h_stack)         #          [ 4  5  6 50 50 50]]"
print(concat_1.shape)  # prints "(2L, 6L)"


### How to find transpose of a matrix in numpy?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# create a 2x3 matrix
m = np.random.random((2,3))

# take the transpose of matrix
mT = m.T

print(m)          # prints "[[ 0.91066068  0.43176095  0.3599931 ]
#          [ 0.3701366   0.26224812  0.76553986]]"
print(mT)         # prints "[[ 0.91066068  0.3701366 ]
#         [ 0.43176095  0.26224812]
#         [ 0.3599931   0.76553986]]"

print(m.shape)    # prints "(2L, 3L)"
print(mT.shape)   # prints "(3L, 2L)"


### How to compute sum of a matrix?code

1
2
3
4
5
6
7
# create a 2x3 matrix
p = np.array([[10,20,30],[40,50,60]])

print(p.shape)           # prints "(2L, 3L)"
print(np.sum(p))         # prints "210"
print(np.sum(p, axis=0)) # sum of each column: prints "[50 70 90]"
print(np.sum(p, axis=1)) # sum of each row   : prints "[60 150]"


### How to reshape a numpy array?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np

# create a 2x3 array
a = np.array([[1,1,1],[2,2,2]])

print(a)              # prints "[[1 1 1]
#         [2 2 2]]"
print(a.shape)        # prints "(2, 3)"

# make 2x3 array to 6x1 array
b = np.reshape(a, 6)

print(b)              # prints "[1 1 1 2 2 2]"
print(b.shape)        # prints "(6,)"

# make 6x1 array to 3x2 array
c = np.reshape(b, (3,2))

print(c)              # prints "[[1 1]
# [1 2]
# [2 2]]"
print(c.shape)        # "(3, 2)"


### How to flatten a numpy array?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# create a 3x3 array
c = np.array([[1,2,3],[4,5,6],[7,8,9]])

print(c)          # prints "[[1 2 3]
#          [4 5 6]
#          [7 8 9]]"
print(c.shape)    # prints "(3, 3)"

# flatten the input array
d = np.ravel(c)
e = c.flatten()

print(d)          # prints "[1 2 3 4 5 6 7 8 9]"
print(e)          # prints "[1 2 3 4 5 6 7 8 9]"

# you can use order to specify how the array needs to be flattened
# order='F' --> To index elements in column major
# order='C' --> To index elements in row major
print(np.ravel(c, order='F'))  # prints "[1 4 7 2 5 8 3 6 9]"
print(np.ravel(c, order='C'))  # prints "[1 2 3 4 5 6 7 8 9]"


### How to expand the shape of a numpy array?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# create an array with 3 elements
a = np.array([500,200,100])

print(a)             # print("[500 200 100]"
print(a.shape)       # print("(3,)"

# insert a new axis along the row
b = np.expand_dims(a, axis=0)

# insert a new axis along the column
c = np.expand_dims(a, axis=1)

print(b)             # prints "[[500 200 100]]"
print(b.shape)       # prints "(1, 3)"

print(c)             # prints "[
#          
#          ]"
print(c.shape)       # prints "(3, 1)"


### How to find unique items in a numpy array?code

1
2
3
4
5
6
7
8
9
10
11
# create an array with 5 elements
m = np.array([1,2,1,1,4])

print(m)         # prints "[1 2 1 1 4]"
print(m.shape)   # prints "(5,)"

# get the unique elements and thier indexes
n, indices = np.unique(m, return_index=True)

print(n)         # prints "[1 2 4]"
print(indices)   # prints "[0 1 4]"


In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.