Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconMachine Learning Hero
Machine Learning Hero

Chapter 4: Supervised Learning Techniques

Practical Exercises Chapter 4

Exercise 1: Linear Regression

Task: You have the following dataset containing information about house prices. Use simple linear regression to predict the house price based on the size of the house.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Exercise 2: Polynomial Regression

Task: You are given a dataset with a non-linear relationship between years of experience and salary. Use polynomial regression to model this relationship.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Exercise 3: Classification with SVM

Task: Use an SVM classifier to predict whether a patient has heart disease based on two features: age and cholesterol level. Train the model using the following dataset:

Solution:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# 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)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Exercise 4: Precision and Recall Calculation

Task: You are working on a classification problem with the following true labels and predicted labels:

True Labels: [1, 0, 1, 1, 0, 1, 0, 0]

Predicted Labels: [1, 0, 1, 0, 0, 1, 0, 1]

Calculate the precision and recall for the positive class (1).

Solution:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Exercise 5: AUC-ROC Calculation

Task: You are given the predicted probabilities of a model for the positive class (heart disease) as follows:

Predicted Probabilities: [0.1, 0.4, 0.8, 0.6, 0.3]

True Labels: [0, 0, 1, 1, 0]

Calculate the AUC-ROC score and plot the ROC curve.

Solution:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Exercise 6: Hyperparameter Tuning with Random Forest

Task: You are using a Random Forest classifier to classify breast cancer cases. Perform randomized search to tune the hyperparameters, such as the number of estimators (n_estimators) and the maximum depth (max_depth), using the following ranges:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solution:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params

_)
print("Best cross-validation accuracy:", random_search.best_score_)

These practical exercises help reinforce key concepts in supervised learning, including regression, classification, evaluation metrics, and hyperparameter tuning. 

Practical Exercises Chapter 4

Exercise 1: Linear Regression

Task: You have the following dataset containing information about house prices. Use simple linear regression to predict the house price based on the size of the house.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Exercise 2: Polynomial Regression

Task: You are given a dataset with a non-linear relationship between years of experience and salary. Use polynomial regression to model this relationship.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Exercise 3: Classification with SVM

Task: Use an SVM classifier to predict whether a patient has heart disease based on two features: age and cholesterol level. Train the model using the following dataset:

Solution:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# 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)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Exercise 4: Precision and Recall Calculation

Task: You are working on a classification problem with the following true labels and predicted labels:

True Labels: [1, 0, 1, 1, 0, 1, 0, 0]

Predicted Labels: [1, 0, 1, 0, 0, 1, 0, 1]

Calculate the precision and recall for the positive class (1).

Solution:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Exercise 5: AUC-ROC Calculation

Task: You are given the predicted probabilities of a model for the positive class (heart disease) as follows:

Predicted Probabilities: [0.1, 0.4, 0.8, 0.6, 0.3]

True Labels: [0, 0, 1, 1, 0]

Calculate the AUC-ROC score and plot the ROC curve.

Solution:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Exercise 6: Hyperparameter Tuning with Random Forest

Task: You are using a Random Forest classifier to classify breast cancer cases. Perform randomized search to tune the hyperparameters, such as the number of estimators (n_estimators) and the maximum depth (max_depth), using the following ranges:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solution:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params

_)
print("Best cross-validation accuracy:", random_search.best_score_)

These practical exercises help reinforce key concepts in supervised learning, including regression, classification, evaluation metrics, and hyperparameter tuning. 

Practical Exercises Chapter 4

Exercise 1: Linear Regression

Task: You have the following dataset containing information about house prices. Use simple linear regression to predict the house price based on the size of the house.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Exercise 2: Polynomial Regression

Task: You are given a dataset with a non-linear relationship between years of experience and salary. Use polynomial regression to model this relationship.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Exercise 3: Classification with SVM

Task: Use an SVM classifier to predict whether a patient has heart disease based on two features: age and cholesterol level. Train the model using the following dataset:

Solution:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# 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)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Exercise 4: Precision and Recall Calculation

Task: You are working on a classification problem with the following true labels and predicted labels:

True Labels: [1, 0, 1, 1, 0, 1, 0, 0]

Predicted Labels: [1, 0, 1, 0, 0, 1, 0, 1]

Calculate the precision and recall for the positive class (1).

Solution:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Exercise 5: AUC-ROC Calculation

Task: You are given the predicted probabilities of a model for the positive class (heart disease) as follows:

Predicted Probabilities: [0.1, 0.4, 0.8, 0.6, 0.3]

True Labels: [0, 0, 1, 1, 0]

Calculate the AUC-ROC score and plot the ROC curve.

Solution:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Exercise 6: Hyperparameter Tuning with Random Forest

Task: You are using a Random Forest classifier to classify breast cancer cases. Perform randomized search to tune the hyperparameters, such as the number of estimators (n_estimators) and the maximum depth (max_depth), using the following ranges:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solution:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params

_)
print("Best cross-validation accuracy:", random_search.best_score_)

These practical exercises help reinforce key concepts in supervised learning, including regression, classification, evaluation metrics, and hyperparameter tuning. 

Practical Exercises Chapter 4

Exercise 1: Linear Regression

Task: You have the following dataset containing information about house prices. Use simple linear regression to predict the house price based on the size of the house.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Exercise 2: Polynomial Regression

Task: You are given a dataset with a non-linear relationship between years of experience and salary. Use polynomial regression to model this relationship.

Solution:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Exercise 3: Classification with SVM

Task: Use an SVM classifier to predict whether a patient has heart disease based on two features: age and cholesterol level. Train the model using the following dataset:

Solution:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# 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)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Exercise 4: Precision and Recall Calculation

Task: You are working on a classification problem with the following true labels and predicted labels:

True Labels: [1, 0, 1, 1, 0, 1, 0, 0]

Predicted Labels: [1, 0, 1, 0, 0, 1, 0, 1]

Calculate the precision and recall for the positive class (1).

Solution:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Exercise 5: AUC-ROC Calculation

Task: You are given the predicted probabilities of a model for the positive class (heart disease) as follows:

Predicted Probabilities: [0.1, 0.4, 0.8, 0.6, 0.3]

True Labels: [0, 0, 1, 1, 0]

Calculate the AUC-ROC score and plot the ROC curve.

Solution:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Exercise 6: Hyperparameter Tuning with Random Forest

Task: You are using a Random Forest classifier to classify breast cancer cases. Perform randomized search to tune the hyperparameters, such as the number of estimators (n_estimators) and the maximum depth (max_depth), using the following ranges:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solution:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params

_)
print("Best cross-validation accuracy:", random_search.best_score_)

These practical exercises help reinforce key concepts in supervised learning, including regression, classification, evaluation metrics, and hyperparameter tuning.