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])

相关内容

热门资讯

同样卖奶茶,为何古茗赚了15亿... 出品/联商专栏 撰文/老刀 编辑/蔡建桢 今年上半年,蜜雪、古茗、霸王茶姬、茶百道、沪上阿姨全部盈利...
现烤散装走俏、新口味亮相、健康... 中秋佳节临近,成都月饼市场已全面开启“上新季”。记者连日来走访成都多家商超看到,月饼专区占据卖场核心...
散装月饼靠性价比当上“人气王” 华润超市货架上的散装月饼。 深圳报业集团记者 欧阳莹 摄 深圳晚报讯 (深圳报业集团记者 欧阳莹) ...
原创 9... 九月初秋,燥气渐起,容易咽喉干痒、口鼻缺水、干咳烦闷。多喝清润汤水,滋养肺阴,补足身体津液,不寒不腻...
原创 生... 山药是秋季深藏不露的养生食材,也是为数不多 生用补肾、熟用健脾 的平价好物。生山药滋阴补肾、固护肾精...