Open In Colab

Homework 7: Classification#

Apply classification methods to chemical engineering problems.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.preprocessing import StandardScaler

Problem 1: Quality Control Classification#

Classify product batches as pass or fail based on process measurements.

# Load QC classification data from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw07_qc_classification.csv"
qc_data = pd.read_csv(url)

print(f"Pass rate: {qc_data['quality'].mean():.1%}")
qc_data.head(10)
Pass rate: 9.5%
temp_deviation pressure_deviation viscosity quality
0 0.993428 0.536681 91.481950 0
1 -0.276529 0.841177 77.340707 0
2 1.295377 1.624577 55.637550 0
3 3.046060 1.580703 136.472238 0
4 -0.468307 -2.066504 131.290101 0
5 -0.468274 -1.406738 149.971767 0
6 3.158426 0.772553 149.663684 0
7 1.534869 0.770679 105.543171 1
8 -0.938949 0.772572 126.898742 1
9 1.085120 5.779097 144.476573 0

1a. Split data 80/20 and train a logistic regression classifier. Report accuracy on both train and test sets.

# Your code here

1b. Create a confusion matrix for the test set. What are the false positive and false negative rates?

# Your code here

1c. In QC, which error is worse: false positive (predicting pass when fail) or false negative (predicting fail when pass)? How could you adjust the model to reduce the worse error?

Your answer here:

1d. Print the classification report showing precision, recall, and F1 score for each class.

# Your code here

Problem 2: k-Nearest Neighbors#

Classify catalyst type from characterization data.

# Load catalyst classification data from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw07_catalyst_classification.csv"
cat_data = pd.read_csv(url)
cat_data.head()
surface_area pore_volume acidity activity catalyst_type
0 -1.231892 -0.762303 -1.043321 -0.331683 2
1 -0.519458 3.067128 -2.639405 -0.863447 1
2 -1.025285 -1.162364 -1.501053 -0.090242 2
3 -1.148635 -0.552102 -0.090918 -0.416949 2
4 0.298951 1.057167 -1.086206 -0.047807 1

2a. Scale the features and train a KNN classifier with k=5. Report test accuracy.

# Your code here

2b. Use cross-validation to find the best k from [1, 3, 5, 7, 9, 11]. Plot accuracy vs k.

# Your code here

2c. Why is feature scaling important for KNN but not for logistic regression?

Your answer here:

Problem 3: Model Comparison#

3a. Compare logistic regression and KNN using 5-fold cross-validation on the catalyst data. Which performs better?

# Your code here

3b. Create a 2D visualization: plot surface_area vs pore_volume colored by predicted class (use your best model).

# Your code here

3c. When would you prefer logistic regression over KNN for a classification problem?

Your answer here: