numpy函数

数据创建

randint

创建随机整数array。

np.random.randint(10,size=(2,3))

randint(low, high=None, size=None, dtype='l')

low为必选参数:

  • 若有low与high,则返回两者之间的数据。[low, high)。
  • 若只有low,则返回数据在[0, low)之间。

返回:返回一个ndarray,或一个整数int(若没有给size参数时)。

np.random.randint(100,size=(1))
np.random.randint(100)

array([3])
21

形状变化

np.flatten()

压成一个向量。

数据处理

切片

In[2]: nega[[2,3],:]
Out[2]: 
array([[ 1612,  9400],
       [ 8885, 10883]], dtype=int32)

用列表[2,3]切片,取数据。

posi = posi[np.random.randint(posi_size, size=batch_size), :]

在posi_size=34109个数据中,随机取256个。
np.random.randint(posi_size, size=batch_size)产生256个随机整数,在[0,posi_size]之间,然后用这256个随机数索引posi边的数据。

np.sort()

np.sort(a, axis = 0)

>>> a = np.array([[3,7],[9,1]])
>>> np.sort(a)  #默认是按行排列的,即在vector内部排列元素
array([[3, 7],
       [1, 9]])
>>> np.sort(a, axis =  0) # axis=0,按列排
array([[3, 1],
       [9, 7]])
>>> np.sort(a, axis =  1)
array([[3, 7],
       [1, 9]])
posted @ 2021-03-02 21:50  zae  阅读(45)  评论(0编辑  收藏  举报