Recap: Clean and Predict
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 49 of 58.
Challenge
MediumWhich campers will come back? camp.csv mixes words, numbers and gaps; the starter splits it and reads a camper.
- Build the chapter's full pipeline: one-hot encode
cabinandactivity, give the numbers a lane that fills gaps with medians, then scales, and end with logistic regression. - Print its 5-fold CV accuracy on the training rows, mean and std.
- 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.91The 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
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler