📚 Comprehensive NumPy Course in Python
Introduction to NumPy
NumPy is a fundamental library for numerical computing in Python. It provides support for arrays, matrices, and a large number of mathematical functions to operate on these data structures.
Installing NumPy
You can install NumPy using pip:
pip install numpy
Key Functions in NumPy
1. Creating Arrays
NumPy provides several functions to create arrays:
np.array()
: Creates an array from a list or tuple.np.zeros()
: Creates an array filled with zeros.np.ones()
: Creates an array filled with ones.np.arange()
: Creates an array with a range of values.np.linspace()
: Creates an array with evenly spaced values.
Examples:
import numpy as np
# Creating an array from a list
arr1 = np.array([1, 2, 3])
print(arr1)
# Creating an array filled with zeros
arr2 = np.zeros((2, 3))
print(arr2)
# Creating an array with a range of values
arr3 = np.arange(0, 10, 2)
print(arr3)
2. Array Properties
Arrays have properties that help understand their structure:
ndim
: Number of dimensions.shape
: Shape of the array.size
: Total number of elements.
Example:
# Properties of an array
print("Number of dimensions:", arr1.ndim)
print("Shape of array:", arr2.shape)
print("Total elements:", arr3.size)
3. Array Manipulation
Manipulating arrays is easy with NumPy:
np.reshape()
: Changes the shape of an array.np.flatten()
: Flattens a multi-dimensional array.np.concatenate()
: Joins two or more arrays.np.split()
: Splits an array into multiple sub-arrays.
Examples:
# Reshaping an array
arr4 = np.arange(6).reshape(2, 3)
print(arr4)
# Flattening an array
flat_arr = arr4.flatten()
print(flat_arr)
# Concatenating arrays
arr5 = np.array([[7, 8, 9]])
concat_arr = np.concatenate((arr4, arr5), axis=0)
print(concat_arr)
4. Mathematical Operations
NumPy allows for element-wise operations:
np.add()
,np.subtract()
,np.multiply()
,np.divide()
: Basic arithmetic operations.np.dot()
: Dot product of two arrays.np.mean()
,np.median()
,np.std()
: Statistical operations.
Examples:
arr6 = np.array([1, 2, 3])
arr7 = np.array([4, 5, 6])
# Basic arithmetic operations
print("Addition:", np.add(arr6, arr7))
print("Dot product:", np.dot(arr6, arr7))
# Statistical operations
print("Mean:", np.mean(arr6))
5. Indexing and Slicing
Accessing elements in an array:
Example:
arr8 = np.array([[1, 2, 3], [4, 5, 6]])
# Indexing
print(arr8[0, 1]) # Access element in first row, second column
# Slicing
print(arr8[:, 1]) # Access all rows, second column
6. Boolean Indexing
Filter arrays using boolean conditions:
Example:
arr9 = np.array([1, 2, 3, 4, 5])
bool_idx = arr9 > 2
print(arr9[bool_idx]) # Outputs elements greater than 2
7. Working with Random Numbers
Generate random numbers using NumPy:
np.random.rand()
: Generates random floats between 0 and 1.np.random.randint()
: Generates random integers.
Examples:
random_arr = np.random.rand(3, 2)
print("Random Array:", random_arr)
random_int = np.random.randint(0, 10, size=(2, 3))
print("Random Integers Array:", random_int)
NumPy Basics: A Simple Guide
What is NumPy?
NumPy is a powerful library for numerical computing in Python, providing support for arrays and a variety of mathematical functions.
Installing NumPy
To install NumPy, run:
pip install numpy
Key Functions
1. Creating Arrays
import numpy as np
# From a list
arr = np.array([1, 2, 3])
# Filled with zeros
zeros = np.zeros((2, 3))
# Range of values
range_arr = np.arange(0, 10, 2)
2. Array Properties
print(arr.ndim) # Number of dimensions
print(zeros.shape) # Shape of the array
print(range_arr.size) # Total elements
3. Array Manipulation
reshaped = range_arr.reshape(2, 5) # Change shape
flattened = reshaped.flatten() # Flatten array
4. Mathematical Operations
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Addition
result = np.add(arr1, arr2)
5. Indexing and Slicing
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Access element
element = matrix[0, 1] # First row, second column
# Slice
slice_arr = matrix[:, 1] # All rows, second column
6. Boolean Indexing
bool_idx = arr1 > 2
filtered = arr1[bool_idx] # Elements greater than 2
7. Random Numbers
random_arr = np.random.rand(3, 2) # Random floats
random_int = np.random.randint(0, 10, size=(2, 3)) # Random integers
Conclusion
Explore these functions to leverage NumPy for data analysis and scientific computing!
Comments
Post a Comment