Scale data to -1 to 1

Advertisements

I have rows of data containing numbers in the range of "-finite to finite". I want to transform this number to the range of "-1 to 1" as it shows polarity. I would like to enter the result into a new column inside the same dataframe. Here’s a sample of my data…

df = pd.DataFrame({
    'reviewId': ['01', '02', '03', '04', '05'],
    'score': [-1, -5, 0, 3, 38]})

>Solution :

You can use MinMaxScaler from sklearn.preprocessing to transform to a specific range:

Code:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame({
    'reviewId': ['01', '02', '03', '04', '05'],
    'score': [-1, -5, 0, 3, 38]})

scaler = MinMaxScaler(feature_range=(-1, 1))
df['polarity'] = scaler.fit_transform(df[['score']])

print(df)

Output:

  reviewId  score  polarity
0       01     -1 -0.813953
1       02     -5 -1.000000
2       03      0 -0.767442
3       04      3 -0.627907
4       05     38  1.000000

Leave a Reply Cancel reply