知行编程网知行编程网  2022-06-13 16:00 知行编程网 隐藏边栏 |   抢沙发  3 
文章评分 0 次,平均分 0.0

Numpy是一个用python实现的科学计算的扩展程序库,包括:
  • 1、一个强大的N维数组对象Array;
  • 2、比较成熟的(广播)函数库;
  • 3、用于整合C/C++和Fortran代码的工具包;
  • 4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。
NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用,以及核心的科学计算组织如:Lawrence Livermore,NASA用其处理一些本来使用C++,Fortran或Matlab等所做的任务。
本文整理了一个Numpy的小抄表,总结了Numpy的常用操作,可以收藏慢慢看。


   安装Numpy


可以通过 Pip 或者 Anaconda安装Numpy:


   本文目录

  1. 基础
  2. 数组
  3. 数学计算
  4. 切片和子集
  5. 小技巧


   基础


NumPy最常用的功能之一就是NumPy数组:列表和NumPy数组的最主要区别在于功能性和速度。


列表提供基本操作,但NumPy添加了FTTs、卷积、快速搜索、基本统计、线性代数、直方图等。


两者数据科学最重要的区别是能够用NumPy数组进行元素级计算。

<span style="font-size: 16px;">axis 0</span> 通常指行

axis 1 通常指列


操作 描述 文档
<span style="font-size: 14px;">np.array([1,2,3])</span> 一维数组 https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
<span style="font-size: 14px;">np.array([(1,2,3),(4,5,6)])</span> 二维数组 https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
<span style="font-size: 14px;">np.arange(start,stop,step)</span> 等差数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html


占位符


操作

描述

文档

<span style="font-size: 14px;">np.linspace(0,2,9)</span>

数组中添加等差的值

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

<span style="font-size: 14px;">np.zeros((1,2))</span>

创建全0数组

docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html

<span style="font-size: 14px;">np.ones((1,2))</span>

创建全1数组

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html#numpy.ones

<span style="font-size: 14px;">np.random.random((5,5))</span>

创建随机数的数组

https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html

<span style="font-size: 14px;">np.empty((2,2))</span>

创建空数组

https://numpy.org/doc/stable/reference/generated/numpy.empty.html


举例:


   数组


数组属性


语法 描述 文档
<span style="font-size: 14px;">array.shape</span> 维度(行,列) https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html
<span style="font-size: 14px;">len(array)</span> 数组长度 https://docs.python.org/3.5/library/functions.html#len
<span style="font-size: 14px;">array.ndim</span> 数组的维度数 https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ndim.html
<span style="font-size: 14px;">array.size</span> 数组的元素数 https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.size.html
<span style="font-size: 14px;">array.dtype</span> 数据类型 https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
<span style="font-size: 14px;">array.astype(type)</span> 转换数组类型 https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html
<span style="font-size: 14px;">type(array)</span> 显示数组类型 https://numpy.org/doc/stable/user/basics.types.html


拷贝 /排序


操作 描述 文档
<span style="font-size: 14px;">np.copy(array)</span> 创建数组拷贝 https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html
<span style="font-size: 14px;">other = array.copy()</span> 创建数组深拷贝 https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html
<span style="font-size: 14px;">array.sort()</span> 排序一个数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
<span style="font-size: 14px;">array.sort(axis=0)</span> 按照指定轴排序一个数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html

举例

数组操作例程

增加或减少元素

操作 描述 文档
<span style="font-size: 14px;">np.append(a,b)</span> 增加数据项到数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
<span style="font-size: 14px;">np.insert(array, 1, 2, axis)</span> 沿着数组0轴或者1轴插入数据项 https://docs.scipy.org/doc/numpy/reference/generated/numpy.insert.html
<span style="font-size: 14px;">np.resize((2,4))</span> 将数组调整为形状(2,4) https://docs.scipy.org/doc/numpy/reference/generated/numpy.resize.html
<span style="font-size: 14px;">np.delete(array,1,axis)</span> 从数组里删除数据项 https://numpy.org/doc/stable/reference/generated/numpy.delete.html

举例

组合数组

操作 描述 文档
<span style="font-size: 14px;">np.concatenate((a,b),axis=0)</span> 连接2个数组,添加到末尾 https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html
<span style="font-size: 14px;">np.vstack((a,b))</span> 按照行堆叠数组 https://numpy.org/doc/stable/reference/generated/numpy.vstack.html
<span style="font-size: 14px;">np.hstack((a,b))</span> 按照列堆叠数组 docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html#numpy.hstack

举例

分割数组


操作

描述

文档

<span style="font-size: 14px;">numpy.split()</span>

分割数组

https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html

<span style="font-size: 14px;">np.array_split(array, 3)</span>

将数组拆分为大小(几乎)相同的子数组

https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_split.html#numpy.array_split

<span style="font-size: 14px;">numpy.hsplit(array, 3)</span>

在第3个索引处水平拆分数组

https://numpy.org/doc/stable/reference/generated/numpy.hsplit.html#numpy.hsplit

举例

数组形状变化

操作 描述 文档
<span style="font-size: 14px;">other = ndarray.flatten()</span> 平铺一个二维数组到一维数组 https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
numpy.flip() 翻转一维数组中元素的顺序 https://docs.scipy.org/doc/stable/reference/generated/numpy.flip.html
np.ndarray[::-1] 翻转一维数组中元素的顺序
reshape 改变数组的维数 https://docs.scipy.org/doc/stable/reference/generated/numpy.reshape.html
squeeze 从数组的形状中删除单维度条目 https://numpy.org/doc/stable/reference/generated/numpy.squeeze.html
expand_dims 扩展数组维度
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.expand_dims.html

