Files
the_information_nexus/tech_docs/python/NumPy.md
2024-05-01 12:28:44 -06:00

160 lines
3.2 KiB
Markdown

A powerful and versatile Python library for scientific computing is `NumPy`. It provides support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. NumPy is foundational for many other Python data science and machine learning libraries, offering efficient array operations and numerical computations. Here's a concise reference guide for common use cases with `NumPy`, formatted in Markdown syntax:
# `NumPy` Reference Guide
## Installation
```
pip install numpy
```
## Basic Operations
### Importing NumPy
```python
import numpy as np
```
### Creating Arrays
```python
# Create a one-dimensional array
arr_1d = np.array([1, 2, 3])
# Create a two-dimensional array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Create an array of zeros
zeros = np.zeros((3, 4))
# Create an array of ones
ones = np.ones((2, 3))
# Create an array with a range of elements
range_array = np.arange(10)
# Create a linearly spaced array
linear_spaced = np.linspace(0, 1, 5)
```
### Array Attributes
```python
# Array shape
print(arr_2d.shape)
# Number of array dimensions
print(arr_2d.ndim)
# Data type of array elements
print(arr_2d.dtype)
# Size of the array (number of elements)
print(arr_2d.size)
```
### Indexing and Slicing
```python
# Get a specific element [r, c]
element = arr_2d[1, 2]
# Get a specific row
row = arr_2d[0, :]
# Get a specific column
col = arr_2d[:, 2]
# Slicing [start_index:end_index:step_size]
slice_arr = arr_2d[0, 0:2]
```
### Basic Array Operations
```python
# Element-wise addition
result = arr_1d + arr_1d
# Element-wise subtraction
result = arr_1d - arr_1d
# Scalar multiplication
result = arr_1d * 2
# Element-wise multiplication
result = arr_1d * arr_1d
# Matrix multiplication
result = np.dot(arr_2d, arr_2d.T)
# Division
result = arr_1d / arr_1d
```
### Mathematical Functions
```python
# Square root
sqrt_arr = np.sqrt(arr_1d)
# Exponential
exp_arr = np.exp(arr_1d)
# Logarithm
log_arr = np.log(arr_1d)
# Trigonometric functions
sin_arr = np.sin(arr_1d)
```
### Statistics
```python
# Minimum
min_val = np.min(arr_1d)
# Maximum
max_val = np.max(arr_1d)
# Sum
sum_val = np.sum(arr_1d)
# Mean
mean_val = np.mean(arr_1d)
# Median
median_val = np.median(arr_1d)
# Standard deviation
std_dev = np.std(arr_1d)
```
### Reshaping and Flattening
```python
# Reshape
reshaped_arr = arr_2d.reshape((3, 2))
# Flatten the array
flat_arr = arr_2d.flatten()
```
## Advanced Operations
### Stacking Arrays
```python
# Vertical stacking
v_stack = np.vstack([arr_1d, arr_1d])
# Horizontal stacking
h_stack = np.hstack([arr_1d, arr_1d])
```
### Splitting Arrays
```python
# Split the array in 3 equally shaped arrays
split_arr = np.split(arr_1d, 3)
```
### Boolean Masking and Advanced Indexing
```python
# Find elements greater than 2
result = arr_1d > 2
# Index with a boolean array
filtered_arr = arr_1d[result]
```
`NumPy` is foundational for numerical and scientific computation in Python, providing efficient operations for handling and processing large data sets. This guide introduces fundamental concepts and operations, but NumPy's capabilities extend much further, making it an essential tool for data analysis, machine learning, and beyond.