0% found this document useful (0 votes)
4 views

Numpy_Notes (1)

NumPy is a Python library designed for numerical computations, offering support for multi-dimensional arrays and a variety of mathematical functions. Key features include broadcasting, integration with other libraries, and capabilities for array manipulation, indexing, and mathematical operations. Applications of NumPy span data preprocessing, machine learning, image processing, and simulations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Numpy_Notes (1)

NumPy is a Python library designed for numerical computations, offering support for multi-dimensional arrays and a variety of mathematical functions. Key features include broadcasting, integration with other libraries, and capabilities for array manipulation, indexing, and mathematical operations. Applications of NumPy span data preprocessing, machine learning, image processing, and simulations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Complete Notes on NumPy for Interviews

Introduction to NumPy
NumPy (Numerical Python) is a Python library used for numerical computa-
tions. It provides support for arrays, matrices, and a collection of mathematical
functions to operate on these data structures.

Key Features
1. N-dimensional array objects: Provides multi-dimensional arrays called
ndarray.
2. Broadcasting: Enables operations on arrays of different shapes.

3. Mathematical Functions: Built-in functions for linear algebra, statis-


tics, and more.
4. Integration with other libraries: Works seamlessly with libraries like
pandas, matplotlib, and scikit-learn.

Getting Started
import numpy as np

Create Arrays
1. 1D Array:

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

2. 2D Array:

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

3. Zeros and Ones:

1
zeros = np.zeros((2, 3)) # 2x3 array of zeros
ones = np.ones((3, 2)) # 3x2 array of ones

4. Empty Array:

empty_arr = np.empty((2, 3)) # Uninitialized array

5. Range of Numbers:

arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]

6. Linearly Spaced Numbers:

lin_space = np.linspace(0, 1, 5) # [0, 0.25, 0.5, 0.75, 1]

Array Properties
• arr.shape: Shape of the array.
• arr.ndim: Number of dimensions.
• arr.size: Total number of elements.
• arr.dtype: Data type of elements.
• arr.itemsize: Memory size of one element.

Array Manipulation
1. Reshape:

reshaped = arr.reshape(2, 3)

2. Flatten:

flat = arr.ravel()

3. Transpose:

transposed = arr.T

2
4. Concatenate:

concatenated = np.concatenate((arr1, arr2), axis=0)

5. Stacking:
• Vertical: np.vstack((arr1, arr2))
• Horizontal: np.hstack((arr1, arr2))

Indexing and Slicing


1. Basic Indexing:

print(arr[1, 2]) # Access element in row 1, column 2

2. Slicing:

subarray = arr[0:2, 1:3] # Slicing rows and columns

3. Boolean Indexing:

filtered = arr[arr > 5]

4. Fancy Indexing:

arr[[0, 2], [1, 3]] # Access specific elements

Mathematical Operations
1. Element-wise Operations:

sum_arr = arr1 + arr2


product_arr = arr1 * arr2

2. Matrix Multiplication:

result = np.dot(arr1, arr2)

3
3. Statistical Functions:

mean = np.mean(arr)
std_dev = np.std(arr)
min_val = np.min(arr)
max_val = np.max(arr)

4. Aggregates:

sum_all = np.sum(arr)
sum_axis0 = np.sum(arr, axis=0)
sum_axis1 = np.sum(arr, axis=1)

Linear Algebra in NumPy


1. Determinant:

from numpy.linalg import det


result = det(matrix)

2. Eigenvalues and Eigenvectors:

eigenvalues, eigenvectors = np.linalg.eig(matrix)

3. Inverse of a Matrix:

inv_matrix = np.linalg.inv(matrix)

Random Number Generation


1. Random Integers:

random_ints = np.random.randint(0, 10, size=(3, 3))

2. Random Floats:

random_floats = np.random.rand(3, 2)

3. Seed for Reproducibility:

np.random.seed(42)

4
Applications of NumPy
1. Data Preprocessing: Used for cleaning, transforming, and preparing
data.
2. Machine Learning: Works with datasets, matrices, and mathematical
operations.
3. Image Processing: Handles pixel data efficiently.

4. Simulation and Modeling: Generates random numbers for simulations.

You might also like