numpy函数总结
admin
2024-02-16 14:52:11

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公里,是一座红色的藏式高层建筑。佛阁的高层宗教建筑在藏区...
原创 5... 要知道,5月27日赵子豪在上海迪士尼的照片和短文在社交平台上被不少人热聊,他背着树懒卡通包,还配了句...
沉浸式露营体验!长春这家河畔休... 露营,作为一种亲近自然、放松身心的休闲方式,越来越受到人们的喜爱。然而,传统的露营需要准备大量的装备...
杭州龙井的茶,飘了旧香 杭州龙井寻香记 一、风里飘来的旧香 暮春的杭州总裹着一层湿润的绿,我原本只是趁着清明后的假期来散心,...