SwadhyAI Notes

All Python notes

Scale ⚖️

Why Scale?

When your data has different values, and even different measurement units, it can be difficult to compare them. For example, Weight (kg) vs Volume (cm3). 2300 vs 1.3. Algorithms based on distance (like K-Means or SVM) will be biased towards the larger numbers.

✨ Standardization

Standardization scales data so that the mean is 0 and the standard deviation is 1.

Formula: z = (x - u) / s

python
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()

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

scaledX = scale.fit_transform(X)

print(scaledX)

🔮 Predicting with Scale

When you train your model on scaled data, you must scale any new data you want to predict.

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

# Scale the input before predicting
scaled = scale.transform([[2300, 1.3]])

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

💡 Did You Know?

Another common scaling method is Normalization (Min-Max Scaling), which scales values between 0 and 1.