python - Error when re-sizing data in numpy array -
i have 2 arrays want re-size, want retain original values. code below re-sizes arrays, problem over-writes original values, can see when @ output
print(x) print(y)
commands @ end of script. however, if comment out line
# newx,newy=resize(x,y,xmin=minrr,xmax=maxrr,ymin=minlvet,ymax=maxlvet)
then original values of x , y print out properly. however, if remove comment , leave code is, x , y apparently over-written becaue
print(x) print(y)
commands output values newx , newy, respectively.
my code below. can show me how fix code below x , y retain original values, , newx , newy newly resized values?
import numpy 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 mins greater maxs, flip them. if xmin>xmax: xmin,xmax=xmax,xmin if ymin>ymax: ymin,ymax=ymax,ymin #---------------------------------------------------------------------------------------------- # rest of code below re-calculates values in x , in y these steps: # 1.) subtract actual minimum of input x-vector each value of x # 2.) multiply each resulting value of x result of dividing difference # between new xmin , xmax actual maximum of input x-vector # 3.) add new minimum each value of x # note: wrote in x-notation, identical process repeated y #---------------------------------------------------------------------------------------------- # subtracts right operand left operand , assigns result left operand. # note: c -= equivalent c = c - newx -= x.min() # multiplies right operand left operand , assigns result left operand. # note: c *= equivalent c = c * newx *= (xmax-xmin)/newx.max() # adds right operand left operand , assigns result left operand. # note: c += equivalent c = c + newx += xmin # subtracts right operand left operand , assigns result left operand. # note: c -= equivalent c = c - newy -= y.min() # multiplies right operand left operand , assigns result left operand. # note: c *= equivalent c = c * newy *= (ymax-ymin)/newy.max() # adds right operand left operand , assigns result left operand. # note: c += equivalent c = c + newy += ymin return (newx,newy) # declare raw data 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 re-calculate coordinates used 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 support __copy__
interface, , can copied copy module, work:
newx = copy.copy(x) newy = copy.copy(y)
if want retain current behaviour of function as-is, you'd need replace occurences of x
, y
newx
, newy
. if current behaviour of function wrong, might keep them are.
Comments
Post a Comment