目录
1 简介
1.1 什么是numpy
多维数组,可以用来储存各种数据。
1.2 列表(list)和数组(numpy)的区别
(1) List慢,Numpy快
(2) Numpy方法多
(3)
List:
a = [1, 2, 3]
b = [1, 2, 3]
a * b = ERROR
Numpy:
a = np.array([1, 3, 5])
b = np.array([1, 2, 3])
a * b = np.array([1, 6, 15])
2 代码
2.1 基础
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[9.0, 8.0, 7.0], [6.0, 5.0, 4.0]])
print(a, b)
# 得到维度
print(a.ndim, b.ndim)
# 得到shape
print(a.shape, b.shape)
# 得到类型
a_new = np.array([1, 2, 3], dtype='int16')
print(a.dtype, b.dtype, a_new.dtype)
# 得到内存大小
print(a.itemsize, b.itemsize)
print(a.size, b.size)
# 字节数:a.nbytes = a.size * a.itemsize
print(a.nbytes, b.nbytes)
2.2 查找和修改
import numpy as np
a = np.array([[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]])
# 得到特定元素[行,列]
print(a[1, 5])
# 得到特定的行,列
print(a[0, :])
print(a[:, 2])
# [开始:结束:步长]
print(a[0, 1:6:2])
print(a[0, 1:-1:2])
# 更改
a_change = a
a[:, 2] = 5
a_change[:, 2] = [1, 2]
print(a, a_change)
# 3维
b = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(b)
# 得到3维元素
print(b[0, 1, 1])
print(b[:, 1, :])
# 更改3维元素
b[:, 1, :] = [[9, 9], [8, 8]]
print(b)
2.3 创建数组
import numpy as np
# 全0数组
a = np.zeros(5)
b = np.zeros((2, 3))
print(a, b)
# 全1数组
c = np.ones((4, 2, 2), dtype='int32')
print(c)
# 其他数字的数组
d = np.full((2, 2), 99, dtype='float32')
print(d)
# 使用已有shape
e = np.full_like(b, 4)
print(e)
# 随机数组
f = np.random.rand(4, 2)
f_new = np.random.random_sample(b.shape)
print(f, f_new)
# 随机整数数组(-4~7)
g = np.random.randint(-4, 7, size=(3, 3))
print(g)
# 单位矩阵
h = np.identity(3)
print(h)
# 重复
arr = np.array([[1, 2, 3]])
r1 = np.repeat(arr, 3, axis=0)
print(r1)
例子:创建如下的数组
import numpy as np
output = np.ones((5, 5))
z = np.zeros((3, 3))
z[1, 1] = 9
output[1:-1, 1:-1] = z
print(output)
注意复制!!!
import numpy as np
# 会更改原始数组
a = np.array([1, 2, 3])
b = a
b[0] = 100
print(a)
# 不会更改原始数组
a_ = np.array([1, 2, 3])
b_ = a_.copy()
b_[0] = 100
print(a_)
2.4 数学
import numpy as np
a = np.array([1, 2, 3, 4])
a += 2
a = a - 2
a = a * 2
e = a / 2
print(a)
b = np.array([2, 2, 4, 4])
c = a + b
print(c)
e = a ** 2
print(e)
f = np.sin(a)
g = np.cos(a)
print(f, g)
更多操作查询:https://numpy.org/doc/stable/reference/routines.math.html
2.5 线性代数
import numpy as np
a = np.ones((2, 3))
b = np.full((3, 2), 2)
# 矩阵相乘
c = np.matmul(a, b)
print(c)
# 计算行列式
d = np.identity(3)
e = np.linalg.det(d)
print(e)
更多操作查询:https://numpy.org/doc/stable/reference/routines.linalg.html
2.6 统计
import numpy as np
stats = np.array([[1, 2, 3], [4, 5, 6]])
a = np.min(stats)
b = np.max(stats)
c = np.min(stats, axis=0)
print(a, b, c)
e = np.sum(stats)
f = np.sum(stats, axis=0)
print(e, f)
2.7 重组
import numpy as np
before = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
after = before.reshape(2, 2, 2)
print(after)
# 垂直堆叠向量
v1 = np.array([1, 2, 3, 4])
v2 = np.array([5, 6, 7, 8])
v_ = np.vstack([v1, v2])
v__ = np.vstack([v1, v2, v1, v2])
print(v_, v__)
# 水平堆叠向量
h1 = np.ones((2, 4))
h2 = np.zeros((2, 2))
h_ = np.hstack((h1, h2))
print(h_)
2.8 其他
import numpy as np
# 加载数据
data = np.genfromtxt('data.txt', delimiter=',')
data = data.astype('int32')
print(data)
# 返回布尔值
high_value = data > 50
print(high_value)
# 返回数值
high_value = data[data > 50]
print(high_value)
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a[[1, 2, 8]]
print(b)
# 有一个值大于就返回True
any_value = np.any(data > 50, axis=0)
print(any_value)
# 全部值值大于就返回True
all_value = np.all(data > 50, axis=1)
print(all_value)
value = ((data > 50) & (data < 100))
print(value)
# 相反
value_fan = (~((data > 50) & (data < 100)))
print(value_fan)
3 小测试