1. Matrix dimension transformation
1.1 numpy.reshape(a, newshape, order=’C’)
The reshape() function is often used to change the dimension of a one-dimensional array, that is, to change a one-dimensional array into a matrix of the specified dimension. Order refers to different indexing rules, generally defaulting to C, and operating according to the row.
Example:
print np.reshape(np.arange(10), (2, 5))
[[0 1 2 3 4]
[5 6 7 8 9]]
1.2 numpy.ravel(a, order=’C’)
The ravel function is a dimensionality reduction operation for matrix data, for example, reducing two-dimensional data to one-dimensional
Example :
data = np.reshape(np.arange(10), (2, 5))
print data.reshape(-1)
print data.ravel()
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
1.3 flatten([order])
This member function is also used for dimensionality reduction, and works similarly to the ravel function
Example :
data = np.reshape(np.arange(10), (2, 5))
print data.flatten()
[0 1 2 3 4 5 6 7 8 9]
The difference from the ravel function: ravel is a view of the original data, and the original data will be modified after it is modified; while the flatten function returns a copy of the original data, and modifying it will not affect the original data.
Example:
# ravel函数
data = np.reshape(np.arange(10), (2, 5))
data1 = data.ravel()
data1[0] = 100
print data
[[100 1 2 3 4]
[ 5 6 7 8 9]]
# flatten函数
data = np.reshape(np.arange(10), (2, 5))
data1 = data.flatten()
data1[0] = 100
print data
[[0 1 2 3 4]
[5 6 7 8 9]]
2. Matrix combination
Two test matrices are used here:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
2.1 Horizontal Combination
Example:
print np.hstack((a, b))
print np.concatenate((a, b), axis=1)
[[1 2 5 6]
[3 4 7 8]]
2.2 Vertical combination
Example:
print np.vstack((a, b))
print np.concatenate((a, b), axis=0)
[[1 2]
[3 4]
[5 6]
[7 8]]
2.3 Deep Combination
Example:
print np.dstack((a, b))
[[[1 5]
[2 6]]
[[3 7]
[4 8]]]
3. Matrix partition
Here is the test with the following data:
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
3.1 Horizontal split
After understanding the level combination in Section 2.1, its inverse process is easy to understand.
Example: Divide the example matrix into three equal parts
print np.hsplit(a, 3)
[array([[1],
[4],
[7]]), array([[2],
[5],
[8]]), array([[3],
[6],
[9]])]
3.2 Vertical division
The process is similar to the process above
Example :
print np.vsplit(a, 3)
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
3.3 Depth segmentation
Example: Note that depth segmentation is only valid for matrices with more than three dimensions
print np.dsplit(np.arange(27).reshape((3, 3, 3)), 3)
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
4. Properties of Matrix
The data used for testing here is still the data in Section 3
4.1 Matrix dimensions
print np.shape(a)
(3, 3)
4.2 Matrix data type
print a.dtype
int64
4.3 Number of Matrix Elements
print a.size
9
4.4 Number of bytes occupied by matrix elements
print a.itemsize
8
4.5 The total number of bytes occupied by the matrix
print a.nbytes
72