CodeBlanks Logo Icon CodeBlanks

Numerical features

Numerical features are scaled to improve model performance.

Numeric transformations

Numeric features can also be transformed to make them more useful. For right-skewed distributions like income, the logarithm compresses the extreme values:

gelir = np.array([20, 25, 30, 40, 500])
print(np.log1p(gelir).round(2))   # [3.04 3.26 3.43 3.71 6.22]

np.log1p(x) means log(1 + x) and works safely at 0 too.

Binning and polynomials

A continuous value can be split into ranges (KBinsDiscretizer, np.digitize). To teach a linear model a curved relationship, PolynomialFeatures adds squares and products:

yas = np.array([15, 25, 45, 70])
print(np.digitize(yas, [18, 40, 65]))   # [0 1 2 3]  young, adult, middle, old

from sklearn.preprocessing import PolynomialFeatures
print(PolynomialFeatures(2, include_bias=False).fit_transform([[2, 3]]))
# [[2. 3. 4. 6. 9.]]  x1, x2, x1², x1·x2, x2²

The number of polynomial features grows fast; degree 2 or 3 is usually enough.

Practice Teach a curve to a linear model

For the relationship y = x², train a plain LinearRegression and a LinearRegression extended with PolynomialFeatures(2). Print the R² score (score) of both with round(..., 3) on separate lines.

Starter code

model.py
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

X = np.arange(-5, 6).reshape(-1, 1)
y = X.ravel() ** 2

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

X = np.arange(-5, 6).reshape(-1, 1)
y = X.ravel() ** 2

duz = LinearRegression().fit(X, y)
Xp = PolynomialFeatures(2).fit_transform(X)
egri = LinearRegression().fit(Xp, y)
print(round(duz.score(X, y), 3))
print(round(egri.score(Xp, y), 3))

Expected output

0.0
1.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 For which kind of distribution is the log transform most useful?

  • Right-skewed, long-tailed Correct answer
  • Symmetric
  • Categorical
  • Negative-valued

Explanation: It compresses large values and makes the distribution more symmetric.

2 Split the values into groups by the boundaries.

Question
grup = np.____(yas, [18, 40, 65])
Answer
grup = np.digitize(yas, [18, 40, 65])

Explanation: digitize gives the index of the range each value falls into.

3 Match the transformations with their purposes.

Term Meaning
np.log1p Reduces skew
np.digitize Splits into ranges
PolynomialFeatures Adds curved relations
x1·x2 Interaction feature

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation