3

I am trying to scale a pandas or numpy array from 0 to a unknown max value with the defined number replaced with 1.

One solution I tried is just dividing the defined number I want by the array.

test = df['Temp'] / 33

plot

This method does not scale all the way from 0 and I'm stuck trying to figure out a better mathematical way of solving this.

BillyRay
  • 413
  • 2
  • 10
  • 24

2 Answers2

3

First, transform the DataFrame to a numpy array

import numpy as np
T = np.array(df['Temp'])

Then scale it to a [0, 1] interval:

def scale(A):
    return (A-np.min(A))/(np.max(A) - np.min(A))

T_scaled = scale(T)

Then transform it to anywhere you want, e.g. to [55..100]

T2 = 55 + 45*T_scaled

I'm sure that this can be done within Pandas too (but I'm not familiar with it). Perhaps you might study Pandas df.apply()

pyano
  • 1,885
  • 10
  • 28
  • Thanks, I was looking for more of an automated way to do this but I guess transforming the normalized function by 0 + 1.35*scaled works. I just have to keep adjusting until It looks right. – BillyRay Apr 08 '19 at 21:24
  • Wow I can't believe I could not figure it out. I just need to replace np.max() with the number I want to be the max! – BillyRay Apr 08 '19 at 21:27
  • 1
    Thanks ! Actually I do not understand the meaning of `0 to a unknown max value with the defined number replaced with 1` . Where do you get the unknown number from ? Do you want to give an example with numbers ? – pyano Apr 08 '19 at 21:30
  • 1
    Great ! So the unknowm number comes from outside and not from the curve it's self . – pyano Apr 08 '19 at 21:32
  • I just posted the answer below. Basically I want to scaled the data from 0 - 100 percent with overshoot as I'm trying to measure a PID response and calculate over under performance from the stable region. – BillyRay Apr 08 '19 at 21:33
1
scaled = (df['Temp']-df['Temp'].min()) / (33 - df['Temp'].min())

Just replace 33 with the number to want your data scaled to!

Original Scaled

BillyRay
  • 413
  • 2
  • 10
  • 24