Menu
CoddyTech

Recap: Clean and Predict

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 49 of 58.

challenge icon

Challenge

Medium

Which campers will come back? camp.csv mixes words, numbers and gaps; the starter splits it and reads a camper.

  1. Build the chapter's full pipeline: one-hot encode cabin and activity, give the numbers a lane that fills gaps with medians, then scales, and end with logistic regression.
  2. Print its 5-fold CV accuracy on the training rows, mean and std.
  3. Fit it on the training rows; print the test accuracy and the camper's chance to return.

Another camp, camper 10, lakeside, archery, 21, -, 4:

CV accuracy: mean 0.707, std 0.065
Test accuracy: 0.740
Chance of coming back: 0.91

The dash is a missing rating: your pipeline fills it like any other gap.

Try it yourself

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

# Last summer's campers: did they come back this year (1) or not (0)?
campers = pd.read_csv("camp.csv")
X = campers.drop(columns="came_back")
y = campers["came_back"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)

# A new camper, one answer per line; a dash (-) is a missing number
answers = []
for i in range(6):
    answers.append(input())
numbers = []
for i in [0, 3, 4, 5]:
    if answers[i] == "-":
        numbers.append(np.nan)
    else:
        numbers.append(float(answers[i]))
camper = pd.DataFrame({"age": [numbers[0]], "cabin": [answers[1]], "activity": [answers[2]],
                       "nights": [numbers[1]], "rating": [numbers[2]], "friends_made": [numbers[3]]})

# Write your code below

All lessons in Supervised Learning

Practice on your own: Online Python compiler