2010-12-01 17 views
0

Ich habe zwei Arrays, die ich neu skalieren möchte, aber ich möchte auch die ursprünglichen Werte beibehalten. Der folgende Code Re-Größen die Arrays, aber das Problem ist, dass es die ursprünglichen Werte überschreibt, wie Sie sehen können, wenn Sie am Ausgang von denFehler beim Ändern der Größe von Daten im Array "numpy"

sehen
print(x) 
print(y) 

Befehle am Ende des Skripts. Wenn wir die Zeile

auskommentieren, werden die ursprünglichen Werte von x und y korrekt ausgedruckt. Wenn wir jedoch den Kommentar und lassen Sie den Code wie entfernen, dann x und y sind offenbar über geschrieben becaue die

print(x) 
print(y) 

Befehle geben dann die Werte für NEWX und Newy sind.

Mein Code ist unten. Kann mir jemand zeigen, wie man den Code unten repariert, damit x und y ihre ursprünglichen Werte behalten, und damit NewX und NewY ihre neu geänderten Werte erhalten?

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 

Antwort

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

numpy Arrays unterstützen auch die __copy__ Schnittstelle und kann mit dem Kopiermodul kopiert werden, so würde dies auch funktionieren:

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

Wenn Sie das aktuelle Verhalten beibehalten möchten von der Funktion wie sie ist, müssten Sie alle Vorkommen von x und y durch NewX und NewY ersetzen. Wenn das aktuelle Verhalten der Funktion falsch ist, können Sie sie so beibehalten, wie sie sind.

+0

Dies funktioniert mit numpy Arrays, aber nicht regelmäßig Listen . Aber es ist sicher einfacher zu verstehen als meine Lösung. +1 – mtrw

1

Stellen explizit Kopien von x und y in resize:

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

Python immer durch Verweis übergibt, so dass alle Änderungen, die Sie in Subroutinen vornehmen, werden auf den tatsächlichen weitergegeben Objekten.

0

Das Original resize wiederholt sich. Alles, was für x getan wird, wiederholt sich für y. That's not good, weil es bedeutet, dass Sie doppelt so viel Code verwalten müssen, wie Sie wirklich benötigen. Die Lösung ist resize Arbeit auf nur eine Reihe zu machen, und es zweimal anrufen (oder je nach Bedarf):

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 

Nennen Sie es wie folgt aus:

NewX=resize(x,lower=MinRR,upper=MaxRR) 
NewY=resize(y,lower=minLVET,upper=maxLVET) 
Verwandte Themen