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:

$ pip install numpy

$ conda install numpy

本文目录

  1. 基础

  • 占位符

  • 数组

    • 增加或减少元素

    • 合并数组

    • 分割数组

    • 数组形状变化

    • 拷贝 /排序

    • 数组操作

    • 其他

  • 数学计算

    • 数学计算

    • 比较

    • 基础统计

    • 更多

  • 切片和子集

  • 小技巧

  • 基础

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

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

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

    axis 0 通常指行

    axis 1 通常指列

    操作 描述 文档
    np.array([1,2,3]) 一维数组 https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
    np.array([(1,2,3),(4,5,6)]) 二维数组 https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
    np.arange(start,stop,step) 等差数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

    占位符

    操作 描述 文档
    np.linspace(0,2,9) 数组中添加等差的值 https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
    np.zeros((1,2)) 创建全0数组 docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html
    np.ones((1,2)) 创建全1数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html#numpy.ones
    np.random.random((5,5)) 创建随机数的数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html
    np.empty((2,2)) 创建空数组 https://numpy.org/doc/stable/reference/generated/numpy.empty.html

    举例:

    import numpy as np# 1 dimensional
    x = np.array([1,2,3])
    # 2 dimensional
    y = np.array([(1,2,3),(4,5,6)])x = np.arange(3)
    >>> array([0, 1, 2])y = np.arange(3.0)
    >>> array([ 0.,  1.,  2.])x = np.arange(3,7)
    >>> array([3, 4, 5, 6])y = np.arange(3,7,2)
    >>> array([3, 5])
    

    数组属性

    数组属性

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

    拷贝 /排序

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

    举例

    import numpy as np
    # Sort sorts in ascending order
    y = np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
    y.sort()
    print(y)
    >>> [ 1  2  3  4  5  6  7  8  9  10]
    

    数组操作例程

    增加或减少元素

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

    举例

    import numpy as np
    # Append items to array
    a = np.array([(1, 2, 3),(4, 5, 6)])
    b = np.append(a, [(7, 8, 9)])
    print(b)
    >>> [1 2 3 4 5 6 7 8 9]# Remove index 2 from previous array
    print(np.delete(b, 2))
    >>> [1 2 4 5 6 7 8 9]
    

    组合数组

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

    举例

    import numpy as np
    a = np.array([1, 3, 5])
    b = np.array([2, 4, 6])# Stack two arrays row-wise
    print(np.vstack((a,b)))
    >>> [[1 3 5][2 4 6]]# Stack two arrays column-wise
    print(np.hstack((a,b)))
    >>> [1 3 5 2 4 6]
    

    分割数组

    操作 描述 文档
    numpy.split() 分割数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html
    np.array_split(array, 3) 将数组拆分为大小(几乎)相同的子数组 https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_split.html#numpy.array_split
    numpy.hsplit(array, 3) 在第3个索引处水平拆分数组 https://numpy.org/doc/stable/reference/generated/numpy.hsplit.html#numpy.hsplit

    举例

    # Split array into groups of ~3
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    print(np.array_split(a, 3))
    >>> [array([1, 2, 3]), array([4, 5, 6]), array([7, 8])]
    

    数组形状变化

    操作
    操作 描述 文档
    other = ndarray.flatten() 平铺一个二维数组到一维数组 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

    其他

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

    举例

    # Find inverse of a given matrix
    >>> np.linalg.inv([[3,1],[2,4]])
    array([[ 0.4, -0.1],[-0.2,  0.3]])
    

    数学计算

    操作

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

    举例

    # If a 1d array is added to a 2d array (or the other way), NumPy
    # chooses the array with smaller dimension and adds it to the one
    # with bigger dimension
    a = np.array([1, 2, 3])
    b = np.array([(1, 2, 3), (4, 5, 6)])
    print(np.add(a, b))
    >>> [[2 4 6][5 7 9]]# Example of np.roots
    # Consider a polynomial function (x-1)^2 = x^2 - 2*x + 1
    # Whose roots are 1,1
    >>> np.roots([1,-2,1])
    array([1., 1.])
    # Similarly x^2 - 4 = 0 has roots as x=±2
    >>> np.roots([1,0,-4])
    array([-2.,  2.])
    

    比较

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

    举例:

    # Using comparison operators will create boolean NumPy arrays
    z = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    c = z < 6
    print(c)
    >>> [ True  True  True  True  True False False False False False]
    

    基本的统计

    操作 描述 文档
    np.mean(array) Mean https://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
    np.median(array) Median https://numpy.org/doc/stable/reference/generated/numpy.median.html#numpy.median
    array.corrcoef() Correlation Coefficient https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef
    np.std(array) Standard Deviation https://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html#numpy.std

    举例

    # Statistics of an array
    a = np.array([1, 1, 2, 5, 8, 10, 11, 12])# Standard deviation
    print(np.std(a))
    >>> 4.2938910093294167# Median
    print(np.median(a))
    >>> 6.5
    

    更多

    操作 描述 文档
    array.sum() 数组求和 https://numpy.org/doc/stable/reference/generated/numpy.sum.html
    array.min() 数组求最小值 https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
    array.max(axis=0) 数组求最大值(沿着0轴)
    array.cumsum(axis=0) 指定轴求累计和 https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html

    切片和子集

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

    举例

    b = np.array([(1, 2, 3), (4, 5, 6)])# The index *before* the comma refers to *rows*,
    # the index *after* the comma refers to *columns*
    print(b[0:1, 2])
    >>> [3]print(b[:len(b), 2])
    >>> [3 6]print(b[0, :])
    >>> [1 2 3]print(b[0, 2:])
    >>> [3]print(b[:, 0])
    >>> [1 4]c = np.array([(1, 2, 3), (4, 5, 6)])
    d = c[1:2, 0:2]
    print(d)
    >>> [[4 5]]

    切片举例

    import numpy as np
    a1 = np.arange(0, 6)
    a2 = np.arange(10, 16)
    a3 = np.arange(20, 26)
    a4 = np.arange(30, 36)
    a5 = np.arange(40, 46)
    a6 = np.arange(50, 56)
    a = np.vstack((a1, a2, a3, a4, a5, a6))
    

    生成矩阵和切片图示

    小技巧

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

    布尔索引

    # Index trick when working with two np-arrays
    a = np.array([1,2,3,6,1,4,1])
    b = np.array([5,6,7,8,3,1,2])# Only saves a at index where b == 1
    other_a = a[b == 1]
    #Saves every spot in a except at index where b != 1
    other_other_a = a[b != 1]
    
    import numpy as np
    x = np.array([4,6,8,1,2,6,9])
    y = x > 5
    print(x[y])
    >>> [6 8 6 9]# Even shorter
    x = np.array([1, 2, 3, 4, 4, 35, 212, 5, 5, 6])
    print(x[x < 5])
    >>> [1 2 3 4 4]

    【参考】

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

    往期精彩回顾适合初学者入门人工智能的路线及资料下载机器学习及深度学习笔记等资料打印机器学习在线手册深度学习笔记专辑《统计学习方法》的代码复现专辑
    AI基础下载机器学习的数学基础专辑获取一折本站知识星球优惠券,复制链接直接打开:https://t.zsxq.com/yFQV7am本站qq群1003271085。加入微信群请扫码进群:
    

