0

I have a function where I am updating the numpy array in each iteration but the original array is not updating after first iteration.

W = np.empty((3,3))
W.fill(1.0/3)

H = np.empty((2,3))
H.fill(1.0/3)

# file.txt contains 100 rows of x,y,z (with different values)

def UpdateHz(a,b,c) :
     return np.subtract(H[b,:],H[c,:])

def UpdateHy(a,b,c) :

     return np.subtract(W[b,:],H[c,:])
def UpdateHz(a,b,c) :
     return np.subtract(W[a,:],H[b,:])

with open('file.txt') as f:
  for line in f:
   x,y,z = [int(s) for s in line.split()] 
   W[x,:] = UpdateW(x,y,z)  
   H[y,:] = UpdateHy(x,y,z)
   H[z,:] = UpdateHz(x,y,z)

here the update function will return the updated rows for W and H. I need to update only xth row in P and Yth and zth row in H. This will keep running until the end of file.

I tried like this too :

W[x] = UpdateW(x,y,z)  
H[y] = UpdateHy(x,y,z)
H[z] = UpdateHz(x,y,z)

But after first iteration the W, H holds the old values (which was initialized), What I am doing wrong here??

learner
  • 499
  • 1
  • 5
  • 13
  • This really needs a complete and minimal example. See the discussion on this question where people are asking for a minimal example http://stackoverflow.com/q/27024702/553404 – YXD Nov 20 '14 at 11:29
  • Without seeing some sample data and the code of the Update functions, it will be hard to give a good answer! Maybe you return views instead of arrays, so that nothing is replaced in W and H. But without seeing the source code, it's just a guess... – jkalden Nov 20 '14 at 13:19
  • @jkalden I have added the sample code. If I print the H[y] W[x] H[z] after the return it shows the correct values but when I see the W and H matrices, it is not updated – learner Nov 20 '14 at 19:21
  • 1
    Hi! The types of return values should be fine, but now I wonder about the contents of `file.txt`. Here at SO, you are requested to pass a minimal example along with your problem, such that anyone can copy the code to an editor and run the code to reproduce the result. In your example, things are missing: minor ones like `import numpy as np` and major ones (how does file.txt look like). Apart from that, the lines `W = np.empty((3,3))` and `W.fill(1.0/3)` could be shortened to `W = np.ones((3,3)) / 3`. – jkalden Nov 20 '14 at 21:17

0 Answers0