- 数组的创建
import numpy as np
x = np.array([1,2,3,4,5,6,7,8])
X = np.array([
[1,2,3,4],
[5,6,7,8]
])
# 形状
print(x.shape)
print(X.shape)
# 维度
print(x.ndim)
print(X.ndim)
# 元素个数
print(x.size)
print(X.size)
# 数据类型
print(x.dtype)
print(X.dtype)
(8,)
(2, 4)
1
2
8
8
int32
int32
2.创建array的一些便捷函数:
arange(startNum,endNum,steplength,dtype=None)
x = np.arange(10)
X = np.arange(1, 10, 2, dtype='float32')
print(x)
print(X)
[0 1 2 3 4 5 6 7 8 9]
[1. 3. 5. 7. 9.]
ones(shape,dtype=None,order = 'C')
x = np.ones(5,dtype='int32')
X = np.ones((2,3), dtype='float32')
print(x)
print(X)
结果如下:
[1 1 1 1 1]
[[1. 1. 1.]
[1. 1. 1.]]
ones_like(array,dtype='float',order = 'C')
创建一个和 array形状一样的新数组
x1 = np.ones_like(x,dtype=float)
[1. 1. 1. 1. 1.]
zeros()
zeros_like()
empty():创建全为零的空数组,数据未初始化不要用
empty_like()
full(shape,num,dtype=None):数组里面数据全为num
full_like(array,num,dtype=None)
x = np.full(4,5)
x1 = np.full_like(x,6)
print(x)
print(x1)
结果如下:
[5 5 5 5]
[6 6 6 6]
np.random.randn(d0,d1,...,dn):dk表示维度
x1 = np.random.randn()
x2 = np.random.randn(2)
x3 = np.random.randn(2,3)
print(x1)
print(x2)
print(x3)
-0.8884917348821152
[-0.75606673 0.00434954]
[[-1.29902618 -0.77021982 -0.26975532]
[-0.29984148 0.09659716 -0.93599409]]
3、array本身支持的大量操作和函数
一下操作都是 elementwise的:
A = np.arange(10).reshape(2,5)
print(A)
print(A.shape)
print(A+1)
print(A*3)
print(np.sin(A))
print(np.exp(A))
B = np.random.randn(2,5)
print(A+B)
结果如下:
[[-0.28060406 1.27730837 0.99342338 3.33554666 3.75335682]
[ 5.26228444 5.1825406 6.75159336 7.92368786 9.00068879]]
4、利用numpy对数组按索引查询
基础索引:
A = np.arange(20).reshape(4,5)
print(A)
print(A[2]) # 第三行
print(A[:1,2:])
A[:2,:3] = 666
print(A)
结果如下:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[10 11 12 13 14]
[[2 3 4]]
[[666 666 666 3 4]
[666 666 666 8 9]
[ 10 11 12 13 14]
[ 15 16 17 18 19]]
神奇索引:
A = np.arange(10)
print(A)
print(A[[0,3,9]])
index = np.array([[0,3,1],[2,4,8]])
print(A[index])
结果如下:
[0 1 2 3 4 5 6 7 8 9]
[0 3 9]
[[0 3 1]
[2 4 8]]
举例:
求得数组中最大的前三个数值:
# 产生十个随机数
arr = np.random.randint(1,100,10)
print(arr)
# 对arr进行有小到大排序,并返回索引
print(arr.argsort())
print(arr[arr.argsort()[-3:]])
结果如下:
[21 91 97 8 13 96 72 14 79 62]
[3 4 7 0 9 6 8 1 5 2]
[91 96 97]
示例2:
A = np.arange(20).reshape(4, 5)
print(A)
print(A[0])
print(A[0, :])
print(A[[1, 3]])
print(A[[0, 2, 3], [1, 3, 4]])
结果如下:
[[ 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]
[0 1 2 3 4]
[[ 5 6 7 8 9]
[15 16 17 18 19]]
[ 1 13 19]
布尔索引:
示例1
A = np.arange(10)
print(A)
print(A>5)
print(A[A>5])
结果如下:
[0 1 2 3 4 5 6 7 8 9]
[False False False False False False True True True True]
[6 7 8 9]
示例2:
arr = np.arange(20).reshape(4,5)
print(arr)
# 返回第三列元素大于5 的行
print(arr[:,3]>5)
print(arr[arr[:,3]>5])
结果如下:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[False True True True]
[[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
示例3:
arr = np.arange(20).reshape(4,5)
print(arr)
condition = (arr%2==0) | (arr>15)
print(arr[condition])
结果如下:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[ 0 2 4 6 8 10 12 14 16 17 18 19]
5、random中常用的随机函数
np.random.seed(666)
x1 = np.random.rand(3,4)
x2 = np.random.randn(5)
x3 = np.random.randint(0,10,3)
x4 = np.random.randint(0,10,size=(2,3))
x5 = np.random.choice(x3,2)
x6 = np.random.choice(6,(2,3)) ---> 从 range(6)中随机选出(2,3)数组
x1 = np.arange(10)
# 随机打散 返回值为 None,改变了原来的数组
np.random.shuffle(x1)
print(x1)
x2 = np.arange(20).reshape(4,5)
# 随机打散行,列不变
np.random.shuffle(x2)
print(x2)
x1 = np.arange(10)
# 随机打散 返回了一个新的数组,原来的数组不变
print(np.random.permutation(x1))
print(x1)
x2 = np.arange(4).reshape(2,2)
print(np.random.permutation(x2))
print(x2)
结果如下:
[7 0 6 8 5 3 9 2 1 4]
[0 1 2 3 4 5 6 7 8 9]
[[2 3]
[0 1]]
[[0 1]
[2 3]]
# 均值为 1 ,方差为5 的(1,10)维高斯分布数组
x1 = np.random.normal(1,5,10)
print(x1)
x2 = np.random.normal(2,4,(2,5))
print(x2)
# 1-10之间的均匀分布 的(1,10)维数组
x1 = np.random.uniform(1,5,10)
print(x1)
x2 = np.random.uniform(2,4,(2,5))
print(x2)
示例:
import numpy as np
import matplotlib.pylab as plt
# 均匀生成 -10 - 10 之间100个数
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
6、常用的数学统计函数
x = np.arange(12).reshape(3, 4)
print(np.sum(x)) # 所有元素求和 --> 66
print(np.prod(x)) # 所有元素相乘 --> 0
print(np.cumsum(x)) # 累加求和 --> [ 0 1 3 6 10 15 21 28 36 45 55 66]
print(np.cumprod(x)) # 累加求乘积 --> [0 0 0 0 0 0 0 0 0 0 0 0]
print(np.min(x)) # 求x中最小元素 --> 0
print(np.max(x)) # 求x中最大元素 --> 11
print(np.percentile(x, [25, 50, 75])) # 0-100之间的分位数 --> [2.75 5.5 8.25]
print(np.quantile(x, [0.25, 0.5, 0.75])) # 0-1之间的分位数 --> [2.75 5.5 8.25]
print(np.median(x)) # 中位数 --> 5.5
print(np.mean(x)) # 平均数--> 5.5
print(np.std(x)) # 标准差--> 3.452052529534663
print(np.var(x)) # 方差--> 11.916666666666666
# 加权平均
# weights 的shape 需要和 x 一样
weights = np.random.rand(*x.shape)
print(np.average(x, weights=weights)) # --> 5.672183001501558
numpy中,axis的作用:
axis=0 代表行,axis=1代表列
axis = k ,则将k视作第一维度进行计算,即 按第k维计算
x = np.arange(12).reshape(3, 4)
print(x)
print(x.sum(axis=0))
print(x.cumsum(axis=1))
print(x.cumprod(axis=0))
结果如下:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[12 15 18 21]
[[ 0 1 3 6]
[ 4 9 15 22]
[ 8 17 27 38]]
[[ 0 1 2 3]
[ 0 5 12 21]
[ 0 45 120 231]]
print(np.sum(x,axis=0))
print(np.cumsum(x,axis=0))
print(np.mean(x,axis=0))
7、数组中满足条件的元素个数
可以看出,numpy比python在处理数据方面要优越很多!
8、如何给数组增加维度
给数组增加维度: 原数组: arr=[1,2,3,4] 是(4,)维的 新数组: newarr = [[1,2,3,4]] 是(1,4)维的 方法: 1、np.newaxis:是一个关键字,使用索引的语法给数组添加关键字 2、np.expand_dims(arr,axis):方法,在axis为arr添加一个新的维度 3、np.reshape(a,newshape):方法
x = np.arange(5)
print(x.shape) #--> (5,)
# np.newaxis 就是 None 的别名
# print(np.newaxis is None)
# print(np.newaxis == None)
print(x[np.newaxis,:]) #--> [[0 1 2 3 4]]
print(x[np.newaxis,:].shape) #--> (1,5)
# print(x[:,np.newaxis])
# print(x[:,np.newaxis].shape)
x = np.arange(5)
print(x.shape)
print(np.expand_dims(x, axis=0))
print(np.expand_dims(x, axis=0).shape)
print(np.reshape(x, (1, 5)))
print(np.reshape(x, (1, -1))) # 一行,自动计算需要多少列,而不需要手动输入数字5
print(np.reshape(x, (-1, 1)))
9、数据合并操作
数组合并操作: 原数组增加行:增加一个样本 原数组增加列:增加一个属性 1、np.concatenate(array_list,axis= 0 or 1) :沿着指定axis进行数组合并 2、np.vstack() 或者 np.row_vstack(array_list): 垂直vertical、按行row wise 进行合并 3、np.hstack() 或者 np.column_stack(array_liat):水平horizontal、按列 column wise 进行合并
按行合并:
a = np.arange(6).reshape(2, 3)
b = np.random.randint(10, 20, (3, 3))
c1 = np.concatenate([a, b])
# print(c1)
c2 = np.vstack([a, b])
# print(c2)
c3 = np.row_stack([a, b])
# print(c3)
按列合并:
a = np.arange(12).reshape(3, 4)
b = np.random.randint(10, 20, (3, 3))
c1 = np.concatenate([a, b],axis=1)
print(c1)
c2 = np.hstack([a, b])
print(c2)
c3 = np.column_stack([a, b])
print(c3)