# Numpy
# Numpy提供了一个N dimension array,即n维数组
# numpy与list对比
# 存储风格:
#
# ndarray 内部存储类型相同,存储空间连续,但通用性不强
#
# list 内部存储类型可以不同,存储空间未必连续,通用性较强
#
# 并行化计算:ndarray支持向量化运算
#
# 底层语言:
# Numpy底层采用C语言编写,内部解除了GIL(全局解释器锁),其对数组的操作速度不受Python解释器的限制,效率远高于纯Python代码。
import numpy as np
# 形状
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([1, 2, 3, 4])
c = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
"""
结果:
a的形状::(2, 3)
b的形状::(4,)
c的形状::(2, 2, 3)
"""
print(f'a的形状::{a.shape}')
print(f'b的形状::{b.shape}')
print(f'c的形状::{c.shape}')
# 全0,1数组
a_1 = np.zeros(shape=(3, 4), dtype="float32")
a_2 = np.ones(shape=(2, 3), dtype="int32")
"""
a1:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
a2:
[[1 1 1]
[1 1 1]]
"""
print(f'a1:\n{a_1}')
print(f'a2:\n{a_2}')
# 深拷贝vs浅拷贝
a1 = np.array(a) # 深拷贝 拷贝值,新创一个地址空间
a2 = np.copy(a) # 深拷贝
a3 = np.asarray(a) # 浅拷贝 共享同一地址空间
a4 = a # 浅拷贝
# 生成固定范围的数组
a_3 = np.linspace(0, 10, 6) # 生成[0,10]之间等距离的6个数,左右均为闭区间
a_4 = np.arange(0, 5, 2) # [0,5),2为步长生成数组 ,左闭右开
# [ 0. 2. 4. 6. 8. 10.]
# [0 2 4]
print(a_3)
print(a_4)
# 生成随机数组
# 生成均匀分布的一组数[low,high) 左闭右开,size是数量
b1 = np.random.uniform(-1, 1, 10)
b2 = np.random.uniform(-1, 1, (2, 3))
# 生成正态分布的一组数,loc:均值;scale:标准差,size数量
b3 = np.random.normal(1.75, 0.1, 10)
# 注意这两处的size都可以规定数组形状 size=(2, 3)
"""
结果:
[ 0.0363088 0.23256217 -0.53286915 -0.86009816 0.37996377 -0.28938485
0.26787858 0.19716844 -0.69029092 -0.90420113]
[[-0.47574163 -0.71456978 0.65178125]
[-0.96966242 -0.62239606 0.43467577]]
[1.75744195 1.66689802 1.65257554 1.68035962 1.7916872 1.73766539
1.64603791 1.78604594 1.60809854 1.75272602]
"""
print(b1)
print(b2)
print(b3)
# 索引切片
b5 = np.random.normal(loc=0, scale=1, size=(8, 10))
# 第0行,前三列的数据(0,1,2)列
print(b5[0, :3])
# 形状改变
# 返回新的ndarray, 原始数据没有改变
b6 = b5.reshape((10, 8))
# 没有返回值, 对原始的ndarray进行了修改
b7 = b5.resize((10, 8))
# 转置 行变成列,列变成行 返回一个ndarray,原数据未改变
b8 = b5.T
# 类型修改
b5.astype("int32")
b5.tostring() # ndarray序列化到本地
# 数组去重
b_1 = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])
# [1 2 3 4 5 6]
np.unique(b_1)
# set的操作对象需要时一维的,.flatten()可以压缩为一维的
# {1, 2, 3, 4, 5, 6}
set(b_1.flatten())
# 逻辑运算
# > < =
# np.all() 只要有一个False就返回False,只有全是True才返回True
# np.any() 只要有一个True就返回True,只有全是False才返回False
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
c_all = np.all(c[0:2, 0:2] >= 1)
c_all1 = np.all(c[0:2, 0:2] > 1)
# True False
print(c_all, c_all1)
c_any = np.any(c[0:3, 0:3] > 7)
c_any1 = np.any(c[0:3, 0:3] < 1)
# True False
print(c_any, c_any1)
# where np.where(布尔表达式,True的值,False的值)
c_where = np.where(np.max(c) > 10, 111, 222)
# 222
print(c_where)
# 统计运算 min max mean median(中位数) var(方差) std(标准差)
# axis=0表示列 axis=1表示行 axis=-1 表示最后一维度
# 返回值
c_max_1 = np.max(c)
c_max_2 = c.max()
# 9 9
print(c_max_1, c_max_2)
# 返回索引
c_argmax_1 = np.argmax(c, axis=0)
c_argmax_2 = np.argmax(c, axis=1)
c_argmin_3 = np.argmin(c, axis=-1)
print(c_argmax_1, c_argmax_2, c_argmin_3)
c_arg = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7., 8]]])
c_argmin_1 = np.argmin(c_arg, axis=-1)
"""
结果:
[[0 0]
[0 0]]
"""
print(c_argmin_1)
# 矩阵运算
# 可以通过np.mat(array)将数组转化为矩阵
# 二维数组实现矩阵运算 区分点乘与叉乘
c1 = np.array([[1, 2], [2, 1]])
c2 = np.array([[2, 1], [1, 2]])
c3 = np.dot(c1, c2)
c4 = np.matmul(c1, c2)
c5 = c1 @ c2
"""
二维数组矩阵运算:
[[4 5]
[5 4]]
[[4 5]
[5 4]]
[[4 5]
[5 4]]
"""
print(f'二维数组矩阵运算:\n{c3}\n{c4}\n{c5}')
# 矩阵运算
c1_parase = np.mat(c1)
c2_parase = np.mat(c2)
c6 = c1_parase * c2_parase
"""
[[4 5]
[5 4]]
"""
print(c6)
"""
[[2 2]
[2 2]]
"""
c7 = c1 * c2
print(c7)
# 数组合并分割
c_hv_1 = np.hstack((c1, c2))
c_hv_2 = np.vstack((c1, c2))
# numpy.concatenate((a1,a2),axis=0|1) 垂直|水平拼接
c_hv_3 = np.concatenate((c1, c2), axis=-1)
"""
数组合并分割:
[[1 2 2 1]
[2 1 1 2]]
[[1 2]
[2 1]
[2 1]
[1 2]]
[[1 2 2 1]
[2 1 1 2]]
"""
print(f'数组合并分割:\n{c_hv_1}\n{c_hv_2}\n{c_hv_3}')
https://blog.csdn.net/m0_52987303/article/details/123609939