EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Data Science

Machine Learning Fundamentals

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}")
End of lesson.