Friday, 1 June 2018

Comprehensive Guide to NumPy: The Essential Library for Numerical Computing

Standard

Visualizing numpy reshape and stack | Towards Data Science

https://www.google.co.in/search?q=numpy+reference&safe=strict&source=lnms&tbm=isch&sa=X&ved=0ahUKEwju_JHd1rHbAhUcSI8KHZKDAvAQ_AUICigB&biw=1707&bih=816&dpr=1.13#imgrc=ZasmLl1TxSPa9M:   

Introduction to NumPy
NumPy (Numerical Python) is a fundamental library for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. NumPy is widely used in data science, machine learning, and scientific research due to its high performance and ease of use.
Installation
To install NumPy, use the following command:
pip install numpy
Importing NumPy
To start using NumPy, import it into your Python script:
import numpy as np
1. Creating NumPy Arrays
NumPy arrays are the core of NumPy. They can be created in various ways:
1.1 Creating Arrays from Lists
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output:
[1 2 3 4 5]
1.2 Creating Arrays with Zeros and Ones
zeros = np.zeros((3,3))
ones = np.ones((2,2))
print("Zeros:\n", zeros)
print("Ones:\n", ones)
1.3 Creating Arrays with a Range of Numbers
arr = np.arange(1, 10, 2)
print(arr)
Output:
[1 3 5 7 9]
1.4 Creating Linearly Spaced Arrays
arr = np.linspace(0, 1, 5)
print(arr)
Output:
[0. 0.25 0.5 0.75 1. ]
1.5 Creating Random Arrays
rand_arr = np.random.rand(3,3) # Uniform distribution
randn_arr = np.random.randn(3,3) # Standard normal distribution
print("Random Array:\n", rand_arr)
print("Random Normal Array:\n", randn_arr)
2. Array Properties and Operations
2.1 Checking Shape and Size
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Size:", arr.size)
Output:
Shape: (2, 3)
Size: 6
2.2 Reshaping Arrays
arr = np.arange(1, 10)
reshaped_arr = arr.reshape(3,3)
print(reshaped_arr)
2.3 Transposing an Array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.T)
2.4 Flattening an Array
arr = np.array([[1,2,3],[4,5,6]])
print(arr.flatten())
3. Mathematical Operations
NumPy allows element-wise operations on arrays.
3.1 Basic Arithmetic Operations
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print("Addition:", arr1 + arr2)
print("Multiplication:", arr1 * arr2)
print("Division:", arr1 / arr2)
3.2 Aggregation Functions
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr))
print("Mean:", np.mean(arr))
print("Max:", np.max(arr))
print("Min:", np.min(arr))
print("Standard Deviation:", np.std(arr))
4. Indexing and Slicing
4.1 Accessing Elements
arr = np.array([10, 20, 30, 40])
print(arr[2])
4.2 Slicing Arrays
arr = np.array([0, 1, 2, 3, 4, 5, 6])
print(arr[1:5])
print(arr[:3])
print(arr[::2])
5. Boolean Masking and Filtering
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr[arr > 3])
6. Linear Algebra Functions
NumPy provides functions to perform matrix operations and solve linear equations.
6.1 Dot Product
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
print(np.dot(A, B))
6.2 Determinant of a Matrix
from numpy.linalg import det
A = np.array([[1, 2], [3, 4]])
print(det(A))
6.3 Inverse of a Matrix
from numpy.linalg import inv
A = np.array([[1, 2], [3, 4]])
print(inv(A))
7. Random Number Generation
NumPy provides powerful tools to generate random numbers.
7.1 Generating Random Integers
rand_ints = np.random.randint(1, 10, (3,3))
print(rand_ints)
7.2 Shuffling an Array
arr = np.array([1,2,3,4,5])
np.random.shuffle(arr)
print(arr)
Conclusion
NumPy is an essential tool for numerical and scientific computing in Python. Its efficient handling of arrays, mathematical functions, and integration with machine learning libraries make it a preferred choice for data scientists and researchers. By mastering NumPy, you unlock the full potential of numerical computing in Python.

0 comments:

Post a Comment