Added circle cards here, please go to today's headlines client view)
Effect np.arange (15) is constructed [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] or is similar to a list row vector, then this method can be used reshape be reconstructed, reshape (3,5) these 15 elements reconstructed 3 rows and five columns, the output is a matrix of three rows of five.
Shape attribute may output the number of rows and columns in the current matrix
ndim properties may output the current matrix dimensions
dtype attributes of the current element in the matrix type
The number of elements in the current matrix size
All zeros create matrix method 0:
Create an array of three rows and four columns, the parameters passed to the method zeros should be a tuple (3,4) represented by three rows four
Create a full matrix ones Method 1:
Create a 3-dimensional, (2,3,4) represent two three rows and four columns of the matrix is three-dimensional
When we do not specify a type, he is the default type float, we can specify its type:
Such matrix elements are all on the type int, behind all did a little bit of
Create a matrix using the sequence numbers:
np.arange means (0,30,50 is from 0 to 30, 30 does not contain, increments the sequence 5 configured in a matrix form
random module
random method
np.random to enter the random module, and then point to invoke random random function, the function generates a random number from -1 to 1, generates two rows and three columns.
linspace method
arr = np.linspace (0, pi, 50) which is an average number of 50 taken from between 0 to 3.14 ...
Matrix operations
If the shape of two matrices is the same, then the addition and subtraction is the corresponding positions of the two matrix addition and subtraction:
If a Save Save then each element corresponding to 1
arr1 ** 2 represents square of each element
arr1 <2 represents a determination of each element is less than 2, less than is true, false is not less than
Matrix multiplication
* Denotes matrix multiplication consisting of multiplying a position corresponding to the new matrix
arr.dot (arr2) equal np.dot (arr, arr2) is the real matrix multiplication, row by row
sqrt and exp methods
exp(x)为e的x次方,exp(arr)为e的矩阵元素次方
sqrt为开根号,sqrt(arr)为矩阵中的元素开根号
floor为向下取整
ravel方法为将一个矩阵展开变成行向量组的形式
reshape为重构,因为重构的时候行若却确定那么列也确定了,reshape(2,-1)就是指定行数为2,那么列数就不用我们自己算出为6,而是自己写上-1,计算机自己会算出来
print(arr.T)这个方法为输出当前矩阵的转秩,行变列,列变行
矩阵的拼接
hstack为行拼接,vstack为列拼接
矩阵的切分
hsplit为行切分,切分成两部分(其实行切分从真实情况看就是一把刀再竖着砍一刀)
还可以再指定位置切分np.hsplit(arr,(1,2))
这个意思是将arr矩阵从1切一下,从2切一下,这样就切成了三部分,三个矩阵
vsplit为列切分,实际上看就是从行的角度来切分,切成两部分,那就是平均分
这个就没有实现copy,arr和arr1指向同一内存,一个改变全部都变
arr.view实现的是浅copy,所谓浅copy是指两个不指向同一位置的内存,但是共用某些数据,比如说arr和arr1就是浅copy,arr和arr1的id不同,可见二者并没有指向同一块内存,但是当arr[1][2]=66的时候,发现arr和arr1的数据都变化了,这就是说明他们共用某些数据
copy可以实现arr1和arr完全指向不同的内存,并且没有共享数据
找出一个矩阵中每行或每列的最大值
一个矩阵可能有多维,如果arr.shape=(1,2,3),那么arr[0]=1,arr[1]=2,arr[2]=3,所以arr[1]就是4
arr.argmax (axis = 0) is to obtain the maximum value from the perspective of the column, the output [2031] For each column represents a maximum value at row 3, row 1, row 4, line 2, we can use this as the row index, they correspond a first column, second column, third column, the fourth column, the column index is rang (4), this is just the first column, second column, third column, the fourth column, so write range (arr.shape [1]) this is represented by 0-3, fixed number of lines
That arr [ind, range (4)] is expressed as:
arr[2][0]
arr[0][1]
arr[3][2]
arr[1][3]
Line is 2,0,3,1, 0,1,2,3 respectively correspond to column
Arr arr1 can be seen by a matrix composed of 4 rows and 3 columns
Within the matrix data sorting
The data can be sorted in a matrix of rows and columns Sort by:
This procedure has two sorting mode, a mode: it is packaged in accordance with the sort method for sorting the rows and columns sorting
Second way: Using the method returns argsort data matrix index coordinate small to large, the result is program [2,0,1,3] then using it as an index to acquire the data, the result is small to large data