共计 5927 个字符,预计需要花费 15 分钟才能阅读完成。
Numpy 长期以来始终是 Python 开发人员进行数组操作的通用抉择,它是基于 C 语言构建的这使得它成为执行数组操作的疾速和牢靠的抉择,并且它曾经成为机器学习和数据迷信必备的根底库。
在本文中,我整顿了一些 NumPy 代码的片段,这些代码片段都是在日常开发中常常用到的。
1、创立数组
import numpy as np
new_array = np.array([1,2,3])
print(new_array)
# Output[1 2 3]
2、获取 Numpy 数组的形态、维度和大小
# shape
print(new_array.shape)
# dimensions
print(new_array.ndim)
# size
print(new_array.size)
# Output(3,)
1
3
3、查看 Numpy 数组中元素的类型
array = np.arange(0,10,1)
print(array.dtype)
# Output
int64
4、获取数组中每个元素的占用字节大小
array = np.array([1,2])
print(array.itemsize)
# Output
8
5、创立时指定数组的类型
array = np.array([[1,2], [3,4]], dtype=complex)
array
# Output
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
6、应用占位符创立数组
# 全零数组
array = np.zeros((3,4))
print(array)
print("---")
# 全 1 数组
array = np.ones((1,2))
print(array)
print("---")
# shape (2,3) 的空数组,随机产生数据
array = np.empty((2,3))
print(array)
print("---")
# Output
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
---
[[1. 1.]]
---
[[4.67280967e-310 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000]]
---
7、创立序列
# 应用 np.arange 创立一个从 0 到 42 个元素的序列,步长 1
array = np.arange(0,42,1)
print(array)
print("---")
# 应用 np.linspace 在 0 到 100 之间插入 42 个元素
array = np.linspace(0,100,42)
print(array)
print("---")
# Output
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41]
---
[ 0. 2.43902439 4.87804878 7.31707317 9.75609756
12.19512195 14.63414634 17.07317073 19.51219512 21.95121951
24.3902439 26.82926829 29.26829268 31.70731707 34.14634146
36.58536585 39.02439024 41.46341463 43.90243902 46.34146341
48.7804878 51.2195122 53.65853659 56.09756098 58.53658537
60.97560976 63.41463415 65.85365854 68.29268293 70.73170732
73.17073171 75.6097561 78.04878049 80.48780488 82.92682927
85.36585366 87.80487805 90.24390244 92.68292683 95.12195122
97.56097561 100. ]
---
8、Numpy 中的数学函数
import numpy as np
import matplotlib.pyplot as plt
# sine function
x = np.linspace(0,2*np.pi, 100)
f = np.sin(x)
plt.figure(figsize=(15,7))
plt.subplot(1,3,1)
plt.plot(f, color="green")
plt.title("np.sin(x)")
# cosine function
f = np.cos(x)
plt.subplot(1,3,2)
plt.plot(f, color="blue")
plt.title("np.cos(x)")
# tangent function
f = np.tan(x)
plt.subplot(1,3,3)
plt.plot(f, color="red")
plt.title("np.tan(x)")
plt.show()
9、通过在每个坐标上执行函数来创立数组
some_function = lambda x: np.cos(x)+1
array = np.fromfunction(some_function, (100,))
plt.figure(figsize=(15,7))
plt.plot(array, color="green")
plt.title("np.cos(x) +1")
plt.show()
10、遍历 Numpy 数组的所有元素
a = np.arange(0,23,1)
for i in a.flat:
print(i)
# Output
0
1
2
...
22
11、获取浮点数的上限
np.floor(10.5)
10.0
12、应用.ravel() 压扁数组
array = np.full(shape=(5,5),fill_value=10)
print(array)
print("---")
print("Flattened array:")
print(array.ravel())
# Output
[[10 10 10 10 10]
[10 10 10 10 10]
[10 10 10 10 10]
[10 10 10 10 10]
[10 10 10 10 10]]
---
Flattened array:
[10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
10]
13、获取一个数组的转置
array = np.random.random((2,5))
print(array)
print(array.T)
[[0.18735704 0.22800582 0.02552177 0.93552346 0.20720663]
[0.74303284 0.1897481 0.91389602 0.23099501 0.07565492]]
[[0.18735704 0.74303284]
[0.22800582 0.1897481]
[0.02552177 0.91389602]
[0.93552346 0.23099501]
[0.20720663 0.07565492]]
14、应用. shape() 和.resize() 进行重塑
a = np.random.randint(100,size=(3,4))
print(a)
a_reshaped = np.reshape(a, (1,12))
print(a_reshaped)
# 应用.resize() 办法
a.resize((1,12))
# Output
[[29 18 39 24]
[53 45 49 8]
[90 75 61 61]]
[[29 18 39 24 53 45 49 8 90 75 61 61]]
15、将数组沿不同轴进行重叠
a = np.random.random((2,2))
print(a)
b = np.random.random((2,2))
print(b)
# 沿垂直轴重叠 (取得更多行)
print(np.vstack((a,b)))
print(np.vstack((a,b)).shape)
# 沿程度轴重叠 (取得更多列)
print(np.hstack((a,b)))
print(np.hstack((a,b)).shape)
# column_stack 列叠加
print(np.column_stack((a,b)))
# Output
[[0.67028492 0.86322792]
[0.38906266 0.36967583]]
[[0.51419553 0.21937852]
[0.50375453 0.31634597]]
[[0.67028492 0.86322792]
[0.38906266 0.36967583]
[0.51419553 0.21937852]
[0.50375453 0.31634597]]
(4, 2)
[[0.67028492 0.86322792 0.51419553 0.21937852]
[0.38906266 0.36967583 0.50375453 0.31634597]]
(2, 4)
[[0.67028492 0.86322792 0.51419553 0.21937852]
[0.38906266 0.36967583 0.50375453 0.31634597]]
16、将一个数组拆分为几个较小的数组
应用 hsplit,通过指定要返回的雷同 shape 的 array 的数量,或者通过指定宰割应该产生之后的列来沿着其横轴拆分原 array。vsplit 沿着垂直轴宰割,其宰割形式与 hsplit 用法雷同
# 沿着程度轴将数组拆分为 5 个较小的数组
a = np.arange(0, 5, 1)
print("Horizontal split")
print(np.hsplit(a, 5))
print("---")
# 沿着垂直轴将数组拆分成 5 个更小的数组
a = np.random.random((5,5))
print("Vertical split")
print(np.vsplit(a, 5))
Horizontal split
[array([0]), array([1]), array([2]), array([3]), array([4])]
---
Vertical split
[array([[0.69059321, 0.55703093, 0.20019592, 0.19697317, 0.37278251]]), array([[0.24597633, 0.87216661, 0.634432 , 0.35326185, 0.03130537]]), array([[0.18063077, 0.45045441, 0.06882852, 0.91273837, 0.07332161]]), array([[0.61738939, 0.11291748, 0.73152623, 0.49177006, 0.95750985]]), array([[0.90212777, 0.53825846, 0.86733505, 0.76165564, 0.17337721]])]
17、数组的浅拷贝
.view() 办法创立了一个与原数组对象雷同的对象,它创立了该数组的浅拷贝,浅拷贝只复制指向某个对象的指针,而不复制对象数据,新旧对象还是共享同一块内存。所以如果其中一个对象扭转了内存的数值,就会影响到另一个对象,也就是说一个对象的数值扭转了,其余的也会扭转(应用雷同的内存)。
a = np.array([[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14]
])
array_object = np.arange(0,10,1)
shallow_copy_object = array_object.view() # shallow copy
print("Array")
print(array_object)
print(f"Id = {id(array_object)}")
print("---")
print("Shallow Copy")
print(shallow_copy_object)
print(f"Id = {id(shallow_copy_object)}")
print("---")
shallow_copy_object[0] = 200
print("After assigment: shallow_copy_object[0] = 200")
print("Array")
print(array_object)
print("Shallow copy")
print(shallow_copy_object)
# Output
Array
[0 1 2 3 4 5 6 7 8 9]
Id = 139980496768528
---
Shallow Copy
[0 1 2 3 4 5 6 7 8 9]
Id = 139980496768720
---
After assigment: shallow_copy_object[0] = 200
Array
[200 1 2 3 4 5 6 7 8 9]
Shallow copy
[200 1 2 3 4 5 6 7 8 9]
18、数组的深拷贝
copy 办法复制对象及其数据的残缺正本。齐全拷贝了一个正本,外部元素地址都不一样,数值的扭转不会相互影响。
array_object = np.arange(0, 23, 1)
deep_copy_object = array_object.copy()
print(deep_copy_object is array_object)
print("Array")
print(array_object)
print(f"Array id = {id(array_object)}")
print("---")
print("Deep Copy")
print(deep_copy_object)
print(f"Deep copy id = {id(deep_copy_object)}")
print("---")
deep_copy_object[0] = 234
print("After assignment: deep_copy_object[0] = 234")
print("Array")
print(array_object)
print("Deep copy")
print(deep_copy_object)
False
Array
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]
Array id = 139980498767472
---
Deep Copy
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]
Deep copy id = 139980496039824
---
After assignment: deep_copy_object[0] = 234
Array
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]
Deep copy
[234 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22]
作者:Lucas Soares