Machine Learning Lab
Python basics: NumPy arrays, Pandas DataFrames, plotting with Matplotlib
NumPy Arrays:-
import numpy as np
# Create a 1D array
arr1 = [Link]([1, 2, 3, 4, 5])
# Create a 2D array
arr2 = [Link]([[1, 2, 3], [4, 5, 6]])
# Array operations
print(arr1 + 10) # Add 10 to each element
print(arr2 * 2) # Multiply all elements by 2
NumPy Arrays:-
# Useful properties
print([Link]) # (2, 3)
print([Link]) # int64 (or similar)
print([Link]) # 2 dimensions
# Basic functions
print([Link](arr1)) # Average
print([Link](arr1)) # Maximum
print([Link](0, 1, 5)) # Evenly spaced numbers
Pandas DataFrames
import pandas as pd # Accessing data
# Create DataFrame from dictionary print(df['Name']) # Column
print([Link][0]) # Row by label
data = {
print([Link][1]) # Row by index
'Name': ['Ali', 'Sara', 'John'],
# Basic statistics
'Age': [25, 30, 22],
print([Link]())
'Score': [85, 90, 88]
# Filtering
}
print(df[df['Age'] > 23])
df = [Link](data)
# Add new column
# Display
df['Passed'] = df['Score'] > 80
print(df)
print(df)
Plotting with Matplotlib
import [Link] as plt [Link]("Sine Wave")
import numpy as np [Link]("x-axis")
# Generate data [Link]("y-axis")
x = [Link](0, 10, 100) [Link]()
y = [Link](x) [Link](True)
# Create a simple line plot [Link]()
[Link](x, y, label='sin(x)',
color='blue')
Other Plot Types:
# Bar plot
[Link](['A', 'B', 'C'], [5, 3, 7])
# Scatter plot
[Link](x, [Link](x), color='red')
# Histogram
data = [Link](1000)
[Link](data, bins=30,
color='orange', edgecolor='black')
Other Plot Types:
| Library | Main Purpose | Key Data Structure | Example Function
| **NumPy** | Fast numerical computation | ndarray | `[Link]()`, `[Link]()` |
| **Pandas**| Data manipulation & analysis | DataFrame, Series |`[Link]()`
| **Matplotlib**| Data visualization | Figure, Axes | `[Link]()` |