Machine Learning

Machine Learning - Scale

Scale Features

Feature engineering is the process of creating, transforming or selecting the most relevant variables (features) from raw data to improve model performance. Effective features help the model capture important patterns and relationships in the data. It directly contributes to model building in the following ways:

Machine Learning

When your data has different values, and even different measurement units, it can be difficult to compare them. What is kilograms compared to meters? Or altitude compared to time?

The answer to this problem is scaling. We can scale data into new values that are easier to compare.

Take a look at the table below, it is the same data set that we used in the multiple regression chapter, but this time the volume column contains values in liters instead of cm³ (1.0 instead of 1000).

CarModelVolumeWeightCO2
ToyotaAygo1.079099
MitsubishiSpace Star1.2116095
SkodaCitigo1.092995
Fiat5000.986590
MiniCooper1.51140105

It can be difficult to compare the volume 1.0 with the weight 790, but if we scale them both into comparable values, we can easily see how much one value is compared to the other.

There are different methods for scaling data, in this tutorial we will use a method called standardization.

z = (x - u) / s

Where z is the new value, x is the original value, u is the mean and s is the standard deviation.

If you take the weight column from the data set above, the first value is 790, and the scaled value will be:

(790 - 1292.23) / 238.74 = -2.1

If you take the volume column from the data set above, the first value is 1.0, and the scaled value will be:

(1.0 - 1.61) / 0.38 = -1.59

Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0.

Example

Scale all values in the Weight and Volume columns:

import pandas
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()

df = pandas.read_csv("data.csv")

X = df[['Weight', 'Volume']]

scaledX = scale.fit_transform(X)

print(scaledX)

Result:

[-2.10389253-1.59336644 ]
[-0.55407235-1.07190106 ]
[-1.52166278-1.59336644 ]
[-1.78973979-1.85409913 ]
[-0.63784641-0.28970299 ]
[-1.52166278-1.59336644 ]
[-0.76769621-0.55043568 ]
[0.30461180-0.28970299 ]
[-0.75513010-0.28970299 ]
[-0.59595938-0.02897030 ]
[-1.30803892-1.33263375 ]
[-1.26615189-0.81116837 ]
[-0.75513010-1.59336644 ]
[-0.16871166-0.02897030 ]
[0.14125238-0.02897030 ]
[0.15800719-0.02897030 ]
[0.30461180-0.02897030 ]
[-0.051427971.53542584 ]
[-0.72580918-0.02897030 ]
[0.149629791.01396046 ]
[1.22193780-0.02897030 ]
[0.568500101.01396046 ]
[0.304611801.27469315 ]
[0.51404696-0.02897030 ]
[0.514046961.01396046 ]
[0.72348212-0.28970299 ]
[0.828199701.01396046 ]
[1.812544951.01396046 ]
[0.96642691-0.02897030 ]
[1.728770891.01396046 ]
[1.309900571.27469315 ]
[1.900507721.01396046 ]
[-0.23991961-0.02897030 ]
[0.40932938-0.02897030 ]
[0.47215993-0.02897030 ]
[0.430272902.31762392 ]]

Predict CO2 Values

The task in the Multiple Regression chapter was to predict the CO2 emission from a car when you only knew its weight and volume.

When the data set is scaled, you will have to use the scale when you predict values:

Example

Predict the CO2 emission from a 1.3 liter car that weighs 2300 kilograms:

import pandas
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()

df = pandas.read_csv("data.csv")

X = df[['Weight', 'Volume']]
y = df['CO2']

scaledX = scale.fit_transform(X)

regr = linear_model.LinearRegression()
regr.fit(scaledX, y)

scaled = scale.transform([[2300, 1.3]])

predictedCO2 = regr.predict([scaled[0]])
print(predictedCO2)

Result:

[107.2087328]