Machine Learning Fundamentals

Supervised, Unsupervised, and Reinforcement Learning

Supervised Learning

Supervised learning involves training a model on a labeled dataset, meaning that each training example is paired with an output label. The model learns to map inputs to the corresponding output, which can then be used to predict the labels for new, unseen data.

  • Example: Classification (e.g., spam detection) and regression (e.g., predicting house prices).
  • Key Algorithms: Linear regression, logistic regression, decision trees, support vector machines (SVM), k-nearest neighbors (KNN).
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Example data: predict house prices based on square footage
X = np.array([[1500], [2000], [2500], [3000], [3500]])  # Square footage
y = np.array([300000, 400000, 500000, 600000, 700000])  # Prices

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

Unsupervised Learning

Unsupervised learning involves training a model on data that does not have labeled responses. The model tries to learn the underlying structure of the data, such as identifying clusters or reducing the dimensionality of the data.

  • Example: Clustering (e.g., customer segmentation) and dimensionality reduction (e.g., principal component analysis).
  • Key Algorithms: K-means clustering, hierarchical clustering, DBSCAN, principal component analysis (PCA), t-SNE.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Example data: predict house prices based on square footage
X = np.array([[1500], [2000], [2500], [3000], [3500]])  # Square footage
y = np.array([300000, 400000, 500000, 600000, 700000])  # Prices

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

Reinforcement Learning

Reinforcement learning involves an agent that learns to make decisions by taking actions in an environment to maximize a cumulative reward. The agent learns through trial and error, receiving feedback from the environment in the form of rewards or penalties.

  • Example: Game playing (e.g., chess, Go) and robotics.
  • Key Algorithms: Q-learning, deep Q-networks (DQN), policy gradients, SARSA (State-Action-Reward-State-Action).
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Example data: predict house prices based on square footage
X = np.array([[1500], [2000], [2500], [3000], [3500]])  # Square footage
y = np.array([300000, 400000, 500000, 600000, 700000])  # Prices

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

Key Algorithms

Regression

Regression algorithms are used for predicting a continuous output variable based on one or more input variables.

  • Linear Regression: Models the relationship between input features and the output as a linear equation.y=β0+β1×1+β2×2+…+βnxn+εy = β₀ + β₁x₁ + β₂x₂ + … + βₙxₙ + ε y=β0+β1×1+β2×2+…+βnxn+εwhere y is the predicted output, x₁, x₂, ..., xₙ are the input features, β₀, β₁, ..., βₙ are the coefficients, and ε is the error term.
  • Logistic Regression: Used for binary classification problems. It models the probability that a given input belongs to a certain class.P(y=1)=1/(1+e(−z))P(y=1) = 1 / (1 + e^(-z)) P(y=1)=1/(1+e(−z))where z = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ.

Decision Trees

Decision trees are a non-parametric supervised learning method used for classification and regression. A decision tree is a flowchart-like structure where:

  • Nodes represent tests on features.
  • Branches represent the outcome of the test.
  • Leaves represent the final prediction (either a class label or a regression value).

The model splits the data based on feature values that result in the most significant information gain (or lowest Gini impurity/entropy).

Support Vector Machines (SVM)

SVMs are supervised learning algorithms used for classification and regression tasks. The goal of an SVM is to find a hyperplane in an N-dimensional space (N being the number of features) that distinctly classifies the data points.

  • Linear SVM: Finds the linear hyperplane that best separates the classes.
  • Kernel SVM: Uses kernel tricks to handle non-linear classification problems by transforming the input data into a higher-dimensional space where a linear separator can be found.

Model Evaluation and Validation

Model evaluation and validation are crucial steps in developing machine learning models to ensure that they perform well on unseen data.

Model Evaluation Metrics

  • Accuracy: The proportion of correctly classified instances over the total number of instances.
  • Precision: The ratio of true positives to the sum of true positives and false positives. Useful in situations where the cost of false positives is high.
  • Recall (Sensitivity): The ratio of true positives to the sum of true positives and false negatives. Useful when the cost of false negatives is high.
  • F1-Score: The harmonic mean of precision and recall, providing a balance between the two.
  • Mean Squared Error (MSE): Used for regression tasks, it measures the average squared difference between the actual and predicted values.
  • AUC-ROC (Area Under the Curve – Receiver Operating Characteristic): Measures the ability of a classifier to distinguish between classes.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier

# Load the iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# Create a decision tree classifier
model = DecisionTreeClassifier()

# Perform 5-fold cross-validation
scores = cross_val_score(model, X, y, cv=5)

# Print the evaluation metrics
print(f"Cross-Validation Scores: {scores}")
print(f"Mean Accuracy: {scores.mean()}")

Model Validation Techniques

  • Train-Test Split: Split the dataset into a training set to train the model and a test set to evaluate it. A common split ratio is 80/20.
  • Cross-Validation: Divides the dataset into k folds (e.g., 5 or 10). The model is trained on k-1 folds and tested on the remaining fold. This process is repeated k times, and the results are averaged. This helps ensure that the model generalizes well to unseen data.
  • Bootstrapping: Involves sampling the dataset with replacement to create multiple training datasets. The model is trained on these datasets and evaluated on the samples not included in the training set (out-of-bag samples).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *