numpy矩阵的基本操作和运用函数
这里是小弟学习numpy array的一些小总结,这里做一下笔记方便日后查看,若有错误请大家指正,共同进步。
矩阵的初始化
import numpy as np
"""
构造1、2、 3维数组
官方文档,print(help(np.array))
"""
a = np.array([1, 2, 3])
b = np.array([[1, 2], [2, 3]])
c = np.array([[[1, 2], [2, 3]], [[1, 2], [2, 3]]])
if False:
print(a)
print(b)
print(c)
打印矩阵shape
a = np.array([1, 2, 3])
b = np.array([[1, 2], [2, 3]])
c = np.array([[[1, 2], [2, 3]], [[1, 2], [2, 3]]])
print(a.shape)
print(b.shape)
print(c.shape)
矩阵的数据类型必须一致
与python list不同所有元素类型是相同的,如
d = np.array([1,2,3,4.0])
print(type(d[2]))
d = np.array([1, 2, 3, 4.0])
print(d.dtype)
print(d[0].dtype)
即使传入的只有一个浮点型,为了适配,会默认全部转化为浮点型
一些基本操作
exp、floor 、T、shape相关操作,demo
"""
e的次幂
"""
if False:
B = np.arange(3)
print(np.exp(B))
print(np.sqrt(B))
"""
floor 向下取整
"""
if False:
a = 10 * np.random.random((2, 4))
print(a)
b = np.floor(a)
print(b)
"""
矩阵拉平,ravel()
一行一行的拼接起来
shape 操作
T 转置 array.T
"""
if False:
a = np.arange(24)
a = a.reshape(4, 6)
print(a)
b = a.ravel()
print(b)
# 这操作类似reshape,reshape操作为 b.reshape(3, -1).这里-1表示自动推断
b.shape = (3, 8)
print(b)
print(b.T)
"""
当前矩阵扩展
tile
"""
if False:
a = np.array([[1, 2, 3], [4, 5, 6]])
print("---a----")
print(a)
print("---tile----")
print(np.tile(a, (2, 3)))
排序
"""
求列最大值 和 最大值序号
"""
if False:
a = np.floor(10 * np.random.random((3, 6)))
a = a.astype(int)
print("---a----")
print(a)
b_index = np.argmax(a)
# 每一列最大值的序号
b_index2 = a.argmax(axis=0)
print("---b_index----")
print(b_index)
print(b_index2)
# 根据序号,找出每一列的最大值
c = np.arange(0, 6)
data_max = a[b_index2, c]
print("---data_max----")
print(data_max)
"""
排序 sort
"""
if True:
a = np.array([[2, 10, 3], [9, 5, 6]])
print("---a----")
print(a)
print("----列排序---")
print(np.sort(a, axis=0))
print("----行排序---")
print(np.sort(a, axis=1))
b = np.array([2, 5, 3, 9])
print("----排序从小到大的数的序号---")
print(np.argsort(b))
切片操作
numpy中 “:”表示所有
1、array[1:3] 表示选取1到2的数据
2、传入数组 array[a], a的size为array的size
当a为bool类型,则false就选取,false就舍弃,size必须与array一样
当a为整型,则表示选取下标为当前整数的项,size不必与array的size一样
二维数组一样,只不过传入的是2个数组,两个数组必须是一维的,关于二维的可以看下面判断操作的例子
vector = np.array([1, 2, 3, 4])
sun_v = vector[0:3]
sun_v2 = vector[[False, True, False, False]]
sun_v22 = vector[[0, 1, 0]]
print(sun_v)
print(sun_v2)
print(sun_v22)
matrix = np.array([[1, 2, 4],
[3, 4, 6],
[7, 9, 6]])
sun_m = matrix[:, 1:3]
print(sun_m)
3、利用split
- 按列切:hsplit
- 按行切:vsplit
- 参数 :
- 1、传入切分数组
- 2、<1>切分的份数(平均切),<2>传入元组则是切分位置(3,4)表示的是在下表3,和4的位置切分
if True:
a = np.floor(10 * np.random.random((3, 6)))
a = a.astype(int)
print("---a----")
print(a)
b = np.hsplit(a, 3)
print("---b----")
b2 = np.hsplit(a, (3, 5))
print(b)
print("---b2----")
print(b2)
c = np.vsplit(a, 3)
print(c)
#输出:
---a----
[[7 5 5 7 7 5]
[8 9 0 2 3 5]
[6 0 8 6 1 0]]
---b----
[array([[7, 5],
[8, 9],
[6, 0]]), array([[5, 7],
[0, 2],
[8, 6]]), array([[7, 5],
[3, 5],
[1, 0]])]
---b2----
[array([[7, 5, 5],
[8, 9, 0],
[6, 0, 8]]), array([[7, 7],
[2, 3],
[6, 1]]), array([[5],
[5],
[0]])]
---c----
[array([[7, 5, 5, 7, 7, 5]]), array([[8, 9, 0, 2, 3, 5]]), array([[6, 0, 8, 6, 1, 0]])]
拼接操作
hstack 横着拼接
vstack 竖着拼接
if True:
a = np.floor(10 * np.random.random((2, 4)))
print(a)
print("-------")
b = np.floor(10 * np.random.random((2, 4)))
print(b)
print("-------")
print(np.hstack((a, b)))
print("-------")
print(np.vstack((a, b)))
# 输出:
[[1. 2. 8. 0.]
[8. 0. 8. 0.]]
-------
[[4. 7. 3. 3.]
[3. 2. 6. 4.]]
-------
[[1. 2. 8. 0. 4. 7. 3. 3.]
[8. 0. 8. 0. 3. 2. 6. 4.]]
-------
[[1. 2. 8. 0.]
[8. 0. 8. 0.]
[4. 7. 3. 3.]
[3. 2. 6. 4.]]
判断操作
语法:r = (array ==10) 针对 array中每个元素做判断,返回与r shape相同的数组,
运用场景:
1、利用r 带入 上面的切片例子中,判断一维数组中是否含有某值,有返回该值的数组,没用就返回空
2、利用用r 带入 上面的切片例子中,这个来 取数组中 某列元素中 等于 10的行
注意:type() 是数据类型 , array.dtype则是数组元素的数据类型
#利用判断来选区特定的行数
a = np.array([1, 2, 1, 6, 321, 1, 312])
b = (a == 10)
r = a[b]
print(r.size)
matrix = np.array([[1, 2, 4, 5],
[3, 4, 6, 5],
[7, 9, 6, 7]]
)
"""
取第二列中等于4的行
"""
# 注意这里,1:2 和 直接写1的区别,前面返回2维数组,后面是返回一维的数组,用前者就会报错
f = matrix[:, 1:2]
f = matrix[:, 1]
print(f)
filter2 = (f == 4)
print(filter2)
result = matrix[filter2]
print(result)
数据转换 astype()
matrix = np.array([[1, 2, 4, 5],
[3, 4, 6, 5],
[7, 9, 6, 7]]
)
print(matrix.dtype)
matrix2 = matrix.astype(float)
print(matrix2.dtype)
矩阵之间的操作
1、直接运用运算符(+ - * / **求幂指数)则是对应位置的数据进行 乘除,
2、.dot() 则是数学意义上的矩阵相乘
下面为求和操作函数sum
matrix = np.array([[1, 2, 4, 5],
[3, 4, 6, 5],
[7, 9, 6, 7]]
)
# min、max
print(matrix.min())
print(matrix.max())
# sum
# 按行求和
sum_r = matrix.sum(axis=1)
# 按列求和
sum_c = matrix.sum(axis=0)
print(sum_r)
print(sum_c)
生成序列数组 arange
arange 参数分别为,start end step , type
ran = np.arange(20)
ran2 = np.arange(1, 21, 2, int)
print(ran)
print(ran2)
改变形状操作:reshape
shape 矩阵的形状
ran = np.arange(20)
a = ran.reshape(4, 5)
print(a)
print(a.shape)
初始化全为0或者1的矩阵
zeros
ones
参数为元组
a = np.zeros((3, 4), int)
b = np.ones((3, 4), int)
print(a)
print(b)
随机数
np.random模块
# 返回区间 [0,1)区间的随机数
r = np.random.random(size=(2, 3))
print(r)
# 随机整数 randint(low, high=None, size=None, dtype=’l’)
r_int = np.random.randint(0, 9, size=(2, 3))
print(r_int)
# 随机高斯分布 normal(loc=0.0, scale=1.0, size=None)
# loc:均值,scale:标准差,size:抽取样本的size
r_gs = np.random.normal(loc=0.0, scale=1, size=(2, 3))
print(r_gs)
# 标准正态分布,参数为size, randn(*dn)
r_n = np.random.randn(2, 3)
print(r_n)
# numpy.random.rand(d0, d1, …, dn)函数
# 生成一个(d0*d1* …* dn)维位于[0, 1)中随机样本
r_n = np.random.randn(2, 3)
print(r_n)
随机打乱序列 选择数据
打乱 numpy.random.shuffle
选择 numpy.random.choice()
x = np.arange(0, 8, 1, int)
print(x)
np.random.shuffle(x)
print(x)
c = np.random.choice(x)
c2 = np.random.choice(x, 6) # 随机选取6个数字
print(c)
print(c2)
二项分布采样
numpy.random.RandomState.binomial(n, p, size=None)
公式: 在 n个样本中,每次选中的概率为p,选中s个的概率
numpy.random.RandomState.binomial(n, p, size=None)表示对一个二项分布进行采样,在n个样本中,选择n次 返回选中的次数,size:实验的次数,返回每次选中的个数的数组 (这里是小弟个人理解,正确与否还望大家指正)
比如:c = np.random.binomial(5, 0.8, size=32)
就是选中的概率 0.8的情况下,选择5次,中的次数,然后实验32次,每次成功的次数
c = np.random.binomial(5, 0.8, size=32)
print(c)
随机种子
numpy.random.RandomState()
循环内:
- 种子相同的时候,每次循环的随机的数是相同的
- 一次循环内调用2次,2个随机的种子相同,数据也不同
循环外与内比:
- uniform 循环外 内都不同
- randint 循环外与循环内种子数相同的第一个调用的随机数相同
具体看下面的例子:
for i in [1, 2]:
rng = np.random.RandomState(12)
arrayA = rng.randint(0, 8, (2, 3))
arrayB = rng.uniform(0, 1, (2, 3))
arrayC = rng.randint(0, 8, (2, 3))
arrayD = rng.uniform(0, 1, (2, 3))
print(arrayA)
print(arrayB)
print(arrayC)
print(arrayD)
print("\n")
arrayC = np.random.RandomState(12).randint(0, 8, (2, 3))
arrayD = rng.uniform(0, 1, (2, 3))
print(arrayC)
print(arrayD)
#输出为
[[3 3 6]
[5 1 2]]
[[0.53373939 0.01457496 0.91874701]
[0.90071485 0.03342143 0.95694934]]
[[6 2 0]
[5 0 4]]
[[0.94422514 0.85273554 0.00225923]
[0.52122603 0.55203763 0.48537741]]
[[3 3 6]
[5 1 2]]
[[0.53373939 0.01457496 0.91874701]
[0.90071485 0.03342143 0.95694934]]
[[6 2 0]
[5 0 4]]
[[0.94422514 0.85273554 0.00225923]
[0.52122603 0.55203763 0.48537741]]
[[3 3 6]
[5 1 2]]
[[0.76813415 0.16071675 0.76456045]
[0.0208098 0.13521018 0.11627302]]
区间内平均取值
linspace在一个区间内平均去取值
a = np.linspace(0, 2*np.pi, 20)
print(a)
这里实验数据加解释文档都在代码中有展示,大家如果感兴趣可以直接下载文档来跑看,如果想演示哪个demo,将开头的if False修改为if True 单独查看跑的结果:https://download.csdn.net/download/zhangpengzp/10570936