简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码)相关推荐

  1. 【Python】简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码)

    Numpy是一个用python实现的科学计算的扩展程序库,包括: 1.一个强大的N维数组对象Array: 2.比较成熟的(广播)函数库: 3.用于整合C/C++和Fortran代码的工具包: 4.实用 ...

  2. Python Numpy知识 - 简约而不简单的 Numpy 小抄表(语法、代码)

    目录 前言 零.安装Numpy 一.Numpy基础 1.常用操作 2.占位符 二.数组 1.数组属性 2.拷贝 /排序 3.数组操作例程 (1)增加或减少元素 (2)组合数组 (3)分割数组 (4)数 ...

  3. 【Python】简约而不简单的Numpy小抄表(含主要语法、代码)

    Numpy是一个用python实现的科学计算的扩展程序库,包括: 1.一个强大的N维数组对象Array: 2.比较成熟的(广播)函数库: 3.用于整合C/C++和Fortran代码的工具包: 4.实用 ...

  4. 傅里叶变换表_Numpy库小抄表!主要语法和代码都在这里啦

    用户2769421 | 作者 腾讯云 云+社区 | 来源 Numpy是一个用python实现的科学计算的扩展程序库,包括: 一个强大的N维数组对象Array: 比较成熟的(广播)函数库: 用于整合C/ ...

  5. 简约而不简单|值得收藏的Pandas基本操作指南

    Pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.pandas提供了大量能使我们快 ...

  6. 小白学数据 | 28张小抄表大放送:Python,R,大数据,机器学习

    1. Python的数据科学快速入门指南 如果你刚入门Python,那么这张小抄表非常适合你.查看这份小抄表,你将获得循序渐进学习Python的指导.它提供了Python学习的必备包和一些有用的学习技 ...

  7. 小白学数据_|_28张小抄表大放送:Python,R,大数据,机器学习

    1. Python的数据科学快速入门指南 如果你刚入门Python,那么这张小抄表非常适合你.查看这份小抄表,你将获得循序渐进学习Python的指导.它提供了Python学习的必备包和一些有用的学习技 ...

  8. 300张小抄表搞定机器学习知识点:学习根本停不下来!

    入坑数据科学和人工智能的同学都知道,机器学习是一个集合了计算机.统计学和数学知识的交叉领域,除了日常练习,也需要很多枯燥的记忆和理解.单纯读书不容易串联概念,又容易忘记. 可能你和我一样,读了无数遍& ...

  9. 像背单词一样搞定机器学习关键概念:300张小抄表满足你的所有AI好奇

    入坑数据科学和人工智能的同学都知道,机器学习是一个集合了计算机.统计学和数学知识的交叉领域,除了日常练习,也需要很多枯燥的记忆和理解.单纯读书不容易串联概念,又容易忘记. 可能你和文摘菌一样,读了无数 ...

最新文章

  1. 全国计算机等级考试第3套,全国计算机等级考试四级计算机网络第3套试题
  2. 从天气项目看Spring Cloud微服务治理
  3. spring-data-rest-rec 复现分析 cve-2017-8046
  4. docker -v 文件夹下没有数据_详细!快速入门指南!Docker
  5. windows病毒和linux吗,与Windows相比,Linux很少感染病毒。()
  6. kotlin自定义View出现 java.lang.ClassNotFoundException
  7. linux如何创建备份文件,如何备份Linux 配置文件
  8. 经典面试题(三):ASP.NET部分----ASP.NET 页面之间传递值的几种方式
  9. Linux 下离线手动下载安装 C++ 开发环境
  10. 在虚拟机环境(CentOS7系统)下将kubernetes中部署服务成功,但在虚拟机外部无法访问到服务...
  11. 卷积神经网络卷积层BN层计算原理和卷积BN层融合
  12. xp系统打印机服务器报错,互联网要点:Win7系统连接XP共享打印机报错0X000004如何解决...
  13. CSS4day(圆角边框,阴影,浮动详解及其示例)
  14. 华硕路由器里的虚拟服务器在哪里,华硕RT-AC86U路由器怎么设置端口转发服务
  15. 上了 BI,B 就 I 了吗?
  16. 极大后验概率(MAP)- maximum a posteriori(转载)
  17. 有瓶颈设备的多级生产计划问题
  18. 弹性云服务器能起到什么作用?
  19. 如何成为一名合格的程序员?
  20. 邪恶心理学-真实面对谎言的本质

热门文章

  1. 数据库-统计信息相关资料
  2. 团队每日冲刺博客05
  3. 苹果浏览器移动端click事件延迟300ms的原因以及解决办法
  4. Getting Installation aborted (Status 7) ApplyParsePerms: lsetfilecon of /syst...【转】
  5. EOS page问题
  6. javascript-模板方法模式-提示框归一化插件
  7. linux入门基础——linux网络配置
  8. android adb server is out of date
  9. 过滤html标签 RemoveHTML
  10. python loop call soon_python3-asyncio 学习笔记 1 -- call_soon