numpy学习笔记
官方文档:https://numpy.org/doc/stable/
numpy.array
从python list创建
np.array([1, 2, 3])
从多个python list创建一维数组
如果是确定数量的list,可以用np.concatenate
:
a = [1, 2, 3]
b = [4, 5, 6]
np.concatenate((a, b))
输出:array([1, 2, 3, 4, 5, 6])
来源:https://stackoverflow.com/a/54773471/13688160
如果有不定数量的list,用迭代器:
listOfLists = [[1, 2, 3], [4, 5, 6]]
np.array([ elem for singleList in listOfLists for elem in singleList])
输出:
array([1, 2, 3, 4, 5, 6])
等差数列
文档:https://numpy.org/doc/stable/reference/generated/numpy.linspace.html
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
二分搜索
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的随机数
a = np.random.rand(10000)
# 求20%分位数,即小于此数的值的数量占总数的20%
np.percentile(a, 20)
# 求多个分位数
np.percentile(a, [10, 20, 30, 40, 50, 60, 70, 80, 90, 99, 99.9, 99.99])
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律