[Tutor] scaling values
i have various functions (that i didn't write) that put out data in lists of various types. But some functions (which i didn't write) that expect the data to be scaled, sometimes 0-1 sometimes 1-2, sometimes 0-127..., sometimes 0 - 32768... gosh you name it. In other words i have a bunch of black boxes that don't speak the same language is there a scaling function in python (or numeric or scipy) that can scale a list of values to a high precision? x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26] foo = scale(x, 0, 1.0) and get that list scaled 0 to 1, or if i had: x = [.12789, .982779, .19798198, .266796, .656527, .257877091] foo = scale(x, 0, 127) cheers, -kp-- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
On Tue, 14 Mar 2006, kevin parks wrote: is there a scaling function in python (or numeric or scipy) that can scale a list of values to a high precision? x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26] foo = scale(x, 0, 1.0) Hi Kevin, I'm still confused by the problem. Let's try small examples. Let's say we had this: ## x = [13] scaledX = scale(x, 0, 1.0) ## What would 'scaledX' have? I'm asking because I have no clue from the problem description! *grin* (There's some hidden knowledge that you have about the problem, so I'm trying to make sure it's out in the open.) We can ask the same question with a slightly larger (but still small) example: x = [13, 71] What should we expect from things like: scale(x, 0.0, 1.0) scale(x, 0.0, 2.0) scale(x, 1.0, 2.0) The results of small examples will help clarify what we'd need to do to make scale() work. Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
kevin parks wrote: i have various functions (that i didn't write) that put out data in lists of various types. But some functions (which i didn't write) that expect the data to be scaled, sometimes 0-1 sometimes 1-2, sometimes 0-127..., sometimes 0 - 32768... gosh you name it. In other words i have a bunch of black boxes that don't speak the same language is there a scaling function in python (or numeric or scipy) that can scale a list of values to a high precision? Perhaps someone on this list knows what you want, but I don't. What is scaling? What does foo look like? x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26] foo = scale(x, 0, 1.0) and get that list scaled 0 to 1, or if i had: x = [.12789, .982779, .19798198, .266796, .656527, .257877091] foo = scale(x, 0, 127) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
Is this what you're asking for? # Scaler.py # def scale(OldList, NewMin, NewMax): NewRange = float(NewMax - NewMin) OldMin = min(x) OldMax = max(x) OldRange = float(OldMax - OldMin) ScaleFactor = NewRange / OldRange print '\nEquasion: NewValue = ((OldValue - ' + str(OldMin) + ') x ' + str(ScaleFactor) + ') + ' + str(NewMin) + '\n' NewList = [] for OldValue in OldList: NewValue = ((OldValue - OldMin) * ScaleFactor) + NewMin NewList.append(NewValue) return NewList x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26] foo = scale(x, 0, 1.0) ##x = [.12789, .982779, .19798198, .266796, .656527, .257877091] ##foo = scale(x, 0, 127) ##x = [0, 50, 100] ##foo = scale(x, 32, 212) ##x = [32, 122, 212] ##foo = scale(x, 0, 100) print 'New List = ' + str(foo) Print Equasion: NewValue = ((OldValue - 10) x 0.00909090909091) + 0 New List = [0.027272727272727271, 0.55454545454545456, 1.0, 0.70909090909090911, 0.65454545454545454, 0.81818181818181812, 0.0, 0.5, 0.82727272727272727, 0.31818181818181818, 0.14545454545454545] -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of kevin parks Sent: Tuesday, March 14, 2006 1:03 AM To: tutor@python.org Subject: [Tutor] scaling values i have various functions (that i didn't write) that put out data in lists of various types. But some functions (which i didn't write) that expect the data to be scaled, sometimes 0-1 sometimes 1-2, sometimes 0-127..., sometimes 0 - 32768... gosh you name it. In other words i have a bunch of black boxes that don't speak the same language is there a scaling function in python (or numeric or scipy) that can scale a list of values to a high precision? x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26] foo = scale(x, 0, 1.0) and get that list scaled 0 to 1, or if i had: x = [.12789, .982779, .19798198, .266796, .656527, .257877091] foo = scale(x, 0, 127) cheers, -kp-- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
Hi Kevin, Do you mean:? 1)take the highest value in the list hval, take the lowest value in the list lval 2) pass top and bottom NEW values for the list: ntop nbot 3) then build another list where hval is replaced by ntop, lval is replaced by nbot, and everything else is geometrically scaled in bewteen? AFAIK, there is no such function in the standard lib (maybe someone knows better) but it is not hard to build such a function. Have you tried? It would be something like: def scaled(x, ntop, nbot) See that you can use min(L) and max(L) to get lval and hval Try it, and if you run into problems or have questions, don't hesitate to ask. Hope that helps, Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
hi, Seems my post added much confusion. Sorry... I was hoping not to have to post my code since it is really wrong and slightly embarrassing.. what i am trying to do is map an input range of values to output range. I was hoping to make it a bit of an all purpose utility that would map pretty much any input range to an output range, also do inverted mapping... and also handle negative numbers and perhaps even a flag for exponential mapping. import random def scaleX(in_seq, low, hi): range1 = max(in_seq) - min(in_seq) #range2 = max(out_seq) - min(outseq) range2 = hi - low ratio = range1/range2 return [(x * ratio) for x in in_seq] def test(): # Create a list of 15 random integers in the range 0 to 127 # see if we can map it to 0 - 1 inseq = random.sample(xrange(128), 25) print print scaleX(inseq, 0.0, 1.0) print if __name__ == __main__: test() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
kevin parks wrote: hi, Seems my post added much confusion. Sorry... I was hoping not to have to post my code since it is really wrong and slightly embarrassing.. I think the confusion was about what input range to use. From your code it looks like you want to use just the actual range of input values. what i am trying to do is map an input range of values to output range. I was hoping to make it a bit of an all purpose utility that would map pretty much any input range to an output range, also do inverted mapping... and also handle negative numbers and perhaps even a flag for exponential mapping. import random def scaleX(in_seq, low, hi): range1 = max(in_seq) - min(in_seq) #range2 = max(out_seq) - min(outseq) range2 = hi - low ratio = range1/range2 return [(x * ratio) for x in in_seq] This is actually pretty close. You have ratio backwards and you need to account for the offsets min(in_seq) and low. Try in_low = min(in_seq) ratio = range2/range1 return [ ((x-in_low) * ratio + low) for x in in_seq ] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] scaling values
Thanks to Kent Johnson, David Heiser and everyone else. Looks like i was most of the way there...hehe... David Heiser gets special bonus points for actually understanding my initial mysterious query. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor