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

相关内容

热门资讯

地方新闻精选 | 杭州宣布灵隐... 【浙江】杭州宣布灵隐寺12月1日起免门票,需至少提前一天预约11月19日,中国蓝新闻记者从浙江省杭州...
从山海古城到青春乐场,日照的滨... 中新网日照11月19日电(记者 左宇坤)深秋时节,山东日照莒县浮来山上的“天下银杏第一树”迎来一年中...
重构温泉体验:项目实践与发展路... 传统温泉同质化、体验形式单一的问题日益凸显,难以满足当下游客对个性化、沉浸式、多功能消费的需求。随着...
原创 非... 面对急需帮助的人,我们会先选择帮助,还是先拍照呢?如果这是发生在10年前,肯定不用多想,大家一定会第...