In the process of learning machine learning is often used in the matrix, then using the NumPy expansion package will be the choice of two
It is recommended to replace matrices (matrix) with multidimensional Arrays (array) in flat python
Get started please test http://old.sebug.net/paper/books/scipydoc/numpy_intro.html#
Import NumPy NP
1. Read and write arrays, which can be seen as matrices
#返回值格式 (Score, trust table, number of users, number of items)
A = Np.arange (0,12,0.5). Reshape (4,-1)
Np.savetxt ("A.txt", a) # saves data by default in '%.18e ' format, separated by spaces
Np.loadtxt ("A.txt")
Np.loadtxt (' a.txt ', dtype= ' int ') #设置读出的数据类型
2. using the reshape method of the array, you can create a new array that changes the dimensions, and the shape of the original array remains the same:
A =np.arange (4). Reshape (2,2)
b = Np.arange (6). Reshape (2,3)
Print (' The result is\n ', a)
Print (' The result is\n ', b)
3.transpose () transpose an array
Print (' Thetranspose is\n ', B.transpose ()) #转置
4. matrix Operations
Multiplication of Np.dot () matrices
Print (' The result is\n ', Np.dot (A, b)) #矩阵乘
NP.LINALG.INV () to find the inverse of a matrix
Print (' The inverse is\n ', NP.LINALG.INV (a)) #逆矩阵
5. to find the size of the column
(m,n) = a.shape# to find the number of rows
6. Find the best value
Temp1 =np.max (a[:,0])
Temp2 = Np.min (a[:,0])
7. Ask for the number of the third column equal to 1
Np.sum (A[:,2]==1)
8. Find a set of random arrays
Randindices =np.random.permutation (4)
ans=array[[3,0,2,1]]
9. combination of two arrays
Np.vstack ((a)) #纵向结合, ensure the same number of columns Note double brackets
Np.hstack ((a)) #横向结合 to ensure the same number of rows
Not finished: Added in
Python numpy using notes-basic matrix operations