I have two arrays that I want to re-size, but I also want to retain the original values. The code below re-sizes the arrays, but the problem is that it over-writes the original values, as you can see when you look at the output from the
print(x)
print(y)
commands at the end of the script. However, if we comment out the line
# NewX,NewY=resize(x,y,xmin=MinRR,xmax=MaxRR,ymin=minLVET,ymax=maxLVET)
then the original values of x and y print out properly. However, if we remove the comment and leave the code as is, then x and y are apparently over-written becaue the
print(x)
print(y)
commands then output the values for NewX and NewY, respectively.
My code is below. Can anyone show me how to fix the code below so that x and y retain their original values, and so that NewX and NewY get their newly resized values?
import numpy as np
def GetMinRR(age):
MaxHR = 208-(0.7*age)
MinRR = (60/MaxHR)*1000
return MinRR
def resize(x,y,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0):
# Create local variables
NewX = x
NewY = y
# If the mins are greater than the maxs, then flip them.
if xmin>xmax: xmin,xmax=xmax,xmin
if ymin>ymax: ymin,ymax=ymax,ymin
#----------------------------------------------------------------------------------------------
# The rest of the code below re-calculates all the values in x and then in y with these steps:
# 1.) Subtract the actual minimum of the input x-vector from each value of x
# 2.) Multiply each resulting value of x by the result of dividing the difference
# between the new xmin and xmax by the actual maximum of the input x-vector
# 3.) Add the new minimum to each value of x
# Note: I wrote in x-notation, but the identical process is also repeated for y
#----------------------------------------------------------------------------------------------
# Subtracts right operand from the left operand and assigns the result to the left operand.
# Note: c -= a is equivalent to c = c - a
NewX -= x.min()
# Multiplies right operand with the left operand and assigns the result to the left operand.
# Note: c *= a is equivalent to c = c * a
NewX *= (xmax-xmin)/NewX.max()
# Adds right operand to the left operand and assigns the result to the left operand.
# Note: c += a is equivalent to c = c + a
NewX += xmin
# Subtracts right operand from the left operand and assigns the result to the left operand.
# Note: c -= a is equivalent to c = c - a
NewY -= y.min()
# Multiplies right operand with the left operand and assigns the result to the left operand.
# Note: c *= a is equivalent to c = c * a
NewY *= (ymax-ymin)/NewY.max()
# Adds right operand to the left operand and assigns the result to the l开发者_如何学运维eft operand.
# Note: c += a is equivalent to c = c + a
NewY += ymin
return (NewX,NewY)
# Declare raw data for use in creating logistic regression equation
x = np.array([821,576,473,377,326],dtype='float')
y = np.array([255,235,208,166,157],dtype='float')
# Call resize() function to re-calculate coordinates that will be used for equation
MinRR=GetMinRR(34)
MaxRR=1200
minLVET=(y[4]/x[4])*MinRR
maxLVET=(y[0]/x[0])*MaxRR
NewX,NewY=resize(x,y,xmin=MinRR,xmax=MaxRR,ymin=minLVET,ymax=maxLVET)
print 'x is: ',x
print 'y is: ',y
NewX = x.copy()
NewY = y.copy()
numpy arrays also support the __copy__
interface, and can be copied with the copy module, so this would also work:
NewX = copy.copy(x)
NewY = copy.copy(y)
If you want to retain the current behaviour of the function as-is, you'd need to replace all occurences of x
and y
with NewX
and NewY
. If the current behaviour of the function is wrong, you might keep them as they are.
Make explicit copies of x
and y
in resize
:
def resize(...):
NewX = [t for t in x]
NewY = [t for t in y]
Python always passes by reference, so any changes you make in subroutines are made to the actual passed objects.
The original resize
repeats itself. Everything that is done for x it repeats for y. That's not good, because it means you have to maintain twice as much code as you really need. The solution is to make resize
work on just one array, and call it twice (or as needed):
def resize(arr,lower=0.0,upper=1.0):
# Create local variables
result = arr.copy()
# If the mins are greater than the maxs, then flip them.
if lower>upper: lower,upper=upper,lower
#----------------------------------------------------------------------------------------------
# The rest of the code below re-calculates all the values in x and then in y with these steps:
# 1.) Subtract the actual minimum of the input x-vector from each value of x
# 2.) Multiply each resulting value of x by the result of dividing the difference
# between the new lower and upper by the actual maximum of the input x-vector
# 3.) Add the new minimum to each value of x
# Note: I wrote in x-notation, but the identical process is also repeated for y
#----------------------------------------------------------------------------------------------
# Subtracts right operand from the left operand and assigns the result to the left operand.
# Note: c -= a is equivalent to c = c - a
result -= result.min()
# Multiplies right operand with the left operand and assigns the result to the left operand.
# Note: c *= a is equivalent to c = c * a
result *= (upper-lower)/result.max()
# Adds right operand to the left operand and assigns the result to the left operand.
# Note: c += a is equivalent to c = c + a
result += lower
return result
Call it like this:
NewX=resize(x,lower=MinRR,upper=MaxRR)
NewY=resize(y,lower=minLVET,upper=maxLVET)
精彩评论