Function to resize global numpy array interactively in ipython
Hi, I have a script with function definitions which I load into ipython for interactive use. These functions modify a global numpy array, whose size I need to be able to change interactively. I thus have a script which looks like this: from numpy import * def do_resize(N): global a a = resize(a, N) a = array([]) N=10; do_resize(N) print "Length of a is: ", len(a) N=20; do_resize(N) print "Length of a is: ", len(a) If I run this in ipython, using "run resize.py", it correctly outputs 10 and then 20. If I now type *interactively* N=30; do_resize(N), then the length of a is still 20, rather than 30 as I was hoping -- somehow I seem to be now dealing with a different copy of a? Doing the same thing in idle works as I expect, i.e. interactively the size is changed to 30. Could somebody please explain what's going on, and how I solve the problem? Thanks and best wishes, David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to resize global numpy array interactively in ipython
On Oct 29, 11:07 am, Robert Kern <[EMAIL PROTECTED]> wrote: > David Sanders wrote: > > Hi, > > > I have a script with function definitions which I load into ipython > > for interactive use. > > These functions modify a global numpy array, whose size I need to be > > able to change interactively. I thus have a script which looks like > > this: > > > from numpy import * > > > def do_resize(N): > >global a > >a = resize(a, N) > > > a = array([]) > > > N=10; do_resize(N) > > print "Length of a is: ", len(a) > > N=20; do_resize(N) > > print "Length of a is: ", len(a) > > > If I run this in ipython, using "run resize.py", it correctly outputs > > 10 and then 20. > > If I now type *interactively* N=30; do_resize(N), then the length of > > a is still 20, rather than 30 as I was hoping -- somehow I seem to be > > now dealing with a different copy of a? > > > Doing the same thing in idle works as I expect, i.e. interactively the > > size is changed to 30. > > > Could somebody please explain what's going on, and how I solve the > > problem? > > By default, %run executes the script in its own namespace. Then the > interactive > prompt's namespace gets updated with the values in that namespace. The global > statement refers to that initial namespace, not the one of the interactive > prompt. Give "%run -i resize.py" a try, though. That should execute the code > in > the interactive prompt's namespace. Great, that's exactly what I needed! Thanks very much. David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python : >>> import math doesn't work ?
On Nov 18, 8:48 pm, [EMAIL PROTECTED] wrote: > Have carefully installed Python 2.5.1 under XP in dir E:\python25 . > ran set path = %path% ; E:\python25 > Python interactive mode works fine for simple arithmetic . > Then tried >>> import math > >>> x = sqrt(100) >Get errorName error : name 'sqrt' is not defined > Same thing with sin(x) . > I'm unable to find "math" , "sqrt" , or "sin" anywhere in lib , Libs > or include directories . > The Tutorial does not clear this up . > Please help.Thanks Dave [EMAIL PROTECTED] Hi, I believe that should be: x = math.sqrt(100) 'import math' makes available the functions in the math module, but they are still inside the math namespace, so still require 'math.' If you will be using sqrt a lot, you can create a copy of the function in the local namespace using sqrt = math.sqrt x = sqrt(100) [In principle, you can give it any name you want, but of course it is sensible to use a name that makes sense. E.g. if you were a Spanish speaker, you could say instead raiz = math.sqrt x = raiz(100) ] For more details about how 'import'ing works, check out the Python tutorial. Hope that helps. Best wishes, David. -- http://mail.python.org/mailman/listinfo/python-list
Efficient processing of large nuumeric data file
Hi, I am processing large files of numerical data. Each line is either a single (positive) integer, or a pair of positive integers, where the second represents the number of times that the first number is repeated in the data -- this is to avoid generating huge raw files, since one particular number is often repeated in the data generation step. My question is how to process such files efficiently to obtain a frequency histogram of the data (how many times each number occurs in the data, taking into account the repetitions). My current code is as follows: --- #!/usr/bin/env python # Counts the occurrences of integers in a file and makes a histogram of them # Allows for a second field which gives the number of counts of each datum import sys args = sys.argv num_args = len(args) if num_args < 2: print "Syntaxis: count.py archivo" sys.exit(); name = args[1] file = open(name, "r") hist = {} # dictionary for histogram num = 0 for line in file: data = line.split() first = int(data[0]) if len(data) == 1: count = 1 else: count = int(data[1])# more than one repetition if first in hist: # add the information to the histogram hist[first]+=count else: hist[first]=count num+=count keys = hist.keys() keys.sort() print "# i fraction hist[i]" for i in keys: print i, float(hist[i])/num, hist[i] - The data files are large (~100 million lines), and this code takes a long time to run (compared to just doing wc -l, for example). Am I doing something very inefficient? (Any general comments on my pythonic (or otherwise) style are also appreciated!) Is "line.split()" efficient, for example? Is a dictionary the right way to do this? In any given file, there is an upper bound on the data, so it seems to me that some kind of array (numpy?) would be more efficient, but the upper bound changes in each file. Thanks and best wishes, David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient processing of large nuumeric data file
On Jan 18, 11:15 am, David Sanders <[EMAIL PROTECTED]> wrote: > Hi, > > I am processing large files of numerical data. Each line is either a > single (positive) integer, or a pair of positive integers, where the > second represents the number of times that the first number is > repeated in the data -- this is to avoid generating huge raw files, > since one particular number is often repeated in the data generation > step. > > My question is how to process such files efficiently to obtain a > frequency histogram of the data (how many times each number occurs in > the data, taking into account the repetitions). My current code is as > follows: Many thanks to all for the very detailed and helpful replies. I'm glad to see I was on the right track, but more happy to have learnt some different approaches. Thanks and best wishes, David. -- http://mail.python.org/mailman/listinfo/python-list