15.4.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: Scaling With Training Statistics
# 1. Fit a StandardScaler on X_train only.
# 2. Use it to scale the new session below and print both scaled values,
# each rounded to 2 decimals.
# Print exactly:
# Scaled page value: -0.14
# Scaled duration: -0.5
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Shopping sessions (made-up numbers for practice): page value in dollars,
# seconds spent on product pages, and whether the session ended in a purchase.
sessions = pd.DataFrame({
"page_value": [0, 12, 0, 35, 0, 4, 28, 0, 0, 19, 0, 41, 0, 7, 0, 22],
"duration": [120, 1500, 300, 2400, 60, 900, 1300, 200, 2600, 1700, 150, 3100, 700, 450, 30, 1400],
"purchase": [0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
})
X = sessions[["page_value", "duration"]]
y = sessions["purchase"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=24, stratify=y)
new_session = pd.DataFrame({"page_value": [10], "duration": [600]})
### Your code starts here.
### Your code ends here.
Scaled page value: -0.14
Scaled duration: -0.5
### Question 2: A Pipeline Classifier
# 1. Make a pipeline of StandardScaler and KNeighborsClassifier with 3 neighbors.
# 2. Fit it on the training set, predict the test set, and print the
# predictions as a list and the test accuracy rounded to 2 decimals.
# Print exactly:
# Predictions: [1, 0, 0, 0]
# Test accuracy: 1.0
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
# Shopping sessions (made-up numbers for practice): page value in dollars,
# seconds spent on product pages, and whether the session ended in a purchase.
sessions = pd.DataFrame({
"page_value": [0, 12, 0, 35, 0, 4, 28, 0, 0, 19, 0, 41, 0, 7, 0, 22],
"duration": [120, 1500, 300, 2400, 60, 900, 1300, 200, 2600, 1700, 150, 3100, 700, 450, 30, 1400],
"purchase": [0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
})
X = sessions[["page_value", "duration"]]
y = sessions["purchase"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=24, stratify=y)
### Your code starts here.
### Your code ends here.
Predictions: [1, 0, 0, 0]
Test accuracy: 1.0
### Question 3: The Confusion Matrix
# 1. Count the true negatives, false positives, false negatives, and true
# positives, with "fraud" as the positive class (confusion_matrix with
# labels=["ok", "fraud"] gives them in that order with .ravel()).
# Print exactly:
# True negatives: 5
# False positives: 3
# False negatives: 1
# True positives: 3
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
# Twelve checked orders (made-up data for practice); fraud is the positive class.
actual = ["fraud", "ok", "ok", "fraud", "ok", "ok", "fraud", "ok", "ok", "ok", "fraud", "ok"]
predicted = ["fraud", "ok", "fraud", "fraud", "ok", "ok", "fraud", "ok", "fraud", "ok", "ok", "fraud"]
### Your code starts here.
### Your code ends here.
True negatives: 5
False positives: 3
False negatives: 1
True positives: 3
### Question 4: Precision, Recall, and F1
# 1. Compute precision, recall, and F1 for the fraud classifier, with "fraud"
# as the positive class. Round each to 3 decimals.
# Print exactly:
# Precision: 0.5
# Recall: 0.75
# F1: 0.6
import numpy as np
import pandas as pd
# Twelve checked orders (made-up data for practice); fraud is the positive class.
actual = ["fraud", "ok", "ok", "fraud", "ok", "ok", "fraud", "ok", "ok", "ok", "fraud", "ok"]
predicted = ["fraud", "ok", "fraud", "fraud", "ok", "ok", "fraud", "ok", "fraud", "ok", "ok", "fraud"]
### Your code starts here.
### Your code ends here.
Precision: 0.5
Recall: 0.75
F1: 0.6
### Question 5: Choosing by Cost
# A missed fraud case costs $200 and a false alarm costs $10.
# 1. Compute each model's total error cost from its false negatives and
# false positives on the same test orders.
# 2. Print each cost and the name of the cheaper model.
# Print exactly:
# Model A cost: $2800
# Model B cost: $2500
# Cheaper: Model B
import numpy as np
import pandas as pd
COST_FN = 200 # dollars lost for each missed fraud case (an assumption)
COST_FP = 10 # dollars spent checking each false alarm (an assumption)
errors = pd.DataFrame({
"model": ["Model A", "Model B"],
"false_negatives": [12, 5],
"false_positives": [40, 150],
})
### Your code starts here.
### Your code ends here.
Model A cost: $2800
Model B cost: $2500
Cheaper: Model B
Question 1
Question 2
Question 3
Question 4
Question 5