杰瑞科技汇

python numpy array

Of course! Here is a comprehensive guide to NumPy arrays in Python, covering what they are, why they're essential, and how to use them with clear examples.

python numpy array-图1
(图片来源网络,侵删)

What is a NumPy Array?

At its core, a NumPy array is a powerful, multi-dimensional data structure in Python. Think of it as a grid of values, all of the same data type. This is the key difference from a standard Python list, which can hold elements of different types.

Why use NumPy arrays instead of Python lists?

  • Performance: NumPy operations are implemented in C and are incredibly fast. This is because the data is stored in a single, contiguous block of memory, and operations are performed on the entire array at once (a concept called vectorization). Loops in Python are slow by comparison.
  • Functionality: NumPy provides a vast library of mathematical, statistical, and logical functions that can be applied to arrays with a single line of code.
  • Memory Efficiency: NumPy arrays are more memory-efficient than Python lists because they store data in a compact, homogeneous format.

Installation and Import

First, you need to install the NumPy library if you haven't already.

pip install numpy

Then, import it in your Python script. The standard convention is to import it as np.

python numpy array-图2
(图片来源网络,侵删)
import numpy as np

Creating NumPy Arrays

There are several ways to create NumPy arrays.

a) From a Python List

This is the most common way to get started.

# Create a 1D array (a vector)
list_1d = [1, 2, 3, 4, 5]
arr_1d = np.array(list_1d)
print(f"1D Array: {arr_1d}")
print(f"Type: {type(arr_1d)}")
print(f"Data type of elements: {arr_1d.dtype}") # 'int32' or 'int64' depending on system
# Create a 2D array (a matrix)
list_2d = [[1, 2, 3], [4, 5, 6]]
arr_2d = np.array(list_2d)
print(f"\n2D Array:\n{arr_2d}")
print(f"Shape: {arr_2d.shape}") # (rows, columns)

b) Using Built-in Functions

NumPy has many functions for creating specific arrays.

# Create an array of zeros
zeros_arr = np.zeros((3, 4)) # Shape is (3 rows, 4 columns)
print("Array of Zeros:\n", zeros_arr)
# Create an array of ones
ones_arr = np.ones((2, 2), dtype=int) # Specify data type
print("\nArray of Ones:\n", ones_arr)
# Create an array with a range of values (like Python's range)
range_arr = np.arange(10) # 0 to 9
print("\nArray using arange:\n", range_arr)
# Create an array with evenly spaced numbers (start, stop, step)
linspace_arr = np.linspace(0, 1, 5) # 5 numbers from 0 to 1 (inclusive)
print("\nArray using linspace:\n", linspace_arr)
# Create an identity matrix
identity_mat = np.eye(3)
print("\nIdentity Matrix:\n", identity_mat)
# Create a random array
random_arr = np.random.rand(3, 3) # 3x3 array with random floats between 0 and 1
print("\nRandom Array:\n", random_arr)

Key Attributes of an Array

Understanding these attributes is crucial for working with arrays.

python numpy array-图3
(图片来源网络,侵删)
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(f"Array:\n{arr}")
print(f"Number of dimensions (ndim): {arr.ndim}")      # 2 (because it's a matrix)
print(f"Shape (rows, cols): {arr.shape}")             # (2, 3)
print(f"Total number of elements (size): {arr.size}")  # 6
print(f"Data type of elements: {arr.dtype}")          # 'int32' or 'int64'
print(f"Memory size of each element (in bytes): {arr.itemsize}") # 4 or 8
print(f"Total memory usage (in bytes): {arr.nbytes}") # size * itemsize

Array Indexing and Slicing

This works very similarly to Python lists, but with multiple dimensions.

a) 1D Array Slicing

arr_1d = np.array([10, 20, 30, 40, 50])
# Get a single element
print(f"Element at index 2: {arr_1d[2]}") # 30
# Get a slice
print(f"Slice from index 1 to 3: {arr_1d[1:4]}") # [20, 30, 40]

b) 2D Array Slicing

The syntax is array[row, column].

arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Get a single element
print(f"Element at row 0, col 1: {arr_2d[0, 1]}") # 2
# Get an entire row
print(f"Row 1: {arr_2d[1, :]}") # [4, 5, 6]
# Get an entire column
print(f"Column 0: {arr_2d[:, 0]}") # [1, 4, 7]
# Get a sub-matrix (rows 0 to 1, columns 1 to 2)
print(f"Sub-matrix:\n{arr_2d[0:2, 1:3]}")
# [[2 3]
#  [5 6]]

