numpy是python中常用的库
import numpy as np
目录
1、numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
–>返回指定区域内的均匀分布的数字,默认包含端点。
–>endpoint默认stop是最后一个点,选择为False是不包含。
–>如果retstep是True,返回 (samples, step),step是间隔。
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Return evenly spaced numbers over a specified interval. #返回指定区间内间隔均匀的数字
Returns num evenly spaced samples, calculated over the interval [start, stop]. #返回num个间隔均匀的样本,计算间隔[开始,停止]
The endpoint of the interval can optionally be excluded. #区间的端点可以选择排除
-->np.linspace(2.0, 3.0, num=5)
array([2. , 2.25, 2.5 , 2.75, 3. ])
-->np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
-->np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
2、numpy.digitize(x, bins, right=False)
返回输入数组x中每个值对应bins的索引,right表示是否包含右边边界(左右边界只有一个包含,因此可以通过一个参数控制左右边界)
Return the indices of the bins to which each value in input array belongs.
注意!这个索引不是对应值,而是一个区间
bins是上升数组时:
right=False —> bins[i-1] <= x < bins[i]
right=True —> bins[i-1] < x <= bins[i]
bins是下降数组时:
right=False —> bins[i-1] > x >= bins[i]
right=True —> bins[i-1] >= x > bins[i]
x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
inds = np.digitize(x, bins)
inds
array([1, 4, 3, 2])
3、np.array()
创建数组
a = np.array([2,3,4])
b = np.array([2.0,3.0,4.0])
c = np.array([[1.0,2.0],[3.0,4.0]])
d = np.array([[1,2],[3,4]],dtype=complex) # 指定数据类型
print a, a.dtype
print b, b.dtype
print c, c.dtype
print d, d.dtype
[2 3 4] int32
[ 2. 3. 4.] float64
[[ 1. 2.]
[ 3. 4.]] float64
[[ 1.+0.j 2.+0.j]
[ 3.+0.j 4.+0.j]] complex128
shape[0]读取矩阵第一维度的长度
.shape可以快速读取矩阵的形状
4、np.squeeze(tmp_ts)
从数组的形状中删除单维度条目,即把shape中为1的维度去掉
numpy.squeeze(a,axis = None)
1)a表示输入的数组;
2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
4)返回值:数组
5) 不会修改原数组;
np.squeeze()函数可以删除数组形状中的单维度条目,即把shape中为1的维度去掉,但是对非单维的维度不起作用。
5、增加维度
增加某一维度,只需要加入None即可
>>> a = np.array([2,3,4])
>>> a.shape
(3,)
>>> a = a[:,None]
>>> a.shape
(3,1)
>>> a
array([[2],
[3],
[4]])
6、data_path[-1]
data_path[-1]表示最后一位
7、np.prod函数
numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<class ‘numpy._globals._NoValue’>)
返回给定轴上的数组元素的乘积。