官方文档:https://numpy.org/doc/stable/
¶ numpy.array
¶ 从python list创建
1, 2, 3]) np.array([
¶ 从多个python list创建一维数组
如果是确定数量的list,可以用np.concatenate
:
= [1, 2, 3]
a = [4, 5, 6]
b np.concatenate((a, b))
输出:array([1, 2, 3, 4, 5, 6])
来源:https://stackoverflow.com/a/54773471/13688160
如果有不定数量的list,用迭代器:
= [[1, 2, 3], [4, 5, 6]]
listOfLists for singleList in listOfLists for elem in singleList]) np.array([ elem
输出:
array([1, 2, 3, 4, 5, 6])
¶ 等差数列
文档:https://numpy.org/doc/stable/reference/generated/numpy.linspace.html
=50, endpoint=True, retstep=False, dtype=None, axis=0) numpy.linspace(start, stop, num
¶ 二分搜索
https://numpy.org/doc/stable/reference/generated/numpy.searchsorted.html
left相当于lower_bound, right相当于upper_bound, 默认是left。
¶ 矩阵
¶ percentile
https://numpy.org/doc/stable/reference/generated/numpy.percentile.html#
# 10000个0到1的随机数
= np.random.rand(10000)
a # 求20%分位数,即小于此数的值的数量占总数的20%
20)
np.percentile(a, # 求多个分位数
10, 20, 30, 40, 50, 60, 70, 80, 90, 99, 99.9, 99.99]) np.percentile(a, [