程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Numpy, the three libraries of Python

編輯:Python

1. Import defaults to import numpy as np

Create a matrix

Use np.array() In parentheses are information about the matrix

The simplest example :

ay=np.array([[1,2,3],
[4,5,6]])

This is a two-dimensional matrix , amount to c Two dimensional array in language , Generally speaking , Count the number of brackets from the outside to the inside , For example, if you count from the left, the number is the number of left square brackets , From the first , End with the first open square bracket next to the number , There are several dimensions , stay numpy In the document , You count a few left square brackets to represent several axes , Such as counting two , Namely x and y Axis , One more Namely xyz Three axes , I don't know what axis it is , Just know that the dimension has risen .

And array Check the matching items array Function of the property of

array.ndim// n Represents a number ,dim yes dimension( dimension ) Abbreviation , return int Represents dimension
array.shape// Returns a logarithm , Representative * A matrix of several
array.size// There are several elements in the calculation matrix , return int

Advanced operation :

1. Set content type , stay 【】 Then add '.dtype',d yes data Abbreviation ,dtype= data type

There are mainly int32,int64,float32,float64

array=np.array([1,2,3],dtpe=np.int32)
array=np.array([1,2,3],dtpe=np.int64)
array=np.array([1,2,3],dtpe=np.float32)
array=np.array([1,2,3],dtpe=np.float64)

2. Create a feature matrix

np.zeros(( Row number , Number of columns ))// Tell the computer , You want a matrix of rows and columns
np.ones(( Row number , Number of columns ))// Tell the computer , You want a matrix of rows and columns
np.empty(( Row number , Number of columns ))// Tell the computer , You want a matrix of rows and columns This matrix should generate infinitesimal 

3 Create an ordered matrix

np.arange( The starting point , End , step )
np.arange( The starting point , End , step ).reshape(( Row number , Number of columns ))// After the delegate has created the matrix, I want to re plan his shape

4. Generating line segments

 

np.linspace( The starting point , End , Number of segments )// From start to end , To generate several line segments .
np.linspace( The starting point , End , Number of segments ).reshape(( Row number , Number of columns ))

Basic operation of matrix

1. arithmetic

// Add
a=np.array([1,2,3,4])
b=np.range(4)// The content is from 0-3 One dimensional matrix of
c=a+b
print(c)// Still get the matrix , nothing but a Add to each number of b Each of
// Subtraction
a=np.array([1,2,3,4])
b=np.range(4)// The content is from 0-3 One dimensional matrix of
c=a-b
print(c)// Still get the matrix , nothing but a Subtract from each number of b Each of
// Multiplication
a=np.array([1,2,3,4])
b=np.range(4)// The content is from 0-3 One dimensional matrix of
c=a*b
print(c)// Still get the matrix , nothing but a Each number of * Go to b Each of
// Rounding Division
a=np.array([1,2,3,4])
b=np.range(4)// The content is from 0-3 One dimensional matrix of
c=a/b
print(c)// Still get the matrix , nothing but a Each number of / Go to b Round each number in 

2. Characteristic operation

// Compare the size , Returns a Boolean type
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=a<b
print(c)
// The result is
[ True True True True]
// Multiplication for multidimensional matrices
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=np.dot(a,b) or c=a.dot(b)
// Multiplication of matrices used in linear algebra , The result is 70
// square
a=3;
a**2==9// To the power of **n Express
// Trigonometric functions
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=np.sin(a)// Yes a Find each number in sin You can also use other trigonometric functions
print(c)
// Find the sum of matrices , Maximum , minimum value
a=np.random.random((2,2))// Generate a with random numbers 2 That's ok 2 Columns of the matrix
print(a)
[[0.34700893 0.85088823]
[0.95787822 0.248027 ]]
//axis Represents the axis ,0 by y Axis ,1 by x Axis
// The first is along y Axis summation , The third is along y Find the maximum value of the axis , The fifth is along y Find the minimum value of the axis
print(np.sum(a,axis=0)) //[1.30488715 1.09891523]
print(np.sum(a,axis=1)) //[1.19789715 1.20590522]
print(np.max(a,axis=0))//[0.95787822 0.85088823]
print(np.max(a,axis=1))//[0.85088823 0.95787822]
print(np.min(a,axis=0))//[0.34700893 0.248027 ]
print(np.min(a,axis=1))//[0.34700893 0.248027 ]

3. Find the index of a characteristic value

a=np.arange(2,14).reshape((3,4))
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
// Find the index of the maximum and minimum value
print(np.argmax(a)) //11 Note the index Not the number !!!!!
print(np.argmin(a))//0
// averaging
print(a.mean())// Notice the parentheses The answer for 7.5
print(np.average(a))// The answer for 7.5
// Find the median
print(np.median(a)) //7.5
// Accumulate and save : Accumulate once and save once
print(np.cumsum(a))//[ 2 5 9 14 20 27 35 44 54 65 77 90]
// Accumulate and save : Cumulative difference is the difference between two adjacent numbers , It's a little unreasonable to keep subtracting
print(np.diff(a))//[[1 1 1]
[1 1 1]
[1 1 1]] The dimension is consistent with the original matrix
// Sort
printf(np.sort(a))// Because it was ordered , So it's the same as the original matrix
// The transpose of the matrix
np.transpose(a)
// The original a yes
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
// The transpose is followed by
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
// Pruning of matrix
print(np.clip(a,5,9)) // Let less than 5 The number of is equal to 5, Greater than 9 The number of is equal to 9, So you can keep 5-9 The number between
[[5 5 5 5]
[6 7 8 9]
[9 9 9 9]]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved