Numpy库的学习

定义:移除指定数据中长度为 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(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

Original: https://blog.csdn.net/Mr_Yuwen_Yin/article/details/124230913
Author: 硕欧巴
Title: Numpy库的学习

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/759288/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球