NUMPY
NumPy is a crucial library for numerical operations in Python.
1. Importing NumPy
import numpy as np
2. Creating Arrays
NumPy arrays are the core object for all computations.
arr = [Link]([1, 2, 3, 4, 5])
print(arr) # Output: [1 2 3 4 5]
Zeros array:
zeros_arr = [Link]((2, 3)) # 2x3 array of zeros
print(zeros_arr)
# Output:
# [[0. 0. 0.]
# [0. 0. 0.]]
Ones array:
ones_arr = [Link]((2, 3))
print(ones_arr)
# Output:
# [[1. 1. 1.]
# [1. 1. 1.]]
Arange (range of numbers):
arr_range = [Link](0, 10, 2) # From 0 to 10 with step 2
print(arr_range) # Output: [0 2 4 6 8]
Linspace (evenly spaced values):
arr_linspace = [Link](0, 1, 5) # 5 numbers between 0 and 1
print(arr_linspace)
# Output: [0. 0.25 0.5 0.75 1. ]
3. Array Indexing and Slicing
Accessing elements:
arr = [Link]([1, 2, 3, 4, 5])
print(arr[0]) # Output: 1 (accessing the first element)
Slicing:
arr = [Link]([1, 2, 3, 4, 5])
print(arr[1:4]) # Output: [2 3 4] (elements from index 1 to 3)
Fancy indexing:
arr = [Link]([1, 2, 3, 4, 5])
print(arr[[0, 2, 4]]) # Output: [1 3 5] (using a list of indices)
Boolean indexing:
arr = [Link]([1, 2, 3, 4, 5])
print(arr[arr > 2]) # Output: [3 4 5] (elements greater than 2)
4. Array Operations
Arithmetic operations:
arr = [Link]([1, 2, 3])
print(arr + 1) # Output: [2 3 4]
print(arr * 2) # Output: [2 4 6]
Element-wise operations (using functions):
arr = [Link]([1, 2, 3])
print([Link](arr)) # Output: [1. 1.41421356 1.73205081] (square root
of each element)
print([Link](arr)) # Output: [ 2.71828183 7.3890561 20.08553692]
(exponential of each element)
print([Link](arr)) # Output: 1
print([Link](arr)) # Output: 5
5. Reshaping and Sum, Mean, Min, Max:
arr = [Link]([1, 2, 3, 4, 5])
print([Link](arr)) # Output: 15
print([Link](arr)) # Output: 3.0
Resizing Arrays
Reshaping:
arr = [Link]([1, 2, 3, 4, 5, 6])
reshaped_arr = [Link]((2, 3)) # Convert 1D array to 2D
print(reshaped_arr)
# Output:
# [[1 2 3]# [4 5 6]]
Flattening:
arr_2d = [Link]([[1, 2], [3, 4]])
flattened = arr_2d.flatten()
print(flattened) # Output: [1 2 3 4]
Transposing:
arr_2d = [Link]([[1, 2], [3, 4]])
transposed = arr_2d.T
print(transposed)
# Output:
# [[1 3]
# [2 4]]
6. Random Module
Random Numbers:
rand_arr = [Link](2, 3) # Random numbers between 0 and 1 in 2x3 shape
print(rand_arr)
Random Integers:
rand_ints = [Link](0, 10, size=(3, 3)) # Random integers between 0
and 10
print(rand_ints)
Random Choice:
choices = [Link]([1, 2, 3, 4, 5], size=3) # Randomly choose
elements
print(choices)
7. Statistical Operations
Mean, Median, Standard Deviation:
arr = [Link]([1, 2, 3, 4, 5])
print([Link](arr)) # Output: 3.0
print([Link](arr)) # Output: 3.0
print([Link](arr)) # Output: 1.4142135623730951
Variance and Percentiles:
print([Link](arr)) # Output: 2.0
print([Link](arr, 50)) # Output: 3.0 (50th percentile = median)
8. Concatenation and Stacking Arrays
Concatenate:
arr1 = [Link]([1, 2])
arr2 = [Link]([3, 4])
concat_arr = [Link]((arr1, arr2))
print(concat_arr) # Output: [1 2 3 4]
Stacking:
arr1 = [Link]([1, 2])
arr2 = [Link]([3, 4])
vstack_arr = [Link]((arr1, arr2)) # Stack arrays vertically (row-wise)
print(vstack_arr)
# Output:
# [[1 2]
# [3 4]]
hstack_arr = [Link]((arr1, arr2)) # Stack arrays horizontally (column-
wise)
print(hstack_arr) # Output: [1 2 3 4]
9. Set Operations
Unique elements:
arr = [Link]([1, 2, 2, 3, 4, 4, 5])
unique_arr = [Link](arr)
print(unique_arr) # Output: [1 2 3 4 5]
Union, Intersection, and Difference:
arr1 = [Link]([1, 2, 3])
arr2 = [Link]([3, 4, 5])
print(np.union1d(arr1, arr2)) # Output: [1 2 3 4 5]
print(np.intersect1d(arr1, arr2)) # Output: [3]
print(np.setdiff1d(arr1, arr2)) # Output: [1 2]
10. Handling Missing Data (NaN)
Handling missing data is an important part of data analysis.
Detect NaN values:
arr = [Link]([1, [Link], 3])
print([Link](arr)) # Output: [False True False]
Replacing NaN values:
arr = [Link]([1, [Link], 3])
arr_cleaned = np.nan_to_num(arr, nan=0) # Replace NaN with 0
print(arr_cleaned) # Output: [1. 0. 3.]
11. Linear Algebra
Dot Product:
arr1 = [Link]([1, 2])
arr2 = [Link]([3, 4])
dot_product = [Link](arr1, arr2)
print(dot_product) # Output: 11
Matrix Multiplication:
arr1 = [Link]([[1, 2], [3, 4]])
arr2 = [Link]([[5, 6], [7, 8]])
matrix_mult = [Link](arr1, arr2)
print(matrix_mult)
# Output:
# [[19 22]
# [43 50]]
Inverse of a Matrix:
arr = [Link]([[1, 2], [3, 4]])
inv_arr = [Link](arr)
print(inv_arr)
# Output:
# [[-2. 1. ]
# [ 1.5 -0.5]]
INBUILT FUNCTIONS
1. Array Creation Functions
[Link](): Create a NumPy array from a Python list or other array-like object.
[Link](): Create an array of zeros with a specified shape.
[Link](): Create an array of ones with a specified shape.
[Link](): Create an array without initializing its values (it may contain random
values).
[Link](): Generate values in a range (like range() but returns a NumPy array).
[Link](): Generate evenly spaced numbers over a specified interval.
[Link](): Create a 2D identity matrix (diagonal 1s, rest 0s).
[Link](): Create an array filled with a constant value.
2. Array Manipulation
[Link](): Reshape an array to a new shape.
[Link](): Flatten a multi-dimensional array into 1D.
[Link](): Transpose an array (swap rows with columns).
[Link](): Join two or more arrays along a specified axis.
[Link](): Stack arrays along a new axis.
[Link](): Split an array into multiple sub-arrays.
[Link](): Append values to the end of an array.
[Link](): Insert values into an array at specified indices.
[Link](): Delete elements from an array at specified indices.
3. Mathematical Functions
[Link](): Add two arrays element-wise.
[Link](): Subtract two arrays element-wise.
[Link](): Multiply two arrays element-wise.
[Link](): Divide two arrays element-wise.
[Link](): Raise each element of an array to the power of a number.
[Link](): Compute the remainder of division element-wise.
[Link](): Compute the square root of each element.
[Link](): Compute the natural logarithm of each element.
[Link](): Compute the exponential of each element.
[Link](): Compute the absolute value of each element.
[Link](): Round elements of an array to the nearest integer.
[Link](): Round elements down to the nearest integer.
[Link](): Round elements up to the nearest integer.
4. Statistical Functions
[Link](): Compute the mean (average) of array elements.
[Link](): Compute the median of array elements.
[Link](): Compute the standard deviation of array elements.
[Link](): Compute the variance of array elements.
[Link](): Compute the sum of array elements.
[Link](): Find the minimum element in an array.
[Link](): Find the maximum element in an array.
[Link](): Find the index of the minimum element.
[Link](): Find the index of the maximum element.
[Link](): Compute the nth percentile of array elements.
[Link](): Compute the correlation coefficient matrix.
[Link](): Compute the covariance matrix.
5. Linear Algebra Functions
[Link](): Compute the dot product of two arrays.
[Link](): Compute the inverse of a matrix.
[Link](): Compute the determinant of a matrix.
[Link](): Compute the eigenvalues and eigenvectors of a matrix.
[Link](): Solve a system of linear equations.
[Link](): Compute the singular value decomposition of a matrix.
6. Random Number Generation
[Link](): Generate random values between 0 and 1 (uniform distribution).
[Link](): Generate random values from a normal distribution (mean = 0, std
= 1).
[Link](): Generate random integers within a specified range.
[Link](): Randomly choose elements from an array.
[Link](): Shuffle the elements of an array in place.
[Link](): Set the seed for random number generation (to get reproducible
results).
7. Array Operations
[Link](): Compute the sum of elements along a specified axis.
[Link](): Compute the product of elements along a specified axis.
[Link](): Compute the cumulative sum of elements.
[Link](): Compute the cumulative product of elements.
[Link](): Compute the difference between adjacent elements.
[Link](): Find the unique elements in an array.
[Link]() (return indices): Return the indices of unique elements.
np.intersect1d(): Find the intersection of two arrays.
np.setdiff1d(): Find the set difference of two arrays.
8. Boolean Operations
[Link](): Check if any element of an array is True.
[Link](): Check if all elements of an array are True.
[Link](): Test if elements of an array are present in another array.
[Link](): Test if elements are NaN.
9. Miscellaneous Functions
[Link](): Return the indices that would sort an array.
[Link](): Find indices where elements should be inserted to maintain order.
[Link](): Test if two arrays are element-wise equal within a tolerance.
[Link](): Limit the values in an array to a specified range.
[Link](): Delete elements from an array.
[Link](): Repeat elements of an array.
[Link](): Repeat an array to create a larger array.
10. Array Information Functions
[Link](): Return the shape (dimensions) of an array.
[Link](): Return the total number of elements in an array.
[Link](): Return the number of dimensions (axes) of an array.
[Link](): Return the data type of an array's elements.
[Link](): Return the size (in bytes) of each element of an array.
[Link](): Return the total number of bytes consumed by an array.
11. Set Operations
np.union1d(): Compute the union of two arrays.
np.intersect1d(): Compute the intersection of two arrays.
np.setdiff1d(): Compute the set difference between two arrays.
np.setxor1d(): Compute the set exclusive-or (symmetric difference) between two
arrays.
12. Set Operations for Multidimensional Arrays
[Link](): Return sorted, unique elements of an array.