Lab

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.

Hide code cell source

### Solution
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]})

scaler = StandardScaler().fit(X_train)
scaled = scaler.transform(new_session)[0]
print("Scaled page value:", round(scaled[0], 2))
print("Scaled duration:", round(scaled[1], 2))
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.

Hide code cell source

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

model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=3))
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Predictions:", predictions.tolist())
print("Test accuracy:", round((predictions == y_test).mean(), 2))
# Without StandardScaler, duration (in the thousands of seconds) swamps page
# value in the distance, and the classifier gets one of the four test sessions wrong.
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.

Hide code cell source

### Solution
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"]

tn, fp, fn, tp = confusion_matrix(actual, predicted, labels=["ok", "fraud"]).ravel()
print("True negatives:", tn)
print("False positives:", fp)
print("False negatives:", fn)
print("True positives:", tp)
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.

Hide code cell source

### Solution
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"]

tp = sum([a == "fraud" and p == "fraud" for a, p in zip(actual, predicted)])
fp = sum([a == "ok" and p == "fraud" for a, p in zip(actual, predicted)])
fn = sum([a == "fraud" and p == "ok" for a, p in zip(actual, predicted)])
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print("Precision:", round(precision, 3))
print("Recall:", round(recall, 3))
print("F1:", round(f1, 3))
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.

Hide code cell source

### Solution
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],
})

errors["cost"] = COST_FN * errors["false_negatives"] + COST_FP * errors["false_positives"]
for name, cost in zip(errors["model"], errors["cost"]):
    print(name, "cost: $" + str(cost))
print("Cheaper:", errors.loc[errors["cost"].idxmin(), "model"])
# Model B is wrong far more often (155 errors against 52), yet cheaper,
# because it misses fewer of the costly fraud cases.
Model A cost: $2800
Model B cost: $2500
Cheaper: Model B