NumPy备忘单–数据科学Python

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。

密钥和导入 (Key and Imports)

In this cheat sheet, we use the following shorthand:

在本备忘单中,我们使用以下速记:

arrarr 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) 3x3 x 1 array with all values 1数组,所有值00
np.ones((3,4))np.ones((3,4)) 3x3 x 4 array with all values 4数组,所有值11
np.eye(5)np.eye(5) 5x5 x 5 array of 50 with 0数组,对角线为1 on diagonal (Identity matrix)1 (恒等矩阵)
np.linspace(0,100,6)np.linspace(0,100,6) 6 evenly divided values from 00 to 1001006分值数组
np.arange(0,10,3)np.arange(0,10,3) 0 to less than 310 with step 0到小于3 (eg 10的值数组(例如[0,3,6,9])[0,3,6,9]
np.full((2,3),8)np.full((2,3),8) 2x2 x 3 array with all values 3数组,所有值88
np.random.rand(4,5)np.random.rand(4,5) 4x4 X 5 array of random floats between 5阵列随机浮子之间00 - 11
np.random.rand(6,7)*100np.random.rand(6,7)*100 6x6 x 7 array of random floats between 7随机浮动数组,介于00100100之间
np.random.randint(5,size=(2,3))np.random.randint(5,size=(2,3)) 2x2 x 3 array with random ints between 3数组,随机整数介于0044之间

检查属性 (Inspecting Properties)

arr.sizearr.size arrarr的元素数
arr.shapearr.shape arr (rows,columns)arr尺寸(行,列)
arr.dtypearr.dtype arrarr中元素的类型
arr.astype(dtype)arr.astype(dtype) arr elements to type arr元素转换为dtypedtype类型
arr.tolist()arr.tolist() arr to a Python listarr转换为Python列表
np.info(np.eye)np.info(np.eye) np.eyenp.eye文档

复制/分类/整形 (Copying/sorting/reshaping)

np.copy(arr)np.copy(arr) arr to new memoryarr复制到新内存
arr.view(dtype)arr.view(dtype) arr elements with type dtypedtypearr元素的视图
arr.sort()arr.sort() arrarr
arr.sort(axis=0)arr.sort(axis=0) arrarr特定轴进行排序
two_d_arr.flatten()two_d_arr.flatten() two_d_arr to 1Dtwo_d_arr为1D
arr.Tarr.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形状更改为5x5 x 6 and fills new values with 6并用00填充新值

添加/删除元素 (Adding/removing Elements)

np.append(arr,values)np.append(arr,values) arrarr末尾
np.insert(arr,2,values)np.insert(arr,2,values) arr before index 2之前的2arr插入值
np.delete(arr,3,axis=0)np.delete(arr,3,axis=0) 3 of arr索引arr3的行
np.delete(arr,4,axis=1)np.delete(arr,4,axis=1) 4 of arr索引arr4的列

合并/拆分 (Combining/splitting)

np.concatenate((arr1,arr2),axis=0)np.concatenate((arr1,arr2),axis=0) arr2 as rows to the end of arr2作为行添加到arr1arr1的末尾
np.concatenate((arr1,arr2),axis=1)np.concatenate((arr1,arr2),axis=1) arr2 as columns to end of arr2作为列添加到arr1arr1末尾
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个索引上水平分割5th indexarr

索引/切片/子集 (Indexing/slicing/subsetting)

arr[5]arr[5] 55的元素
arr[2,5]arr[2,5] [2][5][2][5]上的二维数组元素
arr[1]=4arr[1]=4 1 the value 1的数组元素分配值44
arr[1,3]=10arr[1,3]=10 [1][3] the value [1][3]上的数组元素分配值1010
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列的第40,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<5arr<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] 55数组元素

标量数学 (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) 33
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) 5th power5次方

向量数学 (Vector Math)

np.add(arr1,arr2)np.add(arr1,arr2) arr2 to arr2添加到arr1arr1
np.subtract(arr1,arr2)np.subtract(arr1,arr2) arr2 from arr1逐元素减去arr1arr2
np.multiply(arr1,arr2)np.multiply(arr1,arr2) arr1 by arr1乘以arr2arr2
np.divide(arr1,arr2)np.divide(arr1,arr2) arr1 by arr1除以arr2arr2
np.power(arr1,arr2)np.power(arr1,arr2) arr1 raised to the power of arr1arr2arr2的幂
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() arrarr总和
arr.min()arr.min() arrarr最小值
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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值