深度学习中可能用到的numpy函数

import numpy as np

shape,astype

import numpy as np
vector = np.array([5,6,7,8])
print(vector.shape)
print(vector)
print(vector.dtype)
vector0 = vector.astype(str)
print(vector0)
print(vector0.dtype)
# 这里用的是| 用 or 是错误的
# dim中,1是按行操作,0是按列操作
vector1 = (vector == 5) | (vector ==6)
vector2 = vector[vector1]
print(vector1)
print(vector2)

# (4,)
# [5 6 7 8]
# int32
# ['5' '6' '7' '8']
# <U11
# [ True  True False False]
# [5 6]
# 数据类型是根据vector里边的元素而定的

arange,

import numpy as np
vector = np.arange(15).reshape(3,5)
# 如果不加后边的dtype则创建出来的是float64类型的
vector0 = np.ones((3,5), dtype =np.int32)
# 是在random模块中使用random函数,
vector1 = np.random.random((2,3))
# 在0-100之间取10个数
vector2 = np.linspace(0, 100, 10)
print(vector)
print(vector0)
print(vector2)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]]
# [[1 1 1 1 1]
#  [1 1 1 1 1]
#  [1 1 1 1 1]]
# [  0.          11.11111111  22.22222222  33.33333333  44.44444444
#   55.55555556  66.66666667  77.77777778  88.88888889 100.        ]

floor,ravel,

import numpy as np
x = np.arange(3)
print(np.exp(x))
print(np.sqrt(x))
x1 = np.random.random((2,3))
print(x1)
print(np.floor(x1))# 向下取整函数
print(x1.ravel())# 将矩阵变成1*n就是岑长
x1.shape = (3,2)# 这样可以改变x1的shape
print(x1.shape)

# [1.         2.71828183 7.3890561 ]

# [0.         1.         1.41421356]

# [[0.73688852 0.31035425 0.64673206]
#  [0.11308663 0.21372793 0.5240637 ]]

# [[0. 0. 0.]
#  [0. 0. 0.]]

# [0.73688852 0.31035425 0.64673206 0.11308663 0.21372793 0.5240637 ]

# (3, 2)

vstack,hstack

import numpy as np
x = np.floor(10 * np.random.random((2, 2)) + 1)
x1 = np.floor(10 * np.random.random((2, 2)) + 2)
print(x); print(x1)
print(np.vstack((x,x1)))# 按行进行拼接
print(np.hstack((x,x1)))# 按列进行拼接
# [[10.  9.]
#  [10.  6.]]

# [[ 8. 11.]
#  [10. 11.]]

# [[10.  9.]
#  [10.  6.]
#  [ 8. 11.]
#  [10. 11.]]

# [[10.  9.  8. 11.]
#  [10.  6. 10. 11.]]

hsplit,

import numpy as np
x = np.floor(10 * np.random.random((2, 12)) + 1)
print(x); print("---------")
print(np.hsplit(x, (3)));print("---------")# 按行将x平均分成三份
print(np.hsplit(x, (3,4)))# 按行将x在3,4的位置切开,
# vsplit是按列进行切分
[[ 9.  5.  8.  6.  7. 10.  8.  8.  3.  5.  3.  7.]
 [ 7.  9.  4. 10.  1.  2.  9.  8.  7.  8.  2.  8.]]
---------
[array([[ 9.,  5.,  8.,  6.],
       [ 7.,  9.,  4., 10.]]), array([[ 7., 10.,  8.,  8.],
       [ 1.,  2.,  9.,  8.]]), array([[3., 5., 3., 7.],
       [7., 8., 2., 8.]])]
---------
[array([[9., 5., 8.],
       [7., 9., 4.]]), array([[ 6.],
       [10.]]), array([[ 7., 10.,  8.,  8.,  3.,  5.,  3.,  7.],
       [ 1.,  2.,  9.,  8.,  7.,  8.,  2.,  8.]])]

python中找到索引的值

import numpy as np
x = np.floor(10 * np.random.random((2, 12)) + 1)
print(x)
inx = x.argmax(axis = 0)
print(inx)
print(range(x.shape[1]))
x_max = x[inx, range(x.shape[1])]
print(x_max)

[[ 3.  8.  2.  8.  8.  3.  4.  9.  9. 10.  8.  2.]
 [ 7.  6.  3.  9.  8.  9.  7.  7.  9.  3. 10.  9.]]
[1 0 1 1 0 1 1 0 0 0 1 1]
range(0, 12)
[ 7.  8.  3.  9.  8.  9.  7.  9.  9. 10. 10.  9.]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值