Menu
CoddyTech

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:

A diagram titled 4 columns in, 7 columns out. Three vinyl records with genre, condition, price and year. Genre and condition go through OneHotEncoder into five 0/1 columns: jazz, rock, good, mint, worn. Price and year go through StandardScaler into two scaled columns

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 icon

Challenge

Medium

A chess club wants each player's win chance before a game. The starter splits chess.csv and reads tonight's game.

  1. Encode opening and color, scale rating_gap and minutes, and print how many columns that makes from the training rows.
  2. Chain it with logistic regression, fit, and print the test accuracy.
  3. 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.66

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler