//目录

numpy常用操作

numpy也可以说是随处可见了。

  •  ndarray(np.array),就相当于mxnet 里的ndarray一样,连名字都一样。
import numpy as np

# 创建
a = np.array([0,1,2])
print(a,a.dtype)
c = np.array([[0,1],[1,2],[2,3.0]])
print(c,c.dtype)

a = c.reshape(2,-1)
print(a)

print(np.arange(0,1,0.1))
print(np.linspace(0,1,12))
print(np.logspace(0,2,20))

s = "abcdefgh"
print(np.fromstring(s,dtype=np.int8))

# 存储
a = np.arange(10)
print(a[:-1])
print(a[1:-1:2])
print(a[5:1:-2])

# 共享
b = a[3:7]
print(b)
b[0] = -1
print(b)
print(a)

# 整数序列
x = np.arange(10,1,-1)
print(x)
print(x[[3,3,1,8]])
x[[3,5,1]] = -1,-2,-3
print(x)

# 布尔数组
x = np.arange(5,0,-1)
print(x)
print(x[np.array([True,False,True,False,False])])
x[[True,False,True,False,False]] = -1
print(x)

# 不手动产生True,False

x = np.random.rand(10)      # 0-1的随机数
print(x)
print(x>0.5)
print(x[x>0.5])
print(x[np.array(x>0.5,dtype=bool)])

# 广播机制
ans = np.arange(0,60,10).reshape(-1,1) + np.arange(0,6)
print(ans)

# 结构数组
persontype = np.dtype({
    'names':['name','age','weight'],
    'formats':['S32','i','f']
})

a = np.array([("Tree",12,100),("Dream",18,90)],dtype=persontype)
print(a[1])
print(a[1]['name'])
print(a.dtype)

 

posted @ 2018-12-15 15:44  小草的大树梦  阅读(194)  评论(0编辑  收藏  举报