Machine Learning Basics
Before diving into algorithms, it's crucial to understand the mathematical foundations and data manipulation libraries that power standard ML pipelines in Python: Numpy and Pandas.
Numpy Matrices
Numpy provided support for large, multi-dimensional arrays and matrices.
import numpy as np
# Define Matrix A
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Define Matrix B
B = np.array([[1, 2, 0],
[3, 4, 2],
[4, 7, 8]])
Matrix Operations
Linear algebra operations are the backbone of neural networks.
Addition
# Element-wise Addition
print(np.add(A, B))
# Output:
# [[ 2 4 3]
# [ 7 9 8]
# [11 15 17]]
Matrix Multiplication (Dot Product)
# Dot Product
print(np.dot(A, B))
# Output:
# [[ 19 31 28]
# [ 43 70 58]
# [ 67 109 88]]
Transpose
# Transpose of A (Rows become columns)
print(A.transpose())
# Output:
# [[1 4 7]
# [2 5 8]
# [3 6 9]]
Pandas DataFrames
Pandas provides high-performance data structures like the DataFrame, which is essentially a programmable spreadsheet.
import pandas as pd
# Loading a CSV file
df = pd.read_csv('CarPrice_1.csv', index_col=0)
# Display first 5 rows
print(df.head())
# Check data types
print(df.info())