当前位置:网站首页>NumPy基础
NumPy基础
2022-07-29 05:07:00 【m0_65187443】
NumPy(Numerical Python) 是Python语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,主要用于数组计算。
NumPy安装:在cmd终端输入pip install numpy。在使用Numpy之前,通过import命令将numpy库导入,并命名为np,例如:import numpy as np。
目录
由列表创建数组
一维数组
我们可以首先定义一个列表,然后再通过np.array()函数进行数据类型转换定义一维数组。
data=[6,7.5,8,0,1] # 创建列表
arr=np.array(data) # 转换为一维数组 二维数组
首先定义一个嵌套列表元素的列表,然后在通过np.array()函数进行数据类型转换定义二维数组。
data=[[1,2,3,4],[5,6,7,8]] # 创建嵌套序列
arr=np.array(data) # 转换为二维数组由函数创建数组
利用numpy提供的函数方便的创建一些特定的数组,具体内容如下:
函数 | 含义 |
np.zeros() | 创建指定维度全0多维数组。 |
np.ones() | 创建指定维度全1多维数组。 |
np.eye() | 创建指定维度的单位矩阵。 |
np.arange() | 创建指定数量的顺序数组(默认0开始)。 |
linspace() | 创建指定范围的一维数组,并分成若干等份。 |
reshape() | 更改数组的维度 |
数组的运算
数组和标量(实数)之间的运算
arr=np.array([[1,2,3],[4,5,6]]) #创建二维数组
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.]]) |
同维度数组之间的运算
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.]]) |
不同维度数组之间的运算
不同维度的数组进行运算时,数组具有广播特性,即自动将维度进行扩展。

数组的索引
含有n个元素数组的索引为0,1,…,n-1。
轴
在numpy中可以理解为方向,使用0,1,2...数字表示,对于一个一维数组,只有一个0轴,对于2维数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(2,2, 3)),有0,1,2轴。
一维数组索引

二维数组索引

布尔索引

三元运算符
三元运算符np.where(表达式,数1,数2),当表达式为真,结果为数1,否则结果为数2。
函数
通用函数
通用函数是一种对array中的数据执行元素级运算的函数。你可以将其看做简单函数的矢量化包装器。
一元函数
函数 | 说明 |
Abs、fabs | 计算整数、浮点数或负数的绝对值,对于非复数值,可以使用更快的fabs |
sqrt | 计算各元素的平方根,相当于arr**0.5 |
Square | 计算各元素的平方。相当于arr **2 |
exp | 计算各元素的指数e* |
log、log10、log2、log1p | 分别为自然对数(底数为e)、底数为10的log、底数为2的log、log(1+x) |
sign | 计算各元素的正负号:1(正数)、o(零)、一1(负数) |
Cell | 计算各元素的ceiling值,即大于等于该值的最小整数 |
floor | 计算各元素的floor值,即小于等于该值的最大整数 |
二元函数
函数 | 说明 |
add | 将数组中对应的元素相加 |
subtract | 从第一个数组中减去第二个数组中的元素 |
multiply | 数组元素相乘 |
divide、floor_dvide | 除法或向下圆整除法(丢弃余数) |
power | 对第一个数组中的元素A,根据第二个数组中的相应元素B,计.算AB |
maximum、 fmax | 元素级的最大值计算。fmax将忽略NaN |
minimum、fmin | 元素级的最小值计算。fmin将忽略NaN |
mod | 元素级的求模计算(除法的余数) |
copysign | 将第二个数组中的值的赋值给第一个数组中的值 |
边栏推荐
- How to solve the problem of configuring the progress every time Office2010 is opened?
- WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
- Five correlation analysis, one of the most important skills of data analysts
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- 关于servlet中实现网站的页面跳转
- 输入的查询SQL语句,是如何执行的?
- TCP three handshakes and four waves
- sql日志
- [config] configure array parameters
- Pytorch learning notes
猜你喜欢

传奇开区网站如何添加流量统计代码

输入的查询SQL语句,是如何执行的?

WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!

Activity workflow table structure learning

Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
![[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]](/img/ee/b424d876c64dac652d76f9f26e4b20.png)
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]

如何安装office2010安装包?office2010安装包安装到电脑上的方法

Quick start JDBC

Pytorch learning notes

pytorch学习笔记
随机推荐
TCP三次握手四次挥手
The representation of time series analysis: is the era of learning coming?
【2022新生学习】第三周要点
Understand activity workflow
Unity metaverse (III), protobuf & socket realize multi person online
2021-10-23
How to add traffic statistics codes to the legendary Development Zone website
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
Mysql把查询到的结果集按指定顺寻进行排序
On prepayment of house purchase
Network Security Learning - Intranet Security 1
How does WPS take quick screenshots? WPS quick screenshot method
How to solve the problem of configuring the progress every time Office2010 is opened?
传奇服务端如何添加地图
Flink+iceberg environment construction and production problem handling
开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
JS daily question (11)
Pytorch learning notes
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!