Need help vectorizing code
I have some code that I need help vectorizing. I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so. X is an NxD matrix. y is a 1xD vector. def foo(X, y, mylambda, N, D, epsilon): ... for j in xrange(D): aj = 0 cj = 0 for i in xrange(N): aj += 2 * (X[i,j] ** 2) cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j])) ... If I call numpy.vectorize() on the function, it throws an error at runtime. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help vectorizing code
I didn't paste the whole function, note the ... before and after. I do use the values. I want to get rid of one of the loops so that the computation becomes O(D). Assume vectors a and c should get populated during the compute, each being 1xD. Thanks On Saturday, January 18, 2014 12:51:25 PM UTC-8, Kevin K wrote: > I have some code that I need help vectorizing. > > I want to convert the following to vector form, how can I? I want to get rid > of the inner loop - apparently, it's possible to do so. > > X is an NxD matrix. y is a 1xD vector. > > > > def foo(X, y, mylambda, N, D, epsilon): > > ... > > for j in xrange(D): > > aj = 0 > > cj = 0 > > for i in xrange(N): > > aj += 2 * (X[i,j] ** 2) > > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + > w[j]*X[i,j])) > > > > ... > > > > If I call numpy.vectorize() on the function, it throws an error at runtime. > > > > Thanks -- https://mail.python.org/mailman/listinfo/python-list
File IO Issues, help :(
Hey everyone, I'm new to python and am trying to do a little project with it. I'm running into problems writing over a file. I read from the file and loop through for a specfic case in which I change something. After I open and give it opening options (w, r, etc) one of two things happens: either the file gets truncated and my writing never takes place (or it seems it doesnt) or everything is appended to the file and I have a double of what I started with, its never just the updated file. Can someone shed some light on this for me?? Code below: jsfile = open("../timeline.js", "r+") jscontent = jsfile.readlines() jsfile.truncate() for line in jscontent: if re.search('var d =', line): line = "var d = \""+mint['1'].ascdate()+"\"\n" print line jsfile.write(line) -- http://mail.python.org/mailman/listinfo/python-list
Re: File IO Issues, help :(
On Apr 29, 12:38 am, "Eric Wertman" <[EMAIL PROTECTED]> wrote: > chuck in a jsfile.close(). The buffer isn't flushing with what you > are doing now. jsfile.flush() might work... not sure. Closing and > re-opening the file for sure will help though. > Yeah sorry I forgot to include the close() in the quote but its there. In fact I moved it up a bit and still no luck heres the new code: jsfile = open("../timeline.js", "r+") jscontent = jsfile.readlines() jsfile.truncate() for line in jscontent: if re.search('var d =', line): line = "var d = \""+mint['1'].ascdate()+"\"\n" print line jsfile.write(line) jsfile.close() I tried this can got the same result...?? -- http://mail.python.org/mailman/listinfo/python-list
Re: File IO Issues, help :(
On Apr 29, 12:55 am, Peter Otten <[EMAIL PROTECTED]> wrote: > Kevin K wrote: > > On Apr 29, 12:38 am, "Eric Wertman" <[EMAIL PROTECTED]> wrote: > >> chuck in a jsfile.close(). The buffer isn't flushing with what you > >> are doing now. jsfile.flush() might work... not sure. Closing and > >> re-opening the file for sure will help though. > > > Yeah sorry I forgot to include the close() in the quote but its there. > > In fact I moved it up a bit and still no luck heres the new code: > > > jsfile = open("../timeline.js", "r+") > > jscontent = jsfile.readlines() > > jsfile.truncate() > > > for line in jscontent: > > if re.search('var d =', line): > > line = "var d = \""+mint['1'].ascdate()+"\"\n" > > print line > > jsfile.write(line) > > jsfile.close() > > > I tried this can got the same result...?? > > """ > truncate(...) > truncate([size]) -> None. Truncate the file to at most size bytes. > > Size defaults to the current file position, as returned by tell(). > """ > > After the readlines() call the current file position is at the end of the > file. Try jsfile.truncate(0). > > Also note that readlines() reads the whole file into memory. For large files > it would therefore be better to write to a new file and rename it > afterwards. > > Peter Thanks Peter that seemed to be most of the problem, however I now have a bunch of null characters in the file. Could it be an unwanted line in the list that im writing? Thanks, Kevin -- http://mail.python.org/mailman/listinfo/python-list
Re: File IO Issues, help :(
On Apr 29, 1:07 am, Kevin K <[EMAIL PROTECTED]> wrote: > On Apr 29, 12:55 am, Peter Otten <[EMAIL PROTECTED]> wrote: > > > > > Kevin K wrote: > > > On Apr 29, 12:38 am, "Eric Wertman" <[EMAIL PROTECTED]> wrote: > > >> chuck in a jsfile.close(). The buffer isn't flushing with what you > > >> are doing now. jsfile.flush() might work... not sure. Closing and > > >> re-opening the file for sure will help though. > > > > Yeah sorry I forgot to include the close() in the quote but its there. > > > In fact I moved it up a bit and still no luck heres the new code: > > > > jsfile = open("../timeline.js", "r+") > > > jscontent = jsfile.readlines() > > > jsfile.truncate() > > > > for line in jscontent: > > > if re.search('var d =', line): > > > line = "var d = \""+mint['1'].ascdate()+"\"\n" > > > print line > > > jsfile.write(line) > > > jsfile.close() > > > > I tried this can got the same result...?? > > > """ > > truncate(...) > > truncate([size]) -> None. Truncate the file to at most size bytes. > > > Size defaults to the current file position, as returned by tell(). > > """ > > > After the readlines() call the current file position is at the end of the > > file. Try jsfile.truncate(0). > > > Also note that readlines() reads the whole file into memory. For large files > > it would therefore be better to write to a new file and rename it > > afterwards. > > > Peter > > Thanks Peter that seemed to be most of the problem, however I now have > a bunch of null characters in the file. Could it be an unwanted line > in the list that im writing? > > Thanks, > Kevin Awesome that seems to work fine! Thanks a lot for your help guys! -- http://mail.python.org/mailman/listinfo/python-list