NumPy is the library that gives Python its ability to work with data at speed. Originally, launched in 1995 as ‘Numeric,’ NumPy is the foundation on which man importany Python data science libraries are built, including Pandas, SciPy and scikit-learn.
NumPy是使Python能够快速处理数据的库。 NumPy最初于1995年以“ Numeric”的形式推出,是人类建立任何Python数据科学库(包括Pandas,SciPy和scikit-learn)的基础。
The printable version of this cheat sheet
该备忘单的可打印版本
It’s common when first learning NumPy to have trouble remembering all the functions and methods that you need, and while at Dataquest we advocate getting used to consulting the NumPy documentation, sometimes it’s nice to have a handy reference, so we’ve put together this cheat sheet to help you out!
初次学习NumPy时会很难记住您需要的所有功能和方法,这是很常见的,而在Dataquest中,我们提倡习惯于查阅NumPy文档 ,有时可以很方便地获得参考,因此我们将这种作弊放在一起表来帮助你!
If you’re interested in learning pandas, you can consult our NumPy tutorial blog post, or you can signup for free and start learning NumPy through our interactive python data science course.
如果您对学习熊猫感兴趣,可以查阅我们的NumPy教程博客文章,或者免费注册并通过我们的交互式python数据科学课程开始学习NumPy。
Download a Printable PDF of this Cheat Sheet
密钥和导入 (Key and Imports)
In this cheat sheet, we use the following shorthand:
在本备忘单中,我们使用以下速记:
arr arr | A NumPy Array object | NumPy数组对象 |
You’ll also need to import numpy to get started:
您还需要导入numpy才能开始:
import numpy as np
import numpy as np
导入/导出 (Importing/exporting)
np.loadtxt('file.txt') np.loadtxt('file.txt') | From a text file | 来自文本文件 |
np.genfromtxt('file.csv',delimiter=',') np.genfromtxt('file.csv',delimiter=',') | From a CSV file | 从CSV文件 |
np.savetxt('file.txt',arr,delimiter=' ') np.savetxt('file.txt',arr,delimiter=' ') | Writes to a text file | 写入文本文件 |
np.savetxt('file.csv',arr,delimiter=',') np.savetxt('file.csv',arr,delimiter=',') | Writes to a CSV file | 写入CSV文件 |
创建数组 (Creating Arrays)
np.array([1,2,3]) np.array([1,2,3]) | One dimensional array | 一维数组 |
np.array([(1,2,3),(4,5,6)]) np.array([(1,2,3),(4,5,6)]) | Two dimensional array | 二维数组 |
np.zeros(3) np.zeros(3) | 3 x3 x 1 array with all values 1 数组,所有值0 0 | |
np.ones((3,4)) np.ones((3,4)) | 3 x3 x 4 array with all values 4 数组,所有值1 1 | |
np.eye(5) np.eye(5) | 5 x5 x 5 array of 5 的0 with 0 数组,对角线为1 on diagonal (Identity matrix)1 (恒等矩阵) | |
np.linspace(0,100,6) np.linspace(0,100,6) | 6 evenly divided values from 0 到0 to 100 的100 6 分值数组 | |
np.arange(0,10,3) np.arange(0,10,3) | 0 to less than 3 从10 with step 0 到小于3 (eg 10 的值数组(例如[0,3,6,9] )[0,3,6,9] ) | |
np.full((2,3),8) np.full((2,3),8) | 2 x2 x 3 array with all values 3 数组,所有值8 8 | |
np.random.rand(4,5) np.random.rand(4,5) | 4 x4 X 5 array of random floats between 5 阵列随机浮子之间0 –0 - 1 1 | |
np.random.rand(6,7)*100 np.random.rand(6,7)*100 | 6 x6 x 7 array of random floats between 7 随机浮动数组,介于0 –0 – 100 100 之间 | |
np.random.randint(5,size=(2,3)) np.random.randint(5,size=(2,3)) | 2 x2 x 3 array with random ints between 3 数组,随机整数介于0 –0 到4 4 之间 |
检查属性 (Inspecting Properties)
arr.size arr.size | arr arr 的元素数 |
arr.shape arr.shape | arr (rows,columns)arr 尺寸(行,列) |
arr.dtype arr.dtype | arr arr 中元素的类型 |
arr.astype(dtype) arr.astype(dtype) | arr elements to type arr 元素转换为dtype dtype 类型 |
arr.tolist() arr.tolist() | arr to a Python listarr 转换为Python列表 |
np.info(np.eye) np.info(np.eye) | np.eye np.eye 文档 |
复制/分类/整形 (Copying/sorting/reshaping)
np.copy(arr) np.copy(arr) | arr to new memoryarr 复制到新内存 |
arr.view(dtype) arr.view(dtype) | arr elements with type dtype 的dtype arr 元素的视图 |
arr.sort() arr.sort() | arr arr |
arr.sort(axis=0) arr.sort(axis=0) | arr arr 特定轴进行排序 |
two_d_arr.flatten() two_d_arr.flatten() | two_d_arr to 1Dtwo_d_arr 为1D |
arr.T arr.T | arr (rows become columns and vice versa)arr (行变为列,反之亦然) |
arr.reshape(3,4) arr.reshape(3,4) | arr to arr 重塑为3 rows, 3 行, 4 columns without changing data4 列而不更改数据 |
arr.resize((5,6)) arr.resize((5,6)) | arr shape to arr 形状更改为5 x5 x 6 and fills new values with 6 并用0 0 填充新值 |
添加/删除元素 (Adding/removing Elements)
np.append(arr,values) np.append(arr,values) | arr arr 末尾 |
np.insert(arr,2,values) np.insert(arr,2,values) | arr before index 2 之前的2 arr 插入值 |
np.delete(arr,3,axis=0) np.delete(arr,3,axis=0) | 3 of arr 索引arr 3 的行 |
np.delete(arr,4,axis=1) np.delete(arr,4,axis=1) | 4 of arr 索引arr 4 的列 |
合并/拆分 (Combining/splitting)
np.concatenate((arr1,arr2),axis=0) np.concatenate((arr1,arr2),axis=0) | arr2 as rows to the end of arr2 作为行添加到arr1 arr1 的末尾 |
np.concatenate((arr1,arr2),axis=1) np.concatenate((arr1,arr2),axis=1) | arr2 as columns to end of arr2 作为列添加到arr1 arr1 末尾 |
np.split(arr,3) np.split(arr,3) | arr into arr 分成3 sub-arrays3 个子数组 |
np.hsplit(arr,5) np.hsplit(arr,5) | arr horizontally on the 5 个索引上水平分割5 th indexarr |
索引/切片/子集 (Indexing/slicing/subsetting)
arr[5] arr[5] | 5 5 的元素 | |
arr[2,5] arr[2,5] | [2][5] [2][5] 上的二维数组元素 | |
arr[1]=4 arr[1]=4 | 1 the value 1 的数组元素分配值4 4 | |
arr[1,3]=10 arr[1,3]=10 | [1][3] the value [1][3] 上的数组元素分配值10 10 | |
arr[0:3] arr[0:3] | 0,1,2 (On a 2D array: returns rows 0,1,2 的元素(在2D数组上:返回行0,1,2 )0,1,2 ) | |
arr[0:3,4] arr[0:3,4] | 0,1,2 at column 4 列的第4 0,1,2 行的元素 | |
arr[:2] arr[:2] | 0,1 (On a 2D array: returns rows 0,1 的元素(在2D数组上:返回行0,1 )0,1 ) | |
arr[:,1] arr[:,1] | 1 on all rows1 的元素 | |
arr<5 arr<5 | Returns an array with boolean values | 返回具有布尔值的数组 |
(arr1<3) & (arr2>5) (arr1<3) & (arr2>5) | Returns an array with boolean values | 返回具有布尔值的数组 |
~arr ~arr | Inverts a boolean array | 反转布尔数组 |
arr[arr<5] arr[arr<5] | 5 5 数组元素 |
标量数学 (Scalar Math)
np.add(arr,1) np.add(arr,1) | 1 to each array element1 |
np.subtract(arr,2) np.subtract(arr,2) | 2 from each array element2 |
np.multiply(arr,3) np.multiply(arr,3) | 3 3 |
np.divide(arr,4) np.divide(arr,4) | 4 (returns 4 (返回np.nan for division by zero)np.nan 以除以零) |
np.power(arr,5) np.power(arr,5) | 5 th power5 次方 |
向量数学 (Vector Math)
np.add(arr1,arr2) np.add(arr1,arr2) | arr2 to arr2 添加到arr1 arr1 | |
np.subtract(arr1,arr2) np.subtract(arr1,arr2) | arr2 from arr1 逐元素减去arr1 arr2 | |
np.multiply(arr1,arr2) np.multiply(arr1,arr2) | arr1 by arr1 乘以arr2 arr2 | |
np.divide(arr1,arr2) np.divide(arr1,arr2) | arr1 by arr1 除以arr2 arr2 | |
np.power(arr1,arr2) np.power(arr1,arr2) | arr1 raised to the power of arr1 到arr2 arr2 的幂 | |
np.array_equal(arr1,arr2) np.array_equal(arr1,arr2) | True if the arrays have the same elements and shapeTrue | |
np.sqrt(arr) np.sqrt(arr) | Square root of each element in the array | 数组中每个元素的平方根 |
np.sin(arr) np.sin(arr) | Sine of each element in the array | 数组中每个元素的正弦 |
np.log(arr) np.log(arr) | Natural log of each element in the array | 数组中每个元素的自然对数 |
np.abs(arr) np.abs(arr) | Absolute value of each element in the array | 数组中每个元素的绝对值 |
np.ceil(arr) np.ceil(arr) | Rounds up to the nearest int | 四舍五入到最接近的整数 |
np.floor(arr) np.floor(arr) | Rounds down to the nearest int | 舍入到最接近的整数 |
np.round(arr) np.round(arr) | Rounds to the nearest int | 舍入到最接近的整数 |
统计 (Statistics)
np.mean(arr,axis=0) np.mean(arr,axis=0) | Returns mean along specific axis | 返回沿特定轴的平均值 |
arr.sum() arr.sum() | arr arr 总和 | |
arr.min() arr.min() | arr arr 最小值 | |
arr.max(axis=0) arr.max(axis=0) | Returns maximum value of specific axis | 返回特定轴的最大值 |
np.var(arr) np.var(arr) | Returns the variance of array | 返回数组的方差 |
np.std(arr,axis=1) np.std(arr,axis=1) | Returns the standard deviation of specific axis | 返回特定轴的标准偏差 |
arr.corrcoef() arr.corrcoef() | Returns correlation coefficient of array | 返回数组的相关系数 |
下载此备忘单的可打印版本 (Download a printable version of this cheat sheet)
翻译自: https://www.pybloggers.com/2017/04/numpy-cheat-sheet-python-for-data-science/