您现在的位置是:网站首页> 编程资料编程资料
Python中11种NumPy高级操作总结_python_
2023-05-26
466人已围观
简介 Python中11种NumPy高级操作总结_python_
1.数组上的迭代
NumPy 包含一个迭代器对象numpy.nditer。它是一个有效的多维迭代器对象,可以用于在数组上进行迭代。数组的每个元素可使用 Python 的标准Iterator接口来访问。
import numpy as np a = np.arange(0, 60, 5) a = a.reshape(3, 4) print(a) for x in np.nditer(a): print(x)
输出结果:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
0
5
10
15
20
25
30
35
40
45
50
55
如果两个数组是可广播的,nditer组合对象能够同时迭代它们。假设数 组a具有维度 3X4,并且存在维度为 1X4 的另一个数组b,则使用以下类型的迭代器(数组b被广播到a的大小)。
import numpy as np a = np.arange(0, 60, 5) a = a.reshape(3, 4) print(a) b = np.array([1, 2, 3, 4], dtype=int) print(b) for x, y in np.nditer([a, b]): print(x, y)
输出结果:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
[1 2 3 4]
0 1
5 2
10 3
15 4
20 1
25 2
30 3
35 4
40 1
45 2
50 3
55 4
2.数组形状修改函数
1.ndarray.reshape
函数在不改变数据的条件下修改形状,参数如下:
ndarray.reshape(arr, newshape, order)
import numpy as np a = np.arange(8) print(a) b = a.reshape(4, 2) print(b)
输出结果:
[0 1 2 3 4 5 6 7]
[[0 1]
[2 3]
[4 5]
[6 7]]
2.ndarray.flat
函数返回数组上的一维迭代器,行为类似 Python 内建的迭代器。
import numpy as np a = np.arange(0, 16, 2).reshape(2, 4) print(a) # 返回展开数组中的下标的对应元素 print(list(a.flat))
输出结果:
[[ 0 2 4 6]
[ 8 10 12 14]]
[0, 2, 4, 6, 8, 10, 12, 14]
3.ndarray.flatten
函数返回折叠为一维的数组副本,函数接受下列参数:
ndarray.flatten(order)
其中:
order:‘C’ — 按行,‘F’ — 按列,‘A’ — 原顺序,‘k’ —元素在内存中的出现顺序。
import numpy as np a = np.arange(8).reshape(2, 4) print(a) # default is column-major print(a.flatten()) print(a.flatten(order='F'))
输出结果:
[[0 1 2 3]
[4 5 6 7]]
[0 1 2 3 4 5 6 7]
[0 4 1 5 2 6 3 7]
3.数组翻转操作函数
1.numpy.transpose
函数翻转给定数组的维度。如果可能的话它会返回一个视图。函数接受下列参数:
numpy.transpose(arr, axes)
其中:
arr:要转置的数组
axes:整数的列表,对应维度,通常所有维度都会翻转。
import numpy as np a = np.arange(24).reshape(2, 3, 4) print(a) b = np.array(np.transpose(a)) print(b) print(b.shape)
输出结果:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[[[ 0 12]
[ 4 16]
[ 8 20]]
[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]]
(4, 3, 2)
b = np.array(np.transpose(a, (1, 0, 2))) print(b) print(b.shape
输出结果:
[[[ 0 1 2 3]
[12 13 14 15]]
[[ 4 5 6 7]
[16 17 18 19]]
[[ 8 9 10 11]
[20 21 22 23]]]
(3, 2, 4)
2. numpy.ndarray.T
该函数属于ndarray类,行为类似于numpy.transpose.
import numpy as np a = np.arange(12).reshape(3, 4) print(a) print(a.T)
输出结果:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
3.numpy.swapaxes
函数交换数组的两个轴。这个函数接受下列参数:
numpy.swapaxes(arr, axis1, axis2)
其中:
arr:要交换其轴的输入数组
axis1:对应第一个轴的整数
axis2:对应第二个轴的整数
import numpy as np a = np.arange(8).reshape(2, 2, 2) print(a) print(np.swapaxes(a, 2, 0))
输出结果:
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
[[[0 4]
[2 6]]
[[1 5]
[3 7]]]
4.numpy.rollaxis
numpy.rollaxis() 函数向后滚动特定的轴,直到一个特定位置。这个函数接受三个参数:
numpy.rollaxis(arr, axis, start)
其中:
arr:输入数组
axis:要向后滚动的轴,其它轴的相对位置不会改变
start:默认为零,表示完整的滚动。会滚动到特定位置。
import numpy as np a = np.arange(8).reshape(2,2,2) print(a) print(np.rollaxis(a,2)) print(np.rollaxis(a,2,1))
输出结果:
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
[[[0 2]
[4 6]]
[[1 3]
[5 7]]]
[[[0 2]
[1 3]]
[[4 6]
[5 7]]]
4.数组修改维度函数
1.numpy.broadcast_to
函数将数组广播到新形状。它在原始数组上返回只 读视图。它通常不连续。如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError。该函数接受以下参数:
numpy.broadcast_to(array, shape, subok)
import numpy as np a = np.arange(4).reshape(1,4) print(a) print(np.broadcast_to(a,(4,4)))
输出结果:
[[0 1 2 3]]
[[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]
2.numpy.expand_dims
函数通过在指定位置插入新的轴来扩展数组形状。该函数需要两个参数:
numpy.expand_dims(arr, axis)
其中:
arr:输入数组
axis:新轴插入的位置
import numpy as np x = np.array(([1, 2], [3, 4])) print(x) y = np.expand_dims(x, axis=0) print(y) print(x.shape, y.shape) y = np.expand_dims(x, axis=1) print(y) print(x.ndim, y.ndim) print(x.shape, y.shape)
输出结果:
[[1 2]
[3 4]]
[[[1 2]
[3 4]]]
(2, 2) (1, 2, 2)
[[[1 2]]
[[3 4]]]
2 3
(2, 2) (2, 1, 2)
3.numpy.squeeze
函数从给定数组的形状中删除一维条目。此函数需要两 个参数。
numpy.squeeze(arr, axis)
其中:
arr:输入数组
axis:整数或整数元组,用于选择形状中单一维度条目的子集
import numpy as np x = np.arange(9).reshape(1, 3, 3) print(x) y = np.squeeze(x) print(y) print(x.shape, y.shape)
输出结果:
[[[0 1 2]
[3 4 5]
[6 7 8]]]
[[0 1 2]
[3 4 5]
[6 7 8]]
(1, 3, 3) (3, 3)
5.数组的连接操作
NumPy中数组的连接函数主要有如下四个:
concatenate沿着现存的轴连接数据序列stack沿着新轴连接数组序列hstack水平堆叠序列中的数组(列方向)vstack竖直堆叠序列中的数组(行方向)
1.numpy.stack
函数沿新轴连接数组序列,需要提供以下参数:
numpy.stack(arrays, axis)
其中:
arrays:相同形状的数组序列axis:返回数组中的轴,输入数组沿着它来堆叠
import numpy as np a = np.array([[1,2],[3,4]]) print(a) b = np.array([[5,6],[7,8]]) print(b) print(np.stack((a,b),0)) print(np.stack((a,b),1))
输出结果:
[[1 2]
[3 4]]
[[5 6]
[7 8]]
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
相关内容
- pandas创建series的三种方法小结_python_
- pandas选择或添加列生成新的DataFrame操作示例_python_
- 对比分析BN和dropout在预测和训练时区别_python_
- 使用pyscript在网页中撰写Python程式的方法_python_
- python使用open函数对文件进行处理详解_python_
- Python+Plotly绘制精美的数据分析图_python_
- 基于Python实现一键找出磁盘里所有猫照_python_
- 如何使用Python一键修改上万个文件名_python_
- python目标检测非极大抑制NMS与Soft-NMS_python_
- 关于python中range()的参数问题_python_
