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

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)Meanhttps://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
np.median(array)Medianhttps://numpy.org/doc/stable/reference/generated/numpy.median.html#numpy.median
array.corrcoef()Correlation Coefficienthttps://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef
np.std(array)Standard Deviationhttps://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和 2https://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))

生成矩阵和切片图示

f4c9108c02957a1983c34876ba3ee294.png

05fe22511daa8ec80457e95f734e11e5.png

小技巧

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

布尔索引 

# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Leetcode算法小抄是一份权威的算法手册,包了Leetcode上常见的算法题目的解法和详细讲解。这个小抄对于想要提升自己算法能力的程序员来说非常有用。该小抄包括以下内容: 1.基础数据结构:包括数组、链、栈、队列、树、哈希等。 2.算法基础:包括排序算法、搜索算法、贪心算法、动态规划等。 3.高级算法:包括图论、字符串匹配、线性代数、计算几何等。 每个算法题目都附有详细的解析和代码实现,方便程序员进行学习和练习。此外,该小抄还提供了优秀的算法实现其他程序员的思路和解答,这对于新手来说尤为重要。 总之,Leetcode算法小抄是一份非常实用的算法手册,如果你想成为一名出色的程序员,学习和掌握其中的内容必不可少。 ### 回答2: LeetCode算法小抄是一份非常实用的算法指南,它包了大量的算法问题和解答,而且所有的算法问题都是以LeetCode网站上的题目为蓝本的。这个小抄主要面向准备参加Google、Facebook、 Apple等知名科技公司的笔试或者面试的程序员,也适用于想要提高自己算法能力的人。这份小抄的编制者是Steven Halim和Felix Halim,也就是ACM竞赛的著名选手和教练。他们将自己多年的ACM竞赛经验倾囊相授,帮助大家提高算法能力。小抄中包了高频出现的数据结构和算法,如树、图、排序、数组、动态规划等,每个算法都有详细的解释和代码实现。此外,小抄还包了一些实用技巧,如测试用例设计、代码调试、复杂度分析等。总之,LeetCode算法小抄是一份非常实用、全面的算法指南,如果你想要提高自己的算法能力,相信它一定能为你带来帮助。 ### 回答3: LeetCode算法小抄是一个常用的算法学习工具,它主要是为了帮助程序员更加高效地学习和掌握LeetCode算法。LeetCode算法小抄中收录了大量经典的算法题目,并提供了详细的题解和代码示例,涵盖了各种数据结构、算法和编程技术。 LeetCode算法小抄的优点在于它的简便性和针对性。其内容结构清晰,难度逐渐增加,让读者能够逐步学习并掌握更加复杂的数据结构和算法。同时,小抄中提供了大量的代码示例和优化方法,可以帮助读者更加深入地理解和掌握算法。 另外,LeetCode算法小抄还提供了各种算法题目的分类、标签和解法推荐,让读者能够更加容易地找到自己需要的题目和解法。同时,小抄中还提供了一些常见的面试题目和解题思路,可以帮助读者更好地应对工作中和面试中的挑战。 总之,LeetCode算法小抄是一本非常实用的算法学习工具,它可以帮助程序员更加高效地学习和掌握算法知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值