Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Scale data to -1 to 1

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading