目录
pandas中isin()函数及其逆函数使用 https://blog.csdn.net/lzw2016/article/details/80472649
Numpy中ndim、shape、dtype、astype的用法 https://blog.csdn.net/Da_wan/article/details/80518725
numpy库np.percentile详解 https://blog.csdn.net/brucewong0516/article/details/80205422
np.tile()函数的作用https://blog.csdn.net/qq_39072607/article/details/89364254
np.expand_dims()&np.newaxis()
np.expand_dims:用于扩展数组的形状
该功能是改变数组维度,为了未来的深度学习中选用适当的维度的tensor
import numpy as np
a = np.array([[[1,2,3],[4,5,6]]])
print(a.shape)
print('------------------------------------------------')
# np.expand_dims(a, axis=0)表示在0位置添加数据
b = np.expand_dims(a,axis=0)
print(b.shape)
print('------------------------------------------------')
# np.expand_dims(a, axis=1)表示在1位置添加数据
c = np.expand_dims(a,axis=1)
print(c.shape)
print('------------------------------------------------')
# np.expand_dims(a, axis=2)表示在2位置添加数据
d = np.expand_dims(a,axis=2)
print(d.shape)
print('------------------------------------------------')
# np.expand_dims(a, axis=3)表示在3位置添加数据
e = np.expand_dims(a,axis=3)
print(e.shape)
print('------------------------------------------------')
(1, 2, 3)
------------------------------------------------
(1, 1, 2, 3)
------------------------------------------------
(1, 1, 2, 3)
------------------------------------------------
(1, 2, 1, 3)
------------------------------------------------
(1, 2, 3, 1)
------------------------------------------------
np.newaxis:增加一个维度
可以利用np.newaxis与广播规则做一些矩阵,或者调整我们矩阵的维度
np.argmax:返回沿轴最大值的索引值
A = np.arange(6).reshape(1,6)
A
array([[0, 1, 2, 3, 4, 5]])
B = np.argmax(A)
B
6
E = np.array([[1,2,3],[2,1,4],[3,6,1]])
print(E)
E.shape
[[1 2 3]
[2 1 4]
[3 6 1]]
(3, 3)
F= np.argmax(E, axis=0)
array([2, 2, 1], dtype=int64)
numpy.std() 计算矩阵标准差
>>> a = np.array([[1, 2], [3, 4]])
>>> np.std(a) # 计算全局标准差
1.1180339887498949
>>> np.std(a, axis=0) # axis=0计算每一列的标准差
array([ 1., 1.])
>>> np.std(a, axis=1) # 计算每一行的标准差
array([ 0.5, 0.5])
import numpy as np
arr = [1,2,3,4,5,6]
#求均值
arr_mean = np.mean(arr)
#求方差
arr_var = np.var(arr)
#求标准差
arr_std = np.std(arr,ddof=1)
print("平均值为:%f" % arr_mean)
print("方差为:%f" % arr_var)
print("标准差为:%f" % arr_std)
样本标准差 和 总体标准差
样本方差
总体方差
np.isin
判断数组元素在另一数组中是否存在
Np_invert = np.isin(a, b, invert=True)
# a 中的元素是否在b中,如果设置了invert=True,则情况恰恰相反,即a中元素在b中则返回False
import numpy as np
# 这里使用reshape是为了验证是否对高维数组适用,返回一个和a形状一样的数组
a=np.array([1,3,7]).reshape(3,1)
b=np.arange(9).reshape(3,3)
# a 中的元素是否在b中,如果在b中显示True
Np_No_invert=np.isin(a, b, invert=False)
print("Np_No_invert\n",Np_No_invert)
# a 中的元素是否在b中,如果设置了invert=True,则情况恰恰相反,即a中元素在b中则返回False
Np_invert=np.isin(a, b, invert=True)
print("Np_invert\n",Np_invert)
# Np_No_invert
# [[ True]
# [ True]
# [ True]]
# Np_invert
# [[False]
# [False]
# [False]]