Python数据科学:NumPy Cheat Sheet

简介: Python数据科学:NumPy Cheat Sheet

Key and Imports

In this cheat sheet, we use the following shorthand:


arr | A NumPy Array object


You’ll also need to import numpy to get started:


import numpy as np


Importing/exporting

np.loadtxt(‘file.txt’) | From a text file

np.genfromtxt(‘file.csv’,delimiter=’,’) | From a CSV file

np.savetxt(‘file.txt’,arr,delimiter=’ ‘) | Writes to a text file

np.savetxt(‘file.csv’,arr,delimiter=’,’) | Writes to a CSV file


Creating Arrays

np.array([1,2,3]) | One dimensional array

np.array([(1,2,3),(4,5,6)]) | Two dimensional array

np.zeros(3) | 1D array of length 3 all values 0

np.ones((3,4)) | 3x4 array with all values 1

np.eye(5) | 5x5 array of 0 with 1 on diagonal (Identity matrix)

np.linspace(0,100,6) | Array of 6 evenly divided values from 0 to 100

np.arange(0,10,3) | Array of values from 0 to less than 10 with step 3 (eg [0,3,6,9])

np.full((2,3),8) | 2x3 array with all values 8

np.random.rand(4,5) | 4x5 array of random floats between 0-1

np.random.rand(6,7)*100 | 6x7 array of random floats between 0-100

np.random.randint(5,size=(2,3)) | 2x3 array with random ints between 0-4


Inspecting Properties

arr.size | Returns number of elements in arr

arr.shape | Returns dimensions of arr (rows,columns)

arr.dtype | Returns type of elements in arr

arr.astype(dtype) | Convert arr elements to type dtype

arr.tolist() | Convert arr to a Python list

np.info(np.eye) | View documentation for np.eye


Copying/sorting/reshaping

np.copy(arr) | Copies arr to new memory

arr.view(dtype) | Creates view of arr elements with type dtype

arr.sort() | Sorts arr

arr.sort(axis=0) | Sorts specific axis of arr

two_d_arr.flatten() | Flattens 2D array two_d_arr to 1D

arr.T | Transposes arr (rows become columns and vice versa)

arr.reshape(3,4) | Reshapes arr to 3 rows, 4 columns without changing data

arr.resize((5,6)) | Changes arr shape to 5x6 and fills new values with 0


Adding/removing Elements

np.append(arr,values) | Appends values to end of arr

np.insert(arr,2,values) | Inserts values into arr before index 2

np.delete(arr,3,axis=0) | Deletes row on index 3 of arr

np.delete(arr,4,axis=1) | Deletes column on index 4 of arr


Combining/splitting

np.concatenate((arr1,arr2),axis=0) | Adds arr2 as rows to the end of arr1

np.concatenate((arr1,arr2),axis=1) | Adds arr2 as columns to end of arr1

np.split(arr,3) | Splits arr into 3 sub-arrays

np.hsplit(arr,5) | Splits arr horizontally on the 5th index


Indexing/slicing/subsetting

arr[5] | Returns the element at index 5

arr[2,5] | Returns the 2D array element on index [2][5]

arr[1]=4 | Assigns array element on index 1 the value 4

arr[1,3]=10 | Assigns array element on index [1][3] the value 10

arr[0:3] | Returns the elements at indices 0,1,2 (On a 2D array: returns rows 0,1,2)

arr[0:3,4] | Returns the elements on rows 0,1,2 at column 4

arr[:2] | Returns the elements at indices 0,1 (On a 2D array: returns rows 0,1)

arr[:,1] | Returns the elements at index 1 on all rows

arr<5 | Returns an array with boolean values

(arr1<3) & (arr2>5) | Returns an array with boolean values

~arr | Inverts a boolean array

arr[arr<5] | Returns array elements smaller than 5


Scalar Math

np.add(arr,1) | Add 1 to each array element

np.subtract(arr,2) | Subtract 2 from each array element

np.multiply(arr,3) | Multiply each array element by 3

np.divide(arr,4) | Divide each array element by 4 (returns np.nan for division by zero)

np.power(arr,5) | Raise each array element to the 5th power


Vector Math

np.add(arr1,arr2) | Elementwise add arr2 to arr1

np.subtract(arr1,arr2) | Elementwise subtract arr2 from arr1

np.multiply(arr1,arr2) | Elementwise multiply arr1 by arr2

np.divide(arr1,arr2) | Elementwise divide arr1 by arr2

np.power(arr1,arr2) | Elementwise raise arr1 raised to the power of arr2

np.array_equal(arr1,arr2) | Returns True if the arrays have the same elements and shape

np.sqrt(arr) | Square root of each element in the array

np.sin(arr) | Sine of each element in the array

np.log(arr) | Natural log of each element in the array

