2010-12-01 143 views
0

我有兩個我想重新調整大小的數組,但我也想保留原始值。下面的代碼重新大小的數組,但問題是,它超額寫的原始值,當你查看輸出從重新調整numpy數組中的數據時出錯

print(x) 
print(y) 

命令在腳本結束時,你可以看到。但是,如果我們註釋掉該行

# NewX,NewY=resize(x,y,xmin=MinRR,xmax=MaxRR,ymin=minLVET,ymax=maxLVET) 

那麼x和y的原始值會正確打印出來。但是,如果我們按原樣刪除註釋和離開代碼,那麼x和y顯然過書面becaue的

print(x) 
print(y) 

命令,然後輸出分別下一頁末和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 

回答

3
NewX = x.copy() 
NewY = y.copy() 

numpy的陣列還支持__copy__接口,並且可與複印模塊被複制,所以這也將工作:

NewX = copy.copy(x) 
NewY = copy.copy(y) 

如果你想保留當前的行爲的功能,您需要用NewXNewY替換xy的所有出現次數。如果該函數的當前行爲錯誤,則可以保持原樣。

+0

這適用於numpy的陣列,而不是常規列表。但它肯定比我的解決方案更容易理解。 +1 – mtrw 2010-12-01 23:10:54

1

使xy明確的副本resize

def resize(...): 
    NewX = [t for t in x] 
    NewY = [t for t in y] 

的Python總是按引用傳遞,所以在子程序所做的任何更改都實際傳遞的對象做。

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)