w3resource

Scaling Numerical Features Using RobustScaler in Pandas


Pandas: Machine Learning Integration Exercise-15 with Solution


Write a Pandas program to scale numerical features using Scikit-learn's RobustScaler.

This exercise shows how to scale numerical features using Scikit-learn's RobustScaler to reduce the effect of outliers.

Sample Solution :

Code :

import pandas as pd
from sklearn.preprocessing import RobustScaler

# Load the dataset
df = pd.read_csv('data.csv')

# Initialize the RobustScaler
scaler = RobustScaler()

# Apply RobustScaler to the 'Age' and 'Salary' columns
df[['Age', 'Salary']] = scaler.fit_transform(df[['Age', 'Salary']])

# Output the scaled dataset
print(df)

Output:

   ID      Name  Age  Gender    Salary  Target
0   1      Sara -0.8  Female -0.666667       0
1   2    Ophrah  0.2    Male  0.000000       1
2   3    Torben -1.4    Male  0.666667       0
3   4  Masaharu  1.2    Male  1.333333       1
4   5      Kaya  NaN  Female -0.333333       0
5   6   Abaddon  0.0    Male       NaN       1

Explanation:

  • Loaded the dataset using Pandas.
  • Initialized RobustScaler to scale features while reducing the influence of outliers.
  • Applied RobustScaler to the 'Age' and 'Salary' columns.
  • Displayed the scaled dataset.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.