当前位置:网站首页>Numpy Foundation
Numpy Foundation
2022-07-29 05:13:00 【m0_ sixty-five million one hundred and eighty-seven thousand fo】
NumPy(Numerical Python) yes Python An extended library of language , Support a large number of dimension arrays and matrix operations , In addition, it also provides a large number of mathematical function libraries for array operation , Mainly used for array calculation .
NumPy install : stay cmd The input terminal pip install numpy. In the use of Numpy Before , adopt import The order will numpy Library import , And named it np, for example :import numpy as np.
Catalog
Arrays and scalars ( The set of real Numbers ) Operation between
Operations between arrays of the same dimension
Operations between arrays of different dimensions
Create an array from a list
One dimensional array
We can first define a list , And then through np.array() Function to convert data types and define a one-dimensional array .
data=[6,7.5,8,0,1] # Create a list of
arr=np.array(data) # Convert to a one-dimensional array Two dimensional array
First define a list of nested list elements , And then through np.array() Function to perform data type conversion and define a two-dimensional array .
data=[[1,2,3,4],[5,6,7,8]] # Create nested sequences
arr=np.array(data) # Convert to 2D array Create an array by function
utilize numpy The functions provided are convenient to create some specific arrays , The details are as follows :
function | meaning |
np.zeros() | Create specified dimension full 0 Multidimensional arrays . |
np.ones() | Create specified dimension full 1 Multidimensional arrays . |
np.eye() | Create the identity matrix of the specified dimension . |
np.arange() | Creates a specified number of sequential arrays ( Default 0 Start ). |
linspace() | Create a one-dimensional array with a specified range , And divided into several equal parts . |
reshape() | Change the dimension of the array |
Operation of array
Arrays and scalars ( The set of real Numbers ) Operation between
arr=np.array([[1,2,3],[4,5,6]]) # Create a 2D array
arr+2 | arr-2 | arr*2 | arr/2 |
array([[3,4,5], [6,7,8]]) | array([[-1,0,1], [2,3,4]]) | array([[2,4,6], [8,10,12]]) | array([[0.5,1.,1.5], [2.,2.5,3.]]) |
Operations between arrays of the same dimension
arr1=np.array([[1,2,3],[4,5,6]])
arr2=np.array([[2,2,2],[2,2,2]])
arr1+arr2 | arr1-arr2 | arr1*arr2 | arr1/arr2 |
array([[3,4,5], [6,7,8]]) | array([[-1,0,1], [2,3,4]]) | array([[2,4,6], [8,10,12]]) | array([[0.5,1.,1.5], [2.,2.5,3.]]) |
Operations between arrays of different dimensions
When calculating arrays of different dimensions , Arrays have broadcast properties , That is, automatically expand the dimension .

Index of array
contain n The index of the array of elements is 0,1,…,n-1.
Axis
stay numpy Can be understood as direction , Use 0,1,2... Digital representation , For a one-dimensional array , only one 0 Axis , about 2 Dimension group (shape(2,2)), Yes 0 Axis and 1 Axis , For three-dimensional arrays (shape(2,2, 3)), Yes 0,1,2 Axis .
One dimensional array index

Two dimensional array index

Boolean index

Ternary operator
Ternary operator np.where( expression , Count 1, Count 2), When the expression is true , The result is a number 1, Otherwise, the result is a number 2.
function
The generic function
A general function is a function of array A function that performs element-level operations on the data in . You can think of it as a vectorization wrapper for simple functions .
One variable function
function | explain |
Abs、fabs | Calculate the whole number 、 The absolute value of a floating-point number or negative number , For non complex values , You can use faster fabs |
sqrt | Calculate the square root of each element , amount to arr**0.5 |
Square | Calculate the square of each element . amount to arr **2 |
exp | Calculate the index of each element e* |
log、log10、log2、log1p | They are natural logarithm ( The base number is e)、 The base number is 10 Of log、 The base number is 2 Of log、log(1+x) |
sign | Calculate the sign of each element :1( Positive numbers )、o( zero )、 One 1( negative ) |
Cell | Calculate the ceiling value , That is, the smallest integer greater than or equal to the value |
floor | Calculate the floor value , That is, the maximum integer less than or equal to this value |
Dual function
function | explain |
add | Add the corresponding elements in the array |
subtract | Subtracts the elements of the second array from the first array |
multiply | Array element multiplication |
divide、floor_dvide | Division or downward rounding Division ( Discard the remainder ) |
power | For the elements in the first array A, According to the corresponding element in the second array B, meter . count AB |
maximum、 fmax | Calculation of the maximum value at the element level .fmax Will ignore NaN |
minimum、fmin | Calculation of minimum value at element level .fmin Will ignore NaN |
mod | Modular computation at the element level ( The remainder of division ) |
copysign | Assign the value in the second array to the value in the first array |
边栏推荐
- Double type nullpointexception in Flink flow calculation
- Cache penetration, cache breakdown, cache avalanche and Solutions
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
- Solve the warning prompt of MySQL mapping file
- 一文带你搞懂环绕通知@Around与最终通知@After的实现
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- What if the computer cannot open excel? The solution of Excel not opening
- Unity metaverse (III), protobuf & socket realize multi person online
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- P1009 [noip1998 popularization group] sum of factorials
猜你喜欢

Introduction of JDBC preparestatement+ database connection pool
![[2022 freshmen learning] key points of the third week](/img/21/6a2c4f7e9114260797497d99b75cd6.png)
[2022 freshmen learning] key points of the third week

Big silent event Google browser has no title

Functions in MySQL statements

SparkSql批量插入或更新,保存数据到Mysql中

Office提示系统配置无法运行怎么办?

Northeast University Data Science Foundation (matlab) - Notes

【文件下载】Easyexcel快速上手

How to solve the problem of configuring the progress every time Office2010 is opened?

How to add a map to the legendary server
随机推荐
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
Let you understand several common traffic exposure schemes in kubernetes cluster
scikit-learn——机器学习应用开发的步骤和理解
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Force deduction ----- sort odd and even subscripts respectively
MySQL time calculation function
Google GTEST event mechanism
Northeast University Data Science Foundation (matlab) - Notes
Live in small private enterprises
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
Solve the warning prompt of MySQL mapping file
【config】配置数组参数
Double type nullpointexception in Flink flow calculation
Big silent event Google browser has no title
Apache POI implements excel import, read data, write data and export
Soft link & hard link
Understand activity workflow
AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
Cache penetration, cache breakdown, cache avalanche and Solutions
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me