Array Operations (Vectorization)

This is where NumPy shines. You can perform operations on entire arrays without writing slow Python loops.

a) Arithmetic Operations

These operations are performed element-wise.

a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
print(f"a + b: {a + b}")
print(f"a - b: {a - b}")
print(f"a * b: {a * b}")
print(f"a / b: {a / b}")
print(f"a ** 2 (a squared): {a ** 2}")
print(f"sin(a): {np.sin(a)}") # Using a NumPy mathematical function

b) Matrix Multiplication

There's a crucial difference between element-wise multiplication () and matrix multiplication ().

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Element-wise multiplication
print("Element-wise multiplication (A * B):\n", A * B)
# [[ 5 12]
#  [21 32]]
# Matrix multiplication (Dot Product)
print("Matrix multiplication (A @ B):\n", A @ B)
# [[19 22]
#  [43 50]]

Universal Functions (ufuncs)

NumPy provides a huge number of mathematical functions that operate on arrays element-wise. These are called universal functions or ufuncs.

arr = np.array([0, np.pi / 2, np.pi])
print(f"Array: {arr}")
print(f"sin(arr): {np.sin(arr)}")
print(f"cos(arr): {np.cos(arr)}")
print(f"sqrt(arr): {np.sqrt(arr)}")
print(f"exp(arr): {np.exp(arr)}") # e^x

Useful Array Methods

NumPy arrays have many useful built-in methods.

arr = np.array([[1, 2, 3], [4, 5, 6]])
# Sum
print(f"Sum of all elements: {arr.sum()}")
print(f"Sum along columns (axis=0): {arr.sum(axis=0)}") # [5 7 9]
print(f"Sum along rows (axis=1): {arr.sum(axis=1)}")   # [6 15]
# Mean
print(f"Mean of all elements: {arr.mean()}")
# Max/Min
print(f"Maximum value: {arr.max()}")
print(f"Index of max value: {arr.argmax()}") # Flattened index
# Reshape an array
reshaped_arr = arr.reshape(3, 2)
print(f"\nReshaped array (3x2):\n{reshaped_arr}")
# Flatten an array to 1D
flattened_arr = arr.flatten()
print(f"\nFlattened array:\n{flattened_arr}")

Aggregations and Axis Concept

The axis parameter is fundamental for multi-dimensional arrays. It specifies the dimension along which to perform the operation.

  • axis=0 operates down the rows (column-wise).
  • axis=1 operates across the columns (row-wise).
data = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
print("Original Array:\n", data)
# Sum along axis=0 (column-wise)
# (1+4+7), (2+5+8), (3+6+9)
print("\nSum along axis=0 (columns):", data.sum(axis=0))
# Sum along axis=1 (row-wise)
# (1+2+3), (4+5+6), (7+8+9)
print("Sum along axis=1 (rows):   ", data.sum(axis=1))

Comparison and Boolean Indexing

You can create boolean masks to filter arrays.

arr = np.arange(10)
# Create a boolean condition
mask = arr > 5
print(f"Boolean mask: {mask}")
# [False False False False False False  True  True  True  True]
# Use the mask to select elements
filtered_arr = arr[mask]
print(f"Filtered array (elements > 5): {filtered_arr}")
# You can chain conditions
# Use & (and), | (or), ~ (not) and parentheses ()
chained_mask = (arr > 3) & (arr < 8)
print(f"Chained mask (3 < arr < 8): {chained_mask}")
filtered_arr_2 = arr[chained_mask]
print(f"Filtered array (3 < arr < 8): {filtered_arr_2}")

Summary

Concept Description Example
Creation Make arrays from lists or built-in functions. np.array([1, 2, 3]), np.zeros((2, 2))
Shape The dimensions of the array (rows, cols, ...). arr.shape -> (3, 4)
Indexing Access elements or slices. arr[0, 1], arr[:, 2]
Vectorization Perform element-wise operations on entire arrays. a + b, np.sin(a)
axis The dimension along which an aggregation is performed. arr.sum(axis=0) (column-wise)
Boolean Indexing Filter an array using a boolean condition. arr[arr > 5]

NumPy is the foundational library for scientific computing in Python. Mastering it is the first step towards using more advanced libraries like Pandas, Matplotlib, Scikit-learn, and TensorFlow.

分享:
扫描分享到社交APP
上一篇
下一篇