chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Introduction on Numpy

2018-01-20

安装 numpy

pip3 install numpy

Some basic examples:

import numpy as np

# Create rank 1 array
#[1,2,3]
a = np.array([1, 2, 3])

# Create rank 2 array
#[[ 1, 2, 3],
# [ 4, 5, 6]]
b = np.array([(1,2,3), (4,5,6)])

# matrix with diff data types
b = np.array([(1,2,3), (4,5,"a")]) 

a and b are ndarray type, coming from package: numpy.array. ndarray has the following features:

  • ndarray.ndim: Dimension of the matrix.
  • ndarray.shape: The Shape of an array. i.e. shape(2,3) means 2 rows by 3 columns.
  • ndarray.size: Total number of the elements of this matrix.
  • ndarray.dtype: The element type of the matrix. Notice this matrix can be a mixture of diff data type. numpy.int32, numpy.int16, numpy.float64 and etc.

Some specific array

import numpy as np

# Create a 2x3 with all 0 elements matrix
a = np.zeros((2,3))
print('np.zeros((2,3)= \n{}\n'.format(a))

# Create a 2x3 with all 1 elements matrix
b = np.ones((2,3))
print('np.ones((2,3))= \n{}\n'.format(b))

# Create a 2x3 with all empty (previous matrix value) elements matrix
c = np.empty((2,3))
print('np.empty((2,3))= \n{}\n'.format(c))

# Create a 2x3 with all random elements matrix
f = np.random.random((2,3))
print('np.random.random((2,3))= \n{}\n'.format(f))

# Create a 1 dimension array start from 1, less than 2, step is 0.3
d = np.arange(1, 2, 0.3)
print('np.arange(1, 2, 0.3)= \n{}\n'.format(d))

Manuplate Shape

Method Explanation
reshape Turn current matric into a new matrix with specified (n, m). If you are not sure about the n or m, use -1 instead, it will automatically calculate the corresponding m or n for you. Example 1.
vstack Put two or more matrixs together vertically. So all the matrixs should have the same amount of columns. Example 2.
hstack Put two or more matrixs together horizontally. So all the matrixs should have the same amount of rows. Example 3.
hsplit 1. Divid current matrix horizontally into n small matrixs. Error throws out if cannot be divided evenly. numpy.ndarray format. Example 4. Split
hsplit 2. Divid current matrix vertically into n small matrixs using given columns. numpy.ndarray format. Example 5.
vsplit 1. Divid current matrix horizontally into n small matrixs. Error throws out if cannot be divided evenly. numpy.ndarray format.
vsplit 2. Divid current matrix vertically into n small matrixs using given columns. numpy.ndarray format.
import numpy as np

# Example 1: Turn an 1x9 matrix into 3x3
b = np.arange(11, 20)
print(b.reshape(3, 3))

# or you can write like:
print(b.reshape(3, -1))


# Example 2: Put 3 matrix together vertically.
b = np.arange(11, 20)

b1 = b.reshape(3, 3)
b2 = np.zeros((1,3))
b3 = b.reshape(-1, 3)

c = np.vstack((b1, b2, b3))
print("{}".format(c))

[[11. 12. 13.]
 [14. 15. 16.]
 [17. 18. 19.]
 [ 0.  0.  0.]
 [11. 12. 13.]
 [14. 15. 16.]
 [17. 18. 19.]]

# Example 3: Put 3 matrix together horizontally.

b = np.arange(11, 20)

b1 = b.reshape(3, 3)
b2 = np.zeros((3, 1))
b3 = b.reshape(-1, 3)

c = np.hstack((b1, b2, b3))
print("{}".format(c))

[[11. 12. 13.  0. 11. 12. 13.]
 [14. 15. 16.  0. 14. 15. 16.]
 [17. 18. 19.  0. 17. 18. 19.]]


# Example 4: Split matrix evenly at hiriontal direction.
b = np.arange(11, 20)
b1 = b.reshape(3, 3)
b2 = np.zeros((3, 3))
b3 = b.reshape(-1, 3)

c = np.hstack((b1, b2, b3))

print("{}".format(c))
print("{}".format(np.hsplit(c, 3)[1]))

[[11. 12. 13.  0.  0.  0. 11. 12. 13.]
 [14. 15. 16.  0.  0.  0. 14. 15. 16.]
 [17. 18. 19.  0.  0.  0. 17. 18. 19.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]


# Example 5: Split matrix evenly at hiriontal direction.
b = np.arange(11, 20)
b1 = b.reshape(3, 3)
b2 = np.zeros((3, 3))
b3 = b.reshape(-1, 3)

c = np.hstack((b1, b2, b3))

print("{}".format(c))
print("{}".format(np.hsplit(c, (3, 3))))

[[11. 12. 13.  0.  0.  0. 11. 12. 13.]
 [14. 15. 16.  0.  0.  0. 14. 15. 16.]
 [17. 18. 19.  0.  0.  0. 17. 18. 19.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]


# Example 5: Split matrix with given colum index:
print("{}".format(np.hsplit(c, (3))[1]))
[[11. 12. 13.  0.  0.  0. 11. 12. 13.]
 [14. 15. 16.  0.  0.  0. 14. 15. 16.]
 [17. 18. 19.  0.  0.  0. 17. 18. 19.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Get elements by index

#1. Get single element: numpy.float64 type
print(c[0,0])
11.0 

#2. Get most right and bottom element: numpy.float64 type
print(c[-1, -1])
19.0

#3. Get the whole row
print("{}".format(c[0]))
print("{}".format(c[-1]))

#4. Get the whole column
print("{}".format(c[:,0]))
print("{}".format(c[:,-1]))

#5 Get element in range
print("{}".format(c))
print("{}".format(c[1:3,1:3]))
[[11. 12. 13.  0.  0.  0. 11. 12. 13.]
 [14. 15. 16.  0.  0.  0. 14. 15. 16.]
 [17. 18. 19.  0.  0.  0. 17. 18. 19.]]
[[15. 16.]
 [18. 19.]]

Reference

Introduction on Numpy

arkilis

Make Your Comments