首页 >后端开发 >Python教程 >Numpy 备忘单

Numpy 备忘单

DDD
DDD原创
2024-10-04 06:08:291091浏览

Numpy Cheat Sheet

NumPy 综合指南:终极备忘单

NumPy(数值Python)是Python中科学计算的基础库。它增加了对大型多维数组和矩阵的支持,以及大量数学函数,可以有效地对这些数组进行操作。 NumPy 广泛用于数据分析、机器学习、深度学习和数值计算。


1.导入 NumPy

在使用 NumPy 之前,必须将库导入到您的 Python 环境中。


import numpy as np



2. NumPy 数组

NumPy 数组是该库的核心。它们提供快速高效的大型数据集存储并支持矢量化操作。

创建数组

在 NumPy 中创建数组有多种方法:

1D、2D 和 3D 数组创建


# 1D array
arr_1d = np.array([1, 2, 3, 4])
# 2D array
arr_2d = np.array([[1, 2], [3, 4], [5, 6]])
# 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])


预期输出:


1D array: [1 2 3 4]
2D array: [[1 2]
           [3 4]
           [5 6]]
3D array: [[[1 2]
            [3 4]]
           [[5 6]
            [7 8]]]



3.数组初始化函数

零、一、满、空、眼睛、恒等

这些函数创建具有预定义值的数组。

  • np.zeros(shape) – 返回给定形状的新数组,填充零。
  • np.ones(shape) – 返回一个充满 1 的新数组。
  • np.full(shape, fill_value) – 返回给定形状的新数组,并用指定值填充。
  • np.empty(shape) – 返回指定形状的未初始化数组。
  • np.eye(N) – 返回对角线上有 1 的 2D 单位矩阵。
  • np.identity(N) – 创建大小为 N 的方单位矩阵。

# Creating arrays with initialization functions
zeros_arr = np.zeros((2, 3))
ones_arr = np.ones((2, 2))
full_arr = np.full((3, 3), 7)
eye_arr = np.eye(3)


预期输出:


Zeros array: [[0. 0. 0.]
              [0. 0. 0.]]
Ones array: [[1. 1.]
             [1. 1.]]
Full array: [[7 7 7]
             [7 7 7]
             [7 7 7]]
Identity matrix: [[1. 0. 0.]
                  [0. 1. 0.]
                  [0. 0. 1.]]



4.随机数组生成

NumPy 提供了多种生成随机数的方法。

使用 np.random 的随机数

  • np.random.rand(shape) – 生成给定形状的随机值(0 到 1 之间)。
  • np.random.randint(low, high, size) – 返回从低(包含)到高(不包含)的随机整数。
  • np.random.choice(array) – 从数组中随机选择一个元素。

random_arr = np.random.rand(2, 2)
randint_arr = np.random.randint(1, 10, (2, 3))


预期输出:


Random array: [[0.234 0.983]
               [0.456 0.654]]
Random integer array: [[5 7 2]
                       [3 9 1]]



5.检查和操作数组

数组属性

  • ndarray.shape – 返回数组的维度。
  • ndarray.size – 返回数组中的元素数量。
  • ndarray.ndim – 返回维数。
  • ndarray.dtype – 返回数组中元素的类型。
  • ndarray.itemsize – 返回数组中每个元素的大小(以字节为单位)。

arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Dimensions:", arr.ndim)
print("Data type:", arr.dtype)
print("Item size:", arr.itemsize)


预期输出:


Shape: (2, 3)
Size: 6
Dimensions: 2
Data type: int32
Item size: 4


数组重塑

  • reshape(shape) – 将数组重塑为指定形状而不更改其数据。
  • ravel() – 返回数组的扁平化版本 (1D)。
  • transpose() – 转置数组。

reshaped = arr.reshape(3, 2)
flattened = arr.ravel()
transposed = arr.transpose()


预期输出:


Reshaped array: [[1 2]
                 [3 4]
                 [5 6]]
Flattened array: [1 2 3 4 5 6]
Transposed array: [[1 4]
                   [2 5]
                   [3 6]]



6.数组索引、切片和修改元素

NumPy 数组提供了访问、切片和修改数据的强大方法,使您能够高效地使用 1D、2D 和 3D 数组。在本节中,我们将探讨如何使用索引和切片来访问元素和修改数组。

基本索引

您可以使用方括号 [ ] 访问数组的元素。索引适用于任何维度的数组,包括 1D、2D 和 3D 数组。

一维数组索引

您可以通过指定索引来访问一维数组的各个元素。


arr = np.array([1, 2, 3, 4])
print(arr[1])  # Access second element


预期输出:


2


二维数组索引

在二维数组中,您可以通过指定行索引和列索引来访问元素。格式为arr[行,列].


arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d[1, 2])  # Access element at row 1, column 2


预期输出:


6


3D 数组索引

对于 3D 数组,您需要指定三个索引:深度、行和列。格式为arr[深度,行,列].


arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d[1, 0, 1])  # Access element at depth 1, row 0, column 1


预期输出:


6



切片

切片用于从较大数组中提取子数组。切片的语法是start:stop:step。起始索引包含在内,而停止索引不包含。

一维数组切片

您可以通过指定开始、停止和步长索引来对一维数组进行切片。


arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])  # Slicing from index 1 to 3 (exclusive of index 4)


Expected Output:


[20 30 40]


2D Array Slicing

In a 2D array, you can slice both rows and columns. For example, arr[start_row:end_row, start_col:end_col] will slice rows and columns.


arr_2d = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
print(arr_2d[1:3, 0:2])  # Rows from index 1 to 2, Columns from index 0 to 1


Expected Output:


[[40 50]
 [70 80]]


3D Array Slicing

For 3D arrays, slicing works similarly by specifying the range for depth, rows, and columns.


arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d[1:, 0, :])  # Depth from index 1, Row 0, All columns


Expected Output:


[[5 6]]



Boolean Indexing

Boolean indexing allows you to filter elements based on a condition. The condition returns a boolean array, which is then used to index the original array.


arr = np.array([10, 15, 20, 25, 30])
print(arr[arr > 20])  # Extract elements greater than 20


Expected Output:


[25 30]



Adding, Removing, and Modifying Elements

You can also modify arrays by adding, removing, or altering elements using various functions.

Adding Elements

You can append or insert elements into arrays with the following methods:

  • np.append(arr, values) – Appends values to the end of an array.
  • np.insert(arr, index, values) – Inserts values at a specified index.
  • np.concatenate([arr1, arr2]) – Concatenates two arrays along an existing axis.

arr = np.array([1, 2, 3])
appended = np.append(arr, 4)  # Add 4 at the end
inserted = np.insert(arr, 1, [10, 20])  # Insert 10, 20 at index 1
concatenated = np.concatenate([arr, np.array([4, 5])])  # Concatenate arr with another array


Expected Output:


Appended: [1 2 3 4]
Inserted: [ 1 10 20  2  3]
Concatenated: [1 2 3 4 5]


Removing Elements

To remove elements from an array, you can use np.delete().

  • np.delete(arr, index) – Deletes the element at the specified index.
  • np.delete(arr, slice) – Deletes elements in a slice of the array.

arr = np.array([1, 2, 3, 4])
deleted = np.delete(arr, 1)  # Remove element at index 1
slice_deleted = np.delete(arr, slice(1, 3))  # Remove elements from index 1 to 2 (exclusive of 3)


Expected Output:


Deleted: [1 3 4]
Slice deleted: [1 4]



7. Mathematical Operations

NumPy supports element-wise operations, broadcasting, and a variety of useful mathematical functions.

Basic Arithmetic

You can perform operations like addition, subtraction, multiplication, and division element-wise:


arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2)  # Element-wise addition
print(arr1 - arr2)  # Element-wise subtraction
print(arr1 * arr2)  # Element-wise multiplication
print(arr1 / arr2)  # Element-wise division


Expected Output:


Addition: [5 7 9]
Subtraction: [-3 -3 -3]
Multiplication: [ 4 10 18]
Division: [0.25 0.4 0.5]


Aggregation Functions

These functions return a single value for an entire array.

  • np.sum(arr) – Returns the sum of array elements.
  • np.mean(arr) – Returns the mean of array elements.
  • np.median(arr) – Returns the median of array elements.
  • np.std(arr) – Returns the standard deviation.
  • np.var(arr) – Returns the variance.
  • np.min(arr) / np.max(arr) – Returns the minimum/maximum element.

arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr))
print(np.mean(arr))
print(np.median(arr))
print(np.std(arr))
print(np.min(arr), np.max(arr))


Expected Output:


15


3.0
3.0
1.4142135623730951
1 5



8. Broadcasting and Vectorization

NumPy allows operations between arrays of different shapes via broadcasting, a powerful mechanism for element-wise operations.

Example: Broadcasting


arr = np.array([1, 2, 3])
print(arr + 10)  # Broadcasting scalar value 10


Expected Output:


[11 12 13]



9. Linear Algebra in NumPy

NumPy provides many linear algebra functions, such as:

  • np.dot() – Dot product of two arrays.
  • np.matmul() – Matrix multiplication.
  • np.linalg.inv() – Inverse of a matrix.
  • np.linalg.det() – Determinant of a matrix.
  • np.linalg.eig() – Eigenvalues and eigenvectors.

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
dot_product = np.dot(A, B)
matrix_mult = np.matmul(A, B)
inv_A = np.linalg.inv(A)
det_A = np.linalg.det(A)


Expected Output:


Dot product: [[19 22]
              [43 50]]
Matrix multiplication: [[19 22]
                        [43 50]]
Inverse of A: [[-2.   1. ]
               [ 1.5 -0.5]]
Determinant of A: -2.0



10. Other Useful Functions

Sorting

  • np.sort(arr) – Returns a sorted array.

arr = np.array([3, 1, 2])
sorted_arr = np.sort(arr)


Expected Output:


[1 2 3]


Unique Values

  • np.unique(arr) – Returns the sorted unique elements of an array.

arr = np.array([1, 2, 2, 3, 3, 3])
unique_vals = np.unique(arr)


Expected Output:


[1 2 3]


Stacking and Splitting

  • np.vstack() – Stacks arrays vertically.
  • np.hstack() – Stacks arrays horizontally.
  • np.split() – Splits arrays into multiple sub-arrays.

arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
vstacked = np.vstack((arr1, arr2))
hstacked = np.hstack((arr1, arr2))
splits = np.split(np.array([1, 2, 3, 4]), 2)


Expected Output:


Vertical stack: [[1 2]
                 [3 4]]
Horizontal stack: [1 2 3 4]
Splits: [array([1, 2]), array([3, 4])]



Conclusion

NumPy is an essential library for any Python user working with large amounts of numerical data. With its efficient handling of arrays and vast range of mathematical operations, it lays the foundation for more advanced topics such as machine learning, data analysis, and scientific computing.

以上是Numpy 备忘单的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn