我有两个我想重新调整大小的数组,但我也希望保留原始值。下面的代码重新调整了数组的大小,但问题是它覆盖了原始值,正如您在查看
的输出时所看到的那样print(x)
print(y)
脚本末尾的命令。但是,如果我们注释掉这一行
# NewX,NewY=resize(x,y,xmin=MinRR,xmax=MaxRR,ymin=minLVET,ymax=maxLVET)
然后x和y的原始值正确打印出来。但是,如果我们删除注释并保留代码,那么x和y显然会被覆盖,因为
print(x)
print(y)
命令然后分别输出NewX和NewY的值。
我的代码如下。 任何人都可以告诉我如何修复下面的代码,以便x和y保留其原始值,以便NewX和NewY获得新调整大小的值吗?
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 left 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
答案 0 :(得分:3)
NewX = x.copy()
NewY = y.copy()
numpy数组也支持__copy__
接口,可以使用复制模块进行复制,因此也可以使用:
NewX = copy.copy(x)
NewY = copy.copy(y)
如果您希望按原样保留函数的当前行为,则需要将x
和y
的所有出现替换为NewX
和NewY
。如果函数的当前行为是错误的,您可以按原样保留它们。
答案 1 :(得分:1)
在x
中明确复制y
和resize
:
def resize(...):
NewX = [t for t in x]
NewY = [t for t in y]
Python总是通过引用传递,因此您在子例程中所做的任何更改都是对实际传递的对象进行的。
答案 2 :(得分:0)
原始resize
重演。为x完成的所有事情都会重复y。 That's not good,因为这意味着您必须维护两倍于您真正需要的代码。解决方案是让resize
只对一个数组起作用,然后调用它两次(或根据需要):
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
这样称呼:
NewX=resize(x,lower=MinRR,upper=MaxRR)
NewY=resize(y,lower=minLVET,upper=maxLVET)