Numpy库的学习

Numpy库的学习,第1张

Numpy库
  • Numpy.squeeze()
  • Numpy.linspace()


Numpy.squeeze()

定义:移除指定数据中长度为 1 的轴;
形式:numpy.squeeze(a, axis=None);
参数:a 是输入的数据;axis 目前我也就用到 int,用于删除指定维度的轴,该轴长应当为 1,否则会引发错误。

>>> import numpy as np
>>> a = np.arange(3).reshape(1,3,1)
>>> print(a)
[[[0]
  [1]
  [2]]]
>>> b = np.squeeze(a)
b 变成了一个数组
>>> print(b,b.shape)
[0 1 2] (3,)
把 c 的第 0 维的轴长为 1 的轴去掉
>>> c = np.squeeze(a,0)
>>> print(c,c.shape)
[[0]
 [1]
 [2]] (3, 1)
>>> d = np.array([[666]])
np.squeeze(d) 返回的一个的数组,只不过是一个数字而已
>>> print(np.squeeze(d))
666
>>> print(type(np.squeeze(d)))
<class 'numpy.ndarray'>
>>> print(np.squeeze(d)[()])
666
>>> print(type(np.squeeze(d)[()]))
<class 'numpy.int32'>

Numpy.linspace()

参数定义:numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

  • endpoint : bool, optional.If True, stop is the last sample. Otherwise, it is not included. Default is True.

函数定义:
Return evenly spaced numbers over a specified interval.
在指定的间隔内返回均匀分布的数字

Returns num evenly spaced samples, calculated over the interval [start, stop].
在 [start, stop] 个区间内返回 num 个均匀分布的数字

The endpoint of the interval can optionally be excluded.
间隔的端点可以有选择性地选择是否包括在内。

>>> import numpy as np
>>> a = np.linspace(0,1,num=5,endpoint=False)
endpoint 默认是 True ,如果需要的话,要显式地声明为 False
>>> print(a) 
[0.  0.2 0.4 0.6 0.8]
>>> np.linspace(0,1,num=5)
array([0.  , 0.25, 0.5 , 0.75, 1.  ])
>>> np.linspace(0,1,num=5,endpoint=False,retstep=True)
(array([0. , 0.2, 0.4, 0.6, 0.8]), 0.2)
>>> a1,b1 = np.linspace(0,1,5,endpoint=False,retstep=True)
>>> print(a1,b1)
[0.  0.2 0.4 0.6 0.8] 0.2

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/715560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存