Home > Software design >  Scale data to -1 to 1
Scale data to -1 to 1

Time:01-18

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]})

CodePudding user response:

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

CodePudding user response:

import numpy as np
import pandas as pd

def normalize(x, newRange=(0, 1)): 
    xmin, xmax = np.min(x), np.max(x) 
    norm = (x - xmin)/(xmax - xmin) 
    if newRange == (0, 1):
        return(norm) 
    elif newRange != (0, 1):
        return norm * (newRange[1] - newRange[0])   newRange[0] 

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

polarity = normalize(df.score.array, newRange=(-1,1))

df["polarity"] = polarity
  • Related