1

I have a function that rescales an array to values between 0.0 and 1.0, but I am stuck as to how to rescale the returned array to a user-defined new min/max range. The questions prompt is "Rewrite the rescale function so that it scales data to lie between 0.0 and 1.0 by default, but will allow the caller to specify lower and upper bounds if they want"

Below is the code for my current array, and tests. I know I will have to add an additional default min/max parameter, but I am fairly new to python, and programming in general, and I am at a loss of how to proceed. Any help is appreciated.

import numpy

def rescale(input): 

    revalue = []
    Amax = input.max()
    Amin = input.min()
    print Amax
    print Amin
    print input

    for x in input:
        value = [(x-Amin)/(Amax-Amin)]
        revalue.append(value)
    print revalue

# Tests:

rescale(numpy.arange(10, dtype=np.float))


# Output 
9.0
0.0
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[[0.0], [0.1111111111111111], [0.22222222222222221], [0.33333333333333331], [0.44444444444444442], [0.55555555555555558], [0.66666666666666663], [0.77777777777777779], [0.88888888888888884], [1.0]]
mdml
  • 22,442
  • 8
  • 58
  • 66
LOBthon
  • 11
  • 2
  • Do you want take in the min/max parameters as arguments to the function or as user input? – David Greydanus May 08 '15 at 21:46
  • Hey David, I want to pass the min/max parameters to the function from the rescale call., so something like *rescale(numpy.arange(10, dtype=np.float), 2,7) where 2-7 is the range of values for the array, rather than 0.0-1.0 in my code above. – LOBthon May 08 '15 at 21:58
  • Should not need loops to do this with numpy fyi – reptilicus May 08 '15 at 22:06

0 Answers0