Appendix C: Sample problems and solutions

Instructions for customization

This section is optional and is intended for a collection of practice problems with detailed solutions to aid in understanding and application of complex concepts (the example question below is relevant to computer science).

Use the template structure provided to maintain consistency in your examples. After making your edits, save and review.

Once you have completed your sample problems and solutions section, delete this instruction box.

Practice Problem A

This practice problem and solution demonstrate the application of logistic regression, a fundamental AI concept, to solve a binary classification problem.

Consider a simple binary classification problem where you have a dataset containing information about student performance (features) and whether they passed or failed (label). You want to build a machine learning model to predict whether a student will pass or fail based on their performance features.

The dataset

Features:

  • Hours spent studying (continuous)
  • Previous test scores (continuous)
  • Attendance percentage (continuous)

Label:

Pass (1) or Fail (0)
Your task is to build a logistic regression model to predict whether a student will pass or fail based on the provided features.

The solution

Step 1: Data preprocessing

  • Load the dataset and split it into features (X) and labels (y).
  • Split the dataset into training and testing sets (e.g., 80% for training, 20% for testing).

Step 2: Model training

  • Import the logistic regression model from a machine learning library (e.g., scikit-learn).
  • Initialize the logistic regression model.
  • Fit the model to the training data (X_train, y_train) using the .fit() method.

Step 3: Model evaluation

  • Predict the labels for the testing data (X_test) using the .predict() method.
  • Evaluate the performance of the model using metrics such as accuracy, precision, recall, and F1-score.
  • You can use classification_report from sklearn.metrics for a detailed performance evaluation.

Step 4: Interpretation

  • Examine the coefficients of the logistic regression model to understand the importance of each feature in predicting student outcomes.
  • Visualize the decision boundary to understand how the model separates pass and fail instances in the feature space.

Example Code (using Python and scikit-learn):

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

 

# Step 1: Data Preprocessing
# Load dataset
# Assuming X contains feature columns and y contains label column
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

 

# Step 2: Model Training
# Initialize logistic regression model
model = LogisticRegression()
# Fit the model to the training data
model.fit(X_train, y_train)

 

# Step 3: Model Evaluation
# Predict labels for testing data
y_pred = model.predict(X_test)
# Evaluate model performance
print(classification_report(y_test, y_pred))

 

# Step 4: Interpretation
# Print coefficients
print(“Coefficients:”, model.coef_)

 

License

Icon for the Creative Commons Attribution 4.0 International License

Campus Manitoba Pressbooks Template Copyright © 2024 by Campus Manitoba is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book