import numpy as np#导入
a = np.array([[1, 2], [3, 4]])#直接生成一个
ndarray有.ndim .shape .size .dtype .itemsize 等属性
print(a.ndim)
print(a.shape)#形状
print(a.size)#元素个数
print(a.dtype)#类型
print(a.itemsize)#单个元素占的字节数
3
(2, 3, 4)
24
float64
8
尽量避免非同质的ndarray对象
ndarray数组的创建和变换
#从列表、元组等创建
list = [1, 2, 3]
x0 = np.array(list, dtype=np.float)
x1 = np.arange(10)#返回0~n-1的ndarray
x2 = np.ones((2, 2), dtype = np.float)
x3 = np.zeros((2, 2), dtype = np.float)
x4 = np.eye(3)#生成单位矩阵
print(x0)
print(x1)
print(x2)
print(x3)
print(x4)
[1. 2. 3.]
[0 1 2 3 4 5 6 7 8 9]
[[1. 1.]
[1. 1.]]
[[0. 0.]
[0. 0.]]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
y0 = np.zeros_like(x0)
y1 = np.ones_like(x1)
y2 = np.full_like(x2, 10)#根据x2形状生成数组,每个元素为10
print(y0)
print(y1)
print(y2)
[0. 0. 0.]
[1 1 1 1 1 1 1 1 1 1]
[[10. 10.]
[10. 10.]]
a = np.linspace(0, 10, 5)#从0-10,等间距生成两个元素
print(a)
b = np.linspace(0, 10, 2)
print(b)
c = np.concatenate((a, b))#连接起来
print(c)
[ 0. 2.5 5. 7.5 10. ]
[ 0. 10.]
[ 0. 2.5 5. 7.5 10. 0. 10. ]
下面是ndarray维度变换
'''
.reshape(shape):生成新创建对象,变为shape,不改变原数组
.resize(shape):改变原数组,变为shape,改变原数组
.swapaxes(ax1, ax2):将维度调换
.flatten():将原数组变为1维数组,赋给新的数组,不改变原数组
'''
a = np.ones((2, 3, 4), dtype=np.float)
b = a.reshape(3, 8)
print(a)
print(b)
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]
a = np.ones((2, 3, 4), dtype=np.float)
a.resize((3,8))#注意,a.resize(shape)是一个操作,不能赋值给变量
print(a)
[[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1.]]
a = np.ones((2, 3, 4), dtype=np.float)
b = a.flatten()
print(b)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
#改变数据类型
a = np.ones((2, 3, 4), dtype=np.float)
new_a = a.astype(np.int32)#astype生成新的数组!!!!
print(new_a.flatten())
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
list = new_a.flatten().tolist()
print(list)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ndarray数组的操作
a = [1, 2, 3, 4, 5]
print(a[2])
print(a[0: 4: 2])#不含终止处
3
[1, 3]
a = np.arange(24).reshape((2, 3, 4))
print(a)
a[0,1,1] #第一维0, 第二维1, 第三维1
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
5
a[:, 1:3, :]
array([[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
下面ndarray数组的运算
#数组与标量运算,相当于数组中的每一个元素都与标量进行运算
a = np.arange(24).reshape((2, 3, 4))
mean = a.mean()
print(mean)
a = a / mean
print(a)
11.5
[[[0. 0.08695652 0.17391304 0.26086957]
[0.34782609 0.43478261 0.52173913 0.60869565]
[0.69565217 0.7826087 0.86956522 0.95652174]]
[[1.04347826 1.13043478 1.2173913 1.30434783]
[1.39130435 1.47826087 1.56521739 1.65217391]
[1.73913043 1.82608696 1.91304348 2. ]]]
#numpy提供了一些一元函数
'''
大多数一元函数都生成新的数组
np.abs(x)绝对值
np.fabs(x)
np.sqrt(x)各个元素平方根
np.square(x)各个元素的平方
np.log(x)
np.log10(x)
np.log2(x)
np.rint(x)四舍五入
np.modf(x)
np.cos(x),np.sin(x),np.cosh()........
np.exp(x)指数值
np.sign(x)符号函数
'''
#读写CSV(逗号分隔符)文件
#写入:该函数可以保存为任何字符串分割的文件
#np.savetxt(frame, array, fmt='%18e', delimiter=None)
#frame:文件、字符串或者产生器,可以是.gz or .bz2的压缩文件
#array:存入文件的数组
#fmt:写入文件的格式
#delinmiter:分割字符串,默认为空格
#e.g.:
a = np.arange(20).reshape(4,5)
np.savetxt('a.csv', a, fmt='%d', delimiter=',')
#'%d' -> '%.1f' 保存浮点数数据
#np.loadtxt(frame, dtype = np.float, delimiter=None, unpack=False)
#frame:可以是.gz or .ba2等压缩文件
#dtype:数据类型,可选
#delimiter:分隔符,默认是任何空格
#unpack:False读入一个数组
#e.g.:
b = np.loadtxt('a.csv', dtype=np.float, delimiter=',')
print(b)
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]]
任意维数据的存取
#a.tofile(frame, sep='', format'%s')
#frame:文件、字符串
#sep:数据分割字符串,如果是空,则写入文件为二进制
#format:写入数据的格式
a = np.arange(20).reshape(4,5)
a.tofile('a.dat', sep=',', format = '%d')
#np.fromfile(frame, dtype=np.float, count=-1, sep='')
#count:读入元素个数,-1表示全部读入
#sep:数服分割字符串,如果是空串,表示读入文件为2进制
#e.g.:
c = np.fromfile('a.dat', dtype=np.int, sep=',')
print(c)#这种存的维度会丢失
d = c.reshape(4, 5)
print(d)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
#np.save和np.load函数
#实际用过,非常好用
#np.save(fname, array) .npy或.npz(压缩过)
#np.load(fname)
下面是numpy随机数函数
numpy 的 random 子库,提供了随机数函数
#np.random.rand() 0~1均匀分布
#np.random.randn() 标准正态分布数组
#np.random.randint(low, high, shape)
#np.random.seed(s) 生成随机数种子
#0~1均匀分布
a = np.random.rand(3, 4)
print(a)
[[0.90626231 0.29139332 0.58954906 0.70504869]
[0.01428861 0.33528848 0.40629122 0.32844946]
[0.63475893 0.6272589 0.86733581 0.12854771]]
#正态分布数组
sn = np.random.randn(3, 4)
print(sn)
[[ 0.34513616 -1.05754585 0.29635276 -1.0462211 ]
[-1.08349758 -0.04966122 -0.98514382 -0.80901674]
[-0.48856077 -0.68859755 -0.90790609 0.69630339]]
#[low, high]均匀分布
b = np.random.randint(100, 200, (3,4))
print(b)
[[132 131 137 115]
[163 155 106 117]
[170 175 126 154]]
#通过指定相同的随机数种子来产生相同的随机数数组
np.random.seed(10)
a = np.random.randint(100, 200, (2,2))
print(a)
np.random.seed(10)
b = np.random.randint(100, 200, (2,2))
print(b)
[[109 115]
[164 128]]
[[109 115]
[164 128]]
#np.random.shuffle(a) 根据数组a的第一个轴进行随机排列,打乱数组a
#np.random.permutation(a) 同shuffle,只不过产生新的数组
#np.random.choice(a) 从a中随机抽取元素
a = np.random.randint(100, 200, (2, 2))
print(a)
np.random.shuffle(a)
print(a)
[[189 193]
[129 108]]
[[189 193]
[129 108]]
a = np.random.randint(100, 200, (2, 2))
print(a)
b = np.random.permutation(a)
print(a)
print(b)
[[100 140]
[136 116]]
[[100 140]
[136 116]]
[[136 116]
[100 140]]
b = np.random.randint(100, 200, (8,))
print(b)
c = np.random.choice(b, (2,2), replace=False)
[136 127 118 193 177 122 123 194]
#np.random.uniform(low, high, size) low~high的均匀分布
#np.random.normal(loc, scale, size) loc:均值,scale:标准差, size:形状
#产生高斯分布
#np.possion(lam, size) 泊松分布数组, lam为参数,size形状
u = np.random.uniform(0, 10, (3,4))
print(u)
n = np.random.normal(0, 1, (2,2))
print(n)
[[3.35499647 4.11182546 0.76855502 8.5304299 ]
[4.39987456 1.21954147 7.31734625 1.38782465]
[7.66880049 8.31989768 3.09778055 5.9758229 ]]
[[ 1.05800516 -0.09178444]
[-0.72403459 -0.40046036]]
下面是numpy统计函数
#np.sum(a, axis)求和
#np.mean(a, axis) 均值
#np.average(a, axis, weights) 计算加权平均值
#np.std(a, axis) 标准差
#np.var(a, axis) 方差
a = np.arange(15).reshape(3, 5)
print(a)
print(np.sum(a))
#对于一个数组,最外层为维度0,越往里越高维
mean1 = np.mean(a, axis = 1)
mean2 = np.mean(a, axis = 0)
print(mean1)
print(mean2)
std_a = np.std(a)
var_a = np.var(a)
print(std_a, " ", var_a)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
105
[ 2. 7. 12.]
[5. 6. 7. 8. 9.]
4.320493798938574 18.666666666666668
#np.min(a) np.max(a) 最大值和最小值
#argmax(a) argmin(a) 最大值、最小值索引,数据先降至一维
#np.unravel_index(index, shape)根据shape将一维下标转换为多维下标
#常常与argmax()等联合使用
#median(a) 中位数
b = np.arange(15).reshape(3,5)
print(b)
index = np.argmax(b)
print(index)
index_2d = np.unravel_index(index, (3, 5))
print(index_2d)
median = np.median(b)
print(median)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
14
(2, 4)
7.0
下面是numpy梯度函数
#np.gradient(f)
#计算数组中f元素的梯度,当f为多维的时候,返回每个维度梯度
1014

被折叠的 条评论
为什么被折叠?



