当前位置:网站首页>Numpy基本使用
Numpy基本使用
2022-06-26 13:57:00 【m0_52339560】
Numpy入门
对于Python的列表,其并不是真正意义上的数组。数据量很大时,列表效率不高(即速度很慢)。
Numpy提供两种基本对象,ndarray和ufunc。
ndarray(后面统称为数组),存储单一数据类型的多维数组。
ufunc(通用函数),对数组进行处理的函数
1. 数组的创建
array():向该函数传入一个列表或元组,即可构造简单一位数组,如果传入嵌套列表或嵌套元组,则构造二维数组。
a = np.array([2, 4, 6, 8, 10]) #生成一维数组
b = np.array([1, 2, 3], [2, 3, 4]) #生成二维数组
arange(),empty(),linspace()
- empty():创建一个指定形状(shape),数据类型(dtype)且未初始化的数组。
#函数原型 #numpy.empty(shape, dtype=float, order='C') a = np.empty((2,3), int) #创建2行,3列的空矩阵
- arange():返回给定间隔均匀分布的值构成的数组。
#函数原型,有省略一些不常用的参数 #numpy.arange([start, ]stop, [step, ]dtype=None) a = np.arange(0, 10, 4, dtype=int) #生成[0,4,8] #起始值为0,步长为4,取值区间为[start, stop),注意“左闭右开” #start可省略,默认起始值为0,默认步长为1
- linspace():返回指定间隔内间隔均匀的数字。
#函数原型,省略一些不常用的参数 #numpy.linspace(start, stop, num=50) a = np.linspace(0, 100, 20) #从区间[start, stop]中均匀取20个点,构成一个数组,这里为闭区间其他生成数组的方法省略。
2. 数组的属性
使用numpy生成的数组都是一个数组对象。该对象有一些基本属性。
ndim:数组的维数(int)
shape:数组的尺寸,对于m行n列矩阵,返回为(m,n)元组
size: 数组元素总个数
dtype: 数组中元素的数据类型
itemsize:数组中元素的字节数
使用示例:
a = np.array([2, 3, 4, 5])
print("维数:", a.ndim) # 维数:1
shape属性
使用示例:
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3]])
c = np.array([[1], [2], [3]])
print("维度为:", a.shape) #维度为:(3,)
print("维度为:", b.shape) #维度为:(1,3)
print("维度为:", c.shape) #维度为:(3,1)
注意shape属性(3,)、(1,3)、(3,1)是不一样的,(3,)代表的一维数组即可以看成是行向量,也可以看成是列向量,它的转置是不变的。
3. 数组元素的访问(索引方式)
- 一般索引
使用示例:
#一维数组的索引方式
a = np.arange(0,10)
print(a[7]) #访问下标为7的元素,结果为7
#二维数组的索引方式
a = np.array([[1, 2, 3], [ 5, 6 ,7]])
print(a[1,2]) #访问第2行,第3列的元素,结果为7
- 布尔索引
使用示例:布尔索引会将满足结果的元素合并为一个一维数组
a = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
print(a[a>3]) #提取出数组a中大于3的元素
#结果为:[4 4 5]
- 花式索引:略
4. 数组的修改
- 通过索引访问修改
使用示例:
a = np.arange(0,10)
a[7] = 0 #修改下标为7的元素值为0
#二维数组同理
- 通过函数修改
使用示例:
x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.delete(x, 2, axis=0) #删除数组的第3行
z = np.delete(x, 0, axis=1) #删除数组的第1列
t1 = np.append(x, [[7,8]], axis=0) #增加一行
t2 = np.append(y, [[9], [2], [1]], axis=1) #增加一列
5.数组的变形
数组的变形和转换有很多方法,这里简单介绍reshape和resize。
a = np.arange(4).reshape(2, 2) #生成数组[[0,1], [2, 3]]
b = np.arange(4).reshape(2, 2) #生成数组[[0,1], [2, 3]]
print(a.reshape(4,), '\n', a) #输出:[0 1 2 3]和[[0,1], [2,3]]
print(b.resize(4,), '\n', b) #输出:None和[0 1 2 3]
reshape()会返回改变形状后的数组,但是原来的数组不受影响,resize()不会返回,直接对原数组进行修改。
参考资料
Python数学实验与建模(司守奎,孙玺菁)
边栏推荐
- 工作上对金额价格类小数点的总结以及坑
- VMware partial settings
- Practice with the topic of bit operation force deduction
- 印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网
- Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)
- NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
- 《三体》摘录
- Caelus - full scene offline mixed Department solution
- Sword finger offer 06.24.35 Linked list
- Jianzhi offer 43.47.46.48 dynamic planning (medium)
猜你喜欢

The annual salary of 500000 is one line, and the annual salary of 1million is another line

Login authentication service

Sword finger offer 18.22.25.52 Double pointer (simple)

JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了

印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网

Common controls and custom controls

C language | file operation and error prone points

Freefilesync folder comparison and synchronization software

服务器创建虚拟环境跑代码

这才是优美的文件系统挂载方式,亲测有效
随机推荐
Electron
启动Redis报错:Could not create Server TCP listening socket *:6379: bind: Address already in use–解决办法
GDAL grid data types and their type codes
一个快速切换一个底层实现的思路分享
扩展-Hooks
印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网
Mathematical modeling of war preparation 30 regression analysis 2
C language | the difference between heap and stack
Transformers datacollatorwithpadding class
聊聊 RPA 方向的规划:简单有价值的事情长期坚持做
MHA高可用配合及故障切换
量化框架backtrader之一文读懂observer观测器
Win10 home vs pro vs enterprise vs enterprise LTSC
在云服务器中云磁盘如何挂载
From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity
Atcoder 238
What is the ranking of Guosen Securities? Is it safe to open a stock account?
JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
vmware部分设置
年薪50万是一条线,年薪100万又是一条线…...