numpy常用总结

目录

np.expand_dims()&np.newaxis()

np.expand_dims:用于扩展数组的形状

np.newaxis:增加一个维度

np.argmax:返回沿轴最大值的索引值

numpy.std() 计算矩阵标准差

样本标准差 和 总体标准差

np.isin


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]]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值