Python libraries and frameworks NumPy

NumPy is a popular Python library for scientific computing and data analysis. NumPy provides a powerful N-dimensional array object, and a set of functions and tools for working with this array object, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, and random simulations.

Here are some key features and examples of how to use NumPy:

## Numpy Arrays

The core of NumPy is its N-dimensional array object, called `ndarray`. `ndarray` provides fast and efficient storage and manipulation of homogeneous array data, and supports a wide range of operations, including element-wise operations, broadcasting, slicing, and indexing.

Here’s an example of how to create and manipulate a NumPy array:

python
import numpy as np

# Create a NumPy array
a = np.array([1, 2, 3, 4, 5])

# Perform element-wise operations on the array
b = a * 2
c = np.sin(a)

# Slice and index the array
d = a[1:3]
e = a[a > 3]

# Print the results
print("Original array:", a)
print("Element-wise multiplication:", b)
print("Element-wise sine:", c)
print("Sliced array:", d)
print("Indexed array:", e)

## Linear Algebra

NumPy also provides a set of linear algebra functions for working with matrices and vectors, including matrix multiplication, matrix inversion, eigenvalue and eigenvector decomposition, and singular value decomposition.

Here’s an example of how to perform matrix multiplication using NumPy:

python
import numpy as np

# Create two matrices
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Multiply the matrices
c = np.dot(a, b)

# Print the result
print("Matrix multiplication:", c)

## Random Sampling

NumPy also provides a set of functions for generating random samples from various distributions, including uniform, normal, and binomial distributions.

Here’s an example of how to generate a sample of random numbers from a normal distribution using NumPy:

python
import numpy as np

# Generate a sample of random numbers from a normal distribution
a = np.random.normal(0, 1, size=1000)

# Print the result
print("Sample of random numbers:", a)

Overall, NumPy is a powerful and versatile library for scientific computing and data analysis in Python, and is widely used in a variety of fields, including physics, engineering, economics, and finance.