np.abs(arr) | Absolute value of each element in the array

np.ceil(arr) | Rounds up to the nearest int

np.floor(arr) | Rounds down to the nearest int

np.round(arr) | Rounds to the nearest int


Statistics

np.mean(arr,axis=0) | Returns mean along specific axis

arr.sum() | Returns sum of arr

arr.min() | Returns minimum value of arr

arr.max(axis=0) | Returns maximum value of specific axis

np.var(arr) | Returns the variance of array

np.std(arr,axis=1) | Returns the standard deviation of specific axis

arr.corrcoef() | Returns correlation coefficient of array

相关文章
|
2月前
|
存储 Java 数据处理
(numpy)Python做数据处理必备框架!(一):认识numpy;从概念层面开始学习ndarray数组:形状、数组转置、数值范围、矩阵...
Numpy是什么? numpy是Python中科学计算的基础包。 它是一个Python库,提供多维数组对象、各种派生对象(例如掩码数组和矩阵)以及用于对数组进行快速操作的各种方法,包括数学、逻辑、形状操作、排序、选择、I/0 、离散傅里叶变换、基本线性代数、基本统计运算、随机模拟等等。 Numpy能做什么? numpy的部分功能如下: ndarray,一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环)。 用于读写磁盘数据的工具以及用于操作内存映射文件的工具。 线性代数、随机数生成以及傅里叶变换功能。 用于集成由C、C++
314 1
|
2月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
163 0
|
4月前
|
机器学习/深度学习 API 异构计算
JAX快速上手:从NumPy到GPU加速的Python高性能计算库入门教程
JAX是Google开发的高性能数值计算库,旨在解决NumPy在现代计算需求下的局限性。它不仅兼容NumPy的API,还引入了自动微分、GPU/TPU加速和即时编译(JIT)等关键功能,显著提升了计算效率。JAX适用于机器学习、科学模拟等需要大规模计算和梯度优化的场景,为Python在高性能计算领域开辟了新路径。
387 0
JAX快速上手:从NumPy到GPU加速的Python高性能计算库入门教程
|
4月前
|
存储 数据采集 数据处理
Pandas与NumPy:Python数据处理的双剑合璧
Pandas与NumPy是Python数据科学的核心工具。NumPy以高效的多维数组支持数值计算,适用于大规模矩阵运算;Pandas则提供灵活的DataFrame结构,擅长处理表格型数据与缺失值。二者在性能与功能上各具优势,协同构建现代数据分析的技术基石。
363 0
|
9月前
|
机器学习/深度学习 数据可视化 TensorFlow
Python 高级编程与实战:深入理解数据科学与机器学习
本文深入探讨了Python在数据科学与机器学习中的应用,介绍了pandas、numpy、matplotlib等数据科学工具,以及scikit-learn、tensorflow、keras等机器学习库。通过实战项目,如数据可视化和鸢尾花数据集分类,帮助读者掌握这些技术。最后提供了进一步学习资源,助力提升Python编程技能。
|
9月前
|
机器学习/深度学习 数据可视化 算法
Python 高级编程与实战:深入理解数据科学与机器学习
在前几篇文章中,我们探讨了 Python 的基础语法、面向对象编程、函数式编程、元编程、性能优化和调试技巧。本文将深入探讨 Python 在数据科学和机器学习中的应用,并通过实战项目帮助你掌握这些技术。
|
机器学习/深度学习 数据采集 数据可视化
Python在数据科学中的应用:从入门到实践
本文旨在为读者提供一个Python在数据科学领域应用的全面概览。我们将从Python的基础语法开始,逐步深入到数据处理、分析和可视化的高级技术。文章不仅涵盖了Python中常用的数据科学库,如NumPy、Pandas和Matplotlib,还探讨了机器学习库Scikit-learn的使用。通过实际案例分析,本文将展示如何利用Python进行数据清洗、特征工程、模型训练和结果评估。此外,我们还将探讨Python在大数据处理中的应用,以及如何通过集成学习和深度学习技术来提升数据分析的准确性和效率。
|
数据采集 数据可视化 数据处理
Python数据科学:Pandas库入门与实践
Python数据科学:Pandas库入门与实践
|
数据处理 Python
在数据科学领域,Pandas和NumPy是每位数据科学家和分析师的必备工具
在数据科学领域,Pandas和NumPy是每位数据科学家和分析师的必备工具。本文通过问题解答形式,深入探讨Pandas与NumPy的高级操作技巧,如复杂数据筛选、分组聚合、数组优化及协同工作,结合实战演练,助你提升数据处理能力和工作效率。
169 5
|
机器学习/深度学习 数据采集 数据可视化
Python数据科学实战:从Pandas到机器学习
Python数据科学实战:从Pandas到机器学习

推荐镜像

更多