Mixed Columns
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 46 of 58.
Most tables mix words and numbers, and each kind needs its own step. A ColumnTransformer sends each column to its step and joins the results:
For a record shop, in code:
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
prep = ColumnTransformer([
("words", OneHotEncoder(), ["genre", "condition"]),
("numbers", StandardScaler(), ["price", "year"]),
])
pipe = make_pipeline(prep, LogisticRegression(max_iter=1000))Each entry is a name, a step and its columns. Unlike get_dummies, the encoder remembers the training categories, so one new record gets the same 7 columns.
Challenge
MediumA chess club wants each player's win chance before a game. The starter splits chess.csv and reads tonight's game.
- Encode
openingandcolor, scalerating_gapandminutes, and print how many columns that makes from the training rows. - Chain it with logistic regression, fit, and print the test accuracy.
- Print tonight's chance of a win.
Another club, three openings, and sicilian, black, 50, 15:
Columns after preparing: 7
Test accuracy: 0.805
Win chance: 0.66Try it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
# A chess club's games: the opening, the player's color, their rating minus
# the opponent's, minutes on the clock, and whether the player won (1) or not (0)
games = pd.read_csv("chess.csv")
X = games[["opening", "color", "rating_gap", "minutes"]]
y = games["won"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# Tonight's game, one row with the same columns
game = pd.DataFrame({"opening": [input()], "color": [input()],
"rating_gap": [int(input())], "minutes": [int(input())]})
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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