numpy 学习笔记

np.ceil(x)

对x中所有元素向上取整,

Causion:输出元素都为‘float64’

>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
>>> np.ceil(a)
array([-1., -1., -0., 1., 2., 2., 2.])

np.floor(x)

对x中所有元素向下取整,

Causion:输出元素都为‘float64’

np.random.permutation(x)

随机掉换序列x中各元素的顺序,

x为integer时,返回打乱的range(x)的序列;

x为1-array时,返回elemet被打乱的array;

x为n-array时,返回axis=0被打乱的array;

>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])

>>> np.random.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12])

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8],
       [0, 1, 2],
       [3, 4, 5]])

  

np.squeeze(x)

删除x中的单维条目

如果写成x.squeeze(n),则为删除x数组中的第n维,前提是第n维大小为1

>>> x = np.random.randn(1,3,4,1,3,1)
>>> print(x.shape)
(1, 3, 4, 1, 3, 1)
>>> y = np.squeeze(x)  # 从数组的形状中删除单维条目,即把shape中为1的维度去掉 
>>> print(y.shape)
(3, 4, 3)
>>> x1 = x.squeeze(0) 
>>> print(x1.shape)
(3, 4, 1, 3, 1)
>>> x2 = x.squeeze(1) 
>>> print(x1.shape)
ValueError: cannot select an axis to squeeze out which has size not equal to one

  

原文地址:https://www.cnblogs.com/lizhiqing/p/11230906.html