其他

操作 描述 文档
<span style="font-size: 14px;">other = ndarray.flatten()</span> 平铺2维数组到1维数组 https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
<span style="font-size: 14px;">array = np.transpose(other)</span>
<span style="font-size: 14px;">array.T</span>
数组转置 https://numpy.org/doc/stable/reference/generated/numpy.transpose.html
<span style="font-size: 14px;">inverse = np.linalg.inv(matrix)</span> 求矩阵的逆矩阵 https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html


举例

   数学计算

操作

操作 描述 文档
<span style="font-size: 14px;">np.add(x,y)</span>
<span style="font-size: 14px;">x + y</span>
https://docs.scipy.org/doc/numpy/reference/generated/numpy.add.html
<span style="font-size: 14px;">np.substract(x,y)</span>
<span style="font-size: 14px;">x - y</span>
https://docs.scipy.org/doc/numpy/reference/generated/numpy.subtract.html#numpy.subtract
<span style="font-size: 14px;">np.divide(x,y)</span>
<span style="font-size: 14px;">x / y</span>
https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide
<span style="font-size: 14px;">np.multiply(x,y)</span>
<span style="font-size: 14px;">x * y</span>
https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html#numpy.multiply
<span style="font-size: 14px;">np.sqrt(x)</span> 平方根 https://docs.scipy.org/doc/numpy/reference/generated/numpy.sqrt.html#numpy.sqrt
<span style="font-size: 14px;">np.sin(x)</span> 元素正弦 https://docs.scipy.org/doc/numpy/reference/generated/numpy.sin.html#numpy.sin
<span style="font-size: 14px;">np.cos(x)</span> 元素余弦 https://docs.scipy.org/doc/numpy/reference/generated/numpy.cos.html#numpy.cos
<span style="font-size: 14px;">np.log(x)</span> 元素自然对数 https://docs.scipy.org/doc/numpy/reference/generated/numpy.log.html#numpy.log
<span style="font-size: 14px;">np.dot(x,y)</span> 点积 https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
<span style="font-size: 14px;">np.roots([1,0,-4])</span> 给定多项式系数的根 https://docs.scipy.org/doc/numpy/reference/generated/numpy.roots.html


举例

比较

操作 描述 文档
<span style="font-size: 14px;">==</span> 等于 https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;">!=</span> 不等于
https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;"><</span> 小于 https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;">></span> 大于 https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;"><=</span> 小于等于 https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;">>=</span> 大于等于 https://docs.python.org/2/library/stdtypes.html
<span style="font-size: 14px;">np.array_equal(x,y)</span> 数组比较 https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html

举例:

基本的统计

操作 描述 文档
<span style="font-size: 14px;">np.mean(array)</span> Mean https://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
<span style="font-size: 14px;">np.median(array)</span> Median https://numpy.org/doc/stable/reference/generated/numpy.median.html#numpy.median
<span style="font-size: 14px;">array.corrcoef()</span> Correlation Coefficient https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef
<span style="font-size: 14px;">np.std(array)</span> Standard Deviation https://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html#numpy.std


举例

更多

操作 描述 文档
<span style="font-size: 14px;">array.sum()</span> 数组求和 https://numpy.org/doc/stable/reference/generated/numpy.sum.html
<span style="font-size: 14px;">array.min()</span> 数组求最小值 https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
<span style="font-size: 14px;">array.max(axis=0)</span> 数组求最大值(沿着0轴)
<span style="font-size: 14px;">array.cumsum(axis=0)</span> 指定轴求累计和 https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html


   切片和子集


操作 描述 文档
<span style="font-size: 14px;">array[i]</span> 索引i处的一维数组 https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[i,j]</span> 索引在[i][j]处的二维数组 https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[i<4]</span> 布尔索引 https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[0:3]</span> 选择索引为 0, 1和 2 https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[0:2,1]</span> 选择第0,1行,第1列 https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[:1]</span> 选择第0行数据项 (与[0:1, :]相同) https://numpy.org/doc/stable/reference/arrays.indexing.html
<span style="font-size: 14px;">array[1:2, :]</span> 选择第1行 https://numpy.org/doc/stable/reference/arrays.indexing.html
[comment]: <> " <span style="font-size: 14px;">array[1,...]</span> 等同于 array[1,:,:]
<span style="font-size: 14px;">array[ : :-1]</span> 反转数组 同上


举例


切片举例



生成矩阵和切片图示


简约而不简单!值得收藏的 NumPy 小抄表(含主要语法、代码)

简约而不简单!值得收藏的 NumPy 小抄表(含主要语法、代码)



   小技巧


例子将会越来越多的,欢迎大家提交。

布尔索引 


【参考】

https://github.com/juliangaal/python-cheat-sheet


<pre style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section><section style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;text-align: center;margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><section style="margin: 5px 0px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">长尾分布下图像分类问题最新综述(2019-2020)</section><section style="margin: 5px 0px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">GitHub重大更新:在线开发上线,是时候卸载IDE了</span></section><section style="margin: 5px 0px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">特朗普拿H1B签证开刀,LeCun吴恩达等实名谴责!</section><section style="margin: 5px 0px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">数据分析入门常用的23个牛逼Pandas代码</section><section style="margin: 5px 0px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(87, 107, 149);font-size: 14px;">如何在科研论文中画出漂亮的插图?</span><br  /></section></section></section></section></section></section></section></section></section>
简约而不简单!值得收藏的 NumPy 小抄表(含主要语法、代码)

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享