Python numpy using notes-basic matrix operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.