Re: [Tutor] numerical problem
Gabriele Brambilla writes: > the values in www are like 1.01e-134 and also smaller, and I sum values > like this: there could be any problem summing so small numbers? The default floating-point number type in Python is a fixed-precision float http://docs.python.org/3/library/stdtypes.html#typesnumeric>. Yes, with the ‘float’ type calcuations will lose information, and calculations at differing orders of magnitude will lose information faster. That's a limitation of any fixed-precision float implementation. If you want to use different types with different trade-offs, see http://docs.python.org/3/library/stdtypes.html#typesnumeric> the Python documentation and the NumPy documentation http://docs.scipy.org/doc/numpy/user/basics.types.html> for the numeric data types they support. -- \ “If [a technology company] has confidence in their future | `\ ability to innovate, the importance they place on protecting | _o__) their past innovations really should decline.” —Gary Barnett | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] numerical problem
for example I read this: On Pythons prior to 2.7 and 3.1, once you start experimenting with floating-point numbers, you're likely to stumble across something that may look a bit odd at first glance: >>> 3.1415 * 2 # repr: as code (Pythons < 2.7 and 3.1) 6.2834 >>> print(3.1415 * 2) # str: user-friendly 6.283 The first result isn't a bug; it's a display issue. It turns out that there are two ways to print every object in Python--with full precision (as in the first result shown here), and in a user-friendly form (as in the second). Formally, the first form is known as an object's as-code repr, and the second is its user-friendly str. In older Pythons, the floating-point repr sometimes displays more precision than you might expect. The difference can also matter when we step up to using classes. For now, if something looks odd, try showing it with a print built-in function call statement. Better yet, upgrade to Python 2.7 and the latest 3.X, where floating-point numbers display themselves more intelligently, usually with fewer extraneous digits--since this book is based on Pythons 2.7 and 3.3, this is the display form I'll be showing throughout this book for floating-point numbers: >>> 3.1415 * 2 # repr: as code (Pythons >= 2.7 and 3.1) 6.283 Besides expressions, there are a handful of useful numeric modules that ship with Python--modules are just packages of additional tools that we import to use: >>> import math >>> math.pi 3.141592653589793 >>> math.sqrt(85) 9.219544457292887 The math module contains more advanced numeric tools as functions, while the ran dom module performs random-number generation and random selections (here, from a Python list coded in square brackets--an ordered collection of other objects to be introduced later in this chapter): >>> import random >>> random.random() 0.7082048489415967 >>> random.choice([1, 2, 3, 4]) 1 Could the problem be something like this? thanks Gabriele 2014-03-03 22:44 GMT-05:00 Gabriele Brambilla < gb.gabrielebrambi...@gmail.com>: > ok, > the things I get is very specific. > my results differs from the other guy's ones with which I compare them for > slightly details. > We fit the data with a function and we obtain different but similar > values. our fit routines on the same data return the same results. So we > produce different data. > the values in www are like 1.01e-134 and also smaller, and I sum values > like this: there could be any problem summing so small numbers? is there a > Python guide that helps in using strange values numbers? > > thanks > > Gabriele > > > > > 2014-03-03 21:02 GMT-05:00 Steven D'Aprano : > > On Mon, Mar 03, 2014 at 08:00:52PM -0500, Gabriele Brambilla wrote: >> > Hi, >> > >> > I'm doing a sum in a for loop: >> > >> > www is the quantity I add. >> > >> > MYMAP[i, j, k] = MYMAP[i, j, k] + www >> > >> > MYMAP is a numpy array >> > >> > I have strong reasons to think that in this operation happens some >> > numerical error...Have you suggestions to discover where it is? >> >> You are almost certainly correct. Unless all the numbers are exact >> integers, nearly every floating point operation will add some error. >> >> Without seeing your code, and some representative sample data, it is >> impossible to tell whether the error is tiny or huge. >> >> Tiny error: >> >> MYMAP[1, j, k] = 1.001 >> www = 0.25 >> >> >> Huge error: >> >> MYMAP[1, j, k] = 1.001e19 >> www = 0.25 >> >> >> >> -- >> Steven >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] numerical problem
ok, the things I get is very specific. my results differs from the other guy's ones with which I compare them for slightly details. We fit the data with a function and we obtain different but similar values. our fit routines on the same data return the same results. So we produce different data. the values in www are like 1.01e-134 and also smaller, and I sum values like this: there could be any problem summing so small numbers? is there a Python guide that helps in using strange values numbers? thanks Gabriele 2014-03-03 21:02 GMT-05:00 Steven D'Aprano : > On Mon, Mar 03, 2014 at 08:00:52PM -0500, Gabriele Brambilla wrote: > > Hi, > > > > I'm doing a sum in a for loop: > > > > www is the quantity I add. > > > > MYMAP[i, j, k] = MYMAP[i, j, k] + www > > > > MYMAP is a numpy array > > > > I have strong reasons to think that in this operation happens some > > numerical error...Have you suggestions to discover where it is? > > You are almost certainly correct. Unless all the numbers are exact > integers, nearly every floating point operation will add some error. > > Without seeing your code, and some representative sample data, it is > impossible to tell whether the error is tiny or huge. > > Tiny error: > > MYMAP[1, j, k] = 1.001 > www = 0.25 > > > Huge error: > > MYMAP[1, j, k] = 1.001e19 > www = 0.25 > > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] numerical problem
On Mon, Mar 03, 2014 at 08:00:52PM -0500, Gabriele Brambilla wrote: > Hi, > > I'm doing a sum in a for loop: > > www is the quantity I add. > > MYMAP[i, j, k] = MYMAP[i, j, k] + www > > MYMAP is a numpy array > > I have strong reasons to think that in this operation happens some > numerical error...Have you suggestions to discover where it is? You are almost certainly correct. Unless all the numbers are exact integers, nearly every floating point operation will add some error. Without seeing your code, and some representative sample data, it is impossible to tell whether the error is tiny or huge. Tiny error: MYMAP[1, j, k] = 1.001 www = 0.25 Huge error: MYMAP[1, j, k] = 1.001e19 www = 0.25 -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] numerical problem
> I'm doing a sum in a for loop: > > www is the quantity I add. > > MYMAP[i, j, k] = MYMAP[i, j, k] + www > > MYMAP is a numpy array > > I have strong reasons to think that in this operation happens some numerical > error...Have you suggestions to discover where it is? Hi Gabriele, Can you explain those reasons why you suspect that this operation is responsible? Otherwise, we can not give you as good suggestions as we'd like. If you have literal error messages, that would also be helpful to present those to us. Basically, if you can provide a code snippet of what you're doing, then we can independently try to reproduce the error you're seeing. The question above omits too much details because it prevents folks from knowing that they are answering the right question. Here is an example of a program that adds a delta to every element in a 3d numpy array. ## >>> import numpy as np >>> x = np.array([[[ 0, 1, 2], ... [ 3, 4, 5], ... [ 6, 7, 8]], ...[[ 9, 10, 11], ... [12, 13, 14], ... [15, 16, 17]], ...[[18, 19, 20], ... [21, 22, 23], ... [24, 25, 26]]]) >>> x + 42 array([[[42, 43, 44], [45, 46, 47], [48, 49, 50]], [[51, 52, 53], [54, 55, 56], [57, 58, 59]], [[60, 61, 62], [63, 64, 65], [66, 67, 68]]]) ## Note: adding a scalar to the numpy array has the addition apply pointwise to every component of the array. This is the direct and natural approach. When you mention "loop" in your question above, this makes me pause. If you are doing an explicit loop, as discussed in: http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html then it would be very helpful to see the code you are doing. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] numerical problem
Gabriele Brambilla writes: > www is the quantity I add. > > MYMAP[i, j, k] = MYMAP[i, j, k] + www > > MYMAP is a numpy array Unfortunately, we have no idea what values are in ‘MYMAP’ nor ‘www’. So it's difficult for us to see what might be causing any problem, or even whether there is any problem. > I have strong reasons to think that in this operation happens some > numerical error...Have you suggestions to discover where it is? Can you show a complete example which demonstrates that there is an error? For example:: >>> import numpy >>> map = numpy.array([10, 20, 30]) >>> map[1] + 4 24 Please show a simple, self-contained, correct example http://sscce.org/> like the above, which demonstrates the problematic behaviour. You'll also need to be more descriptive of what you think the problem is: What are you expecting? What are you experiencing instead? -- \ “Not using Microsoft products is like being a non-smoker 40 or | `\ 50 years ago: You can choose not to smoke, yourself, but it's | _o__) hard to avoid second-hand smoke.” —Michael Tiemann | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] numerical problem
Hi, I'm doing a sum in a for loop: www is the quantity I add. MYMAP[i, j, k] = MYMAP[i, j, k] + www MYMAP is a numpy array I have strong reasons to think that in this operation happens some numerical error...Have you suggestions to discover where it is? thanks Gabriele ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use multiprocessing Managers?
On Mon, Mar 3, 2014 at 6:45 AM, James Chapman wrote: > Thanks for the explanation. > > Is there any reason or case you can think of where on a single system > you would use the manager (proxied) queue over the multiprocessing > (piped) queue? Not really. But a manager also lets you share lists, dicts, namespaces, or other registered types. Note that a proxy isn't thread safe, so it should be used by only a single thread, or with a lock. > The manager could potentially be extended to do some kind of data > validation / error checking before submitting to the Queue. This could > be important if the data going into the Queue was for example, user > generated. You could have another function that does that, or subclass multiprocessing.queues.Queue to override its `put` method. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use multiprocessing Managers?
Thanks for the explanation. Is there any reason or case you can think of where on a single system you would use the manager (proxied) queue over the multiprocessing (piped) queue? Actually I can probably answer this myself... The manager could potentially be extended to do some kind of data validation / error checking before submitting to the Queue. This could be important if the data going into the Queue was for example, user generated. Hmm, yeah I'd say question answered. Thanks eryksun. -- James On 1 March 2014 16:48, eryksun wrote: > On Fri, Feb 28, 2014 at 6:31 AM, James Chapman wrote: >> >> log_Q = multiprocessing.Queue() > > This is a Queue from multiprocessing.queues. It uses system resources > (e.g. a semaphore for the queue capacity) that can be shared with > processes on the same machine. > > A value `put` in a queue.Queue is available immediately: > > >>> import queue > >>> q1 = queue.Queue() > >>> try: q1.put('value'); q1.get_nowait() > ... except queue.Empty: 'empty' > ... > 'value' > > On the other hand, a Queue from multiprocessing.queues writes to a > pipe using a background thread, so there can be a small delay: > > >>> import multiprocessing as mp > >>> q2 = mp.Queue() > >>> try: q2.put('value'); q2.get_nowait() > ... except queue.Empty: 'empty' > ... > 'empty' > >>> q2.get_nowait() > 'value' > >> or whether I create it like this: >> >> multimanager = multiprocessing.Manager() >> log_Q = multimanager.Queue() > > This is a queue.Queue wrapped by an AutoProxy. For example, its `get` > method calls _callmethod('get', *args, **kwds), which connects to the > manager, sends the request, and receives the result. > > The docs demonstrate using a manager with remote processes: > > http://docs.python.org/3/library/multiprocessing#using-a-remote-manager > > You can also proxy the Queue type from multiprocessing.queues. In that > case, remote processes use a proxy, but local processes can use the > queue directly. > >> Perhaps the manager would be important if I was writing to a Queue and >> expecting all threads to see that message? > > Only 1 thread will `get` the message. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with "Guess the number" script
On 03/03/2014 11:27 AM, spir wrote: How would you define what these variables represent, using everyday language? My own definitions would lead me to choose the following variable names: guess_text = raw_input(promt) guess_number = int(user_guess) return guess_number sorry, should be: guess_text = raw_input(promt) guess_number = int(guess_text) return guess_number d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with "Guess the number" script
On 03/03/2014 05:03 AM, Scott W Dunning wrote: Ben Finney makes numerous fine comments already. I'll add a few, some on the same points but but expressed a bit differently (case it helps). This is what Im having trouble with now. Here are the directions I’m stuck on and what I have so far, I’ll bold the part that’s dealing with the instructions if anyone could help me figure out where I’m going wrong. Thanks! from random import randrange randrange(1, 101) from random import seed seed(129) def print_description(): print """Welcome to Guess the Number. I have seleted a secret number in the range 1 ... 100. You must guess the number within 10 tries. I will tell you if you ar high or low, and I will tell you if you are hot or cold.\n""" def get_guess(guess_number): promt = "(" + str(guess_number) +") Please enter a guess:" user_guess = raw_input(promt) user_guess = int(user_guess) return user_guess Very good choice of variable name for 'promt'. (Apart from ortography, but since you are consistent with the error... ;-) There are 2 user guesses here, and only 1 variable, thus 1 name. The name should say what (idea) the variable represents in the program; this should be said by the name's *meaning*. It is one of the greatest difficulties in programming. How would you define what these variables represent, using everyday language? My own definitions would lead me to choose the following variable names: guess_text = raw_input(promt) guess_number = int(user_guess) return guess_number Note: it is especially obviuos that these are 2 separate numbers, since they do not even are of the same type (a piece of text, or "string", vs a number, here an "int"). Good naming is very, very hard; differences of naming can make some piece of program nearly trivial or instead nearly impossible to understand; often bad naming is worse than hypothetical randomly chosen names, because bad naming *misleads* your thinking. Changing the value of a local variable is always, or nearly, a sign that there are here 2 ideas which should be represented by 2 variables with 2 names. Example of 2 programming styles (note the difference in ease of understanding, even if you don't know the python features used here): def get_data (data): data = File(data) # (a file) data = data.read() # (a piece of text) data = data.split("") # (a list of words) return data ... data = get_data("data.text") def data_words (file_name): data_file = File(file_name) # (a file) text = data_file.read() # (a piece of text) words = text.split(" ") # (a list of words) return words ... words = data_words("data.text") (A special case is loop variables, but even then you only write the assignment once, the value chages across multiple passes on the same code. The only real exception is accumulators, like for computing a sum, which need to be first initialised to a start value, often 0.) def print_hints(secrets, guess): secret_number = secret guess = guess if guess < 0 or user_guess> 101: print "Out of range!" Parameters are input variables. Once they are given execution values by a call like print_hints(input_value1, input_value2) these variables exist _inside_ the function body (each with a name and a value). As if functions were defined like: def print_hints:# note: no param secret = input_value1 guess = input_value2 ... use these variables ... This is more or less what the language does for you. This is the whole point of defining parameters, in fact. So, _you_ do not need to _rebind_ parameters to local variables; they already are local variables. In addition, you are not consistent with variable _names_, evendently, so your programs have no chance to work. This is an annoying, but necessary part of programming. But the language will always tell about such errors, at once, *if and only if* the wrong name does *not* otherwise exist. --> pay attention! def main(): print_description() secret = randrange(1,101) current_guess = get_guess(1) if current_guess != secret: print_hints(secret_number, guess) current_guess = get_guess(2) * 'secret_number' appears from nowhere: pay attention! * To be more coherent checking if the guess is right or wrong (or too high or too low) should be done in function print_hints as well. This function _evaluates_ the guess (maybe it should be renamed). if secret == current_guess: print "Congratulations, you win!" else: print "Please play again" print "The secret number was", secret These are (also) hints to the player, actually, aren't they? main() d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python
Re: [Tutor] Help with "Guess the number" script
Scott W Dunning writes: > This is what Im having trouble with now. Here are the directions I’m > stuck on and what I have so far, I’ll bold the part that’s dealing > with the instructions if anyone could help me figure out where I’m > going wrong. “Bold” assumes that markup of text will survive; that's not reliable, since this is a text-only medium and only the plain text will reliably survive to all readers. Instead, explain what is confusing you, and if you need to show someone else's words, use the plain-text quotation. > def get_guess(guess_number): > promt = "(" + str(guess_number) +") Please enter a guess:" You're creating a prompt string, so this is a good choice of name; except that its correct spelling is “prompt”. > user_guess = raw_input(promt) > user_guess = int(user_guess) These are two separate values, why are you binding one name and then immediately re-binding the same name to a different value? Have a think about what each intermediate value *means*, and choose names on that basis. This is valuable! Thinking about what the values mean is vital to understanding what the program is doing, which is of course a big part of what you're in this to learn. > def print_hints(secrets, guess): > secret_number = secret You never use this “secret_number” name again, so the binding is useless. What was your intention? You also never use the “secrets” parameter; and the “secret” name was not bound before you used it. Have you mis-spelled one of those? > guess = guess I don't know what the point of this no-op assignment is. What are you trying to do? > if guess < 0 or user_guess> 101: Likewise, you never bind the name “user_guess”. Where are you expecting it to be bound? I suspect at this point you've been flailing around without much understanding of the changes you're making. This leads to a confused mass of code that you can't understand or explain. Better would be to experiment at the interactive Python prompt to test what these changes *do*, before putting them into your code. Reduce the mechanisms down to very minimal cases, and try them out; confirm your assumptions and guesses. Only then should you plan a change to the program file. -- \ “I must have a prodigious quantity of mind; it takes me as much | `\ as a week sometimes to make it up.” —Mark Twain, _The Innocents | _o__) Abroad_ | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with "Guess the number" script
This is what Im having trouble with now. Here are the directions I’m stuck on and what I have so far, I’ll bold the part that’s dealing with the instructions if anyone could help me figure out where I’m going wrong. Thanks! from random import randrange randrange(1, 101) from random import seed seed(129) def print_description(): print """Welcome to Guess the Number. I have seleted a secret number in the range 1 ... 100. You must guess the number within 10 tries. I will tell you if you ar high or low, and I will tell you if you are hot or cold.\n""" def get_guess(guess_number): promt = "(" + str(guess_number) +") Please enter a guess:" user_guess = raw_input(promt) user_guess = int(user_guess) return user_guess def print_hints(secrets, guess): secret_number = secret guess = guess if guess < 0 or user_guess> 101: print "Out of range!" def main(): print_description() secret = randrange(1,101) current_guess = get_guess(1) if current_guess != secret: print_hints(secret_number, guess) current_guess = get_guess(2) if secret == current_guess: print "Congratulations, you win!" else: print "Please play again" print "The secret number was", secret main() Just below the body of the get guess function, define a new function named print hints that takes two arguments. The first is a secret num- ber and is kept in a parameter named secret. The second is a guess made by the user and it is held in a parameter named guess. The user’s guess is supposed to be within the range 1 ... 100. Write a conditional statement that checks if the guess is out of that range, and if it is print ‘out of range’ in the body of the print hints function. Now we are going to give the user the option to make a second guess. You must add code to the main function immediately after assignment statement you wrote for task 7. Write a conditional statement to check if the current guess does not match the secret number. If the numbers to not match, in the body of the conditional statement you will do two things. (a) call print hints to give the user hints, (b) re-assign current guess to the result of calling get guess with an argument of 2. -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with "Guess the number" script
On Mar 2, 2014, at 12:43 AM, Ben Finney wrote: > > No, that's the opposite direction :-) Inside the ‘get_guess’ function > you should use as many names as you need for the different purposes. > > So, you have one name ‘guess_number’ bound to the function's parameter. > Don't bind anything else to it! > > Then, for the return value from ‘raw_input’, you should choose a > different suitable name and bind that name to the value (with an > assignment statement). > > Then, for the computed result, you should choose yet another suitable > name, and bind *that* name to the computed value. > > All these different names will make clear in the code what the meaning > of each value is. > Ok, I see. So, I think I got a good portion of this done thanks to you guys and understanding the directions a little better. I’ll post my code. For some reason I’m not sure what the user_guess variable in the get_guess is actually doing, or if it’s even an appropriate variable name?? Also, I’m going to bold another part I’m struggling with about the number being out of range, I’m not sure I’m going in the right direction with that, mainly because it does not work. Here is what I have so far… from random import randrange randrange(1, 101) from random import seed seed(129) def print_description(): print """Welcome to Guess the Number. I have seleted a secret number in the range 1 ... 100. You must guess the number within 10 tries. I will tell you if you ar high or low, and I will tell you if you are hot or cold.\n""" def get_guess(guess_number): promt = "(" + str(guess_number) +") Please enter a guess:" user_guess = raw_input(promt) user_guess = int(user_guess) return user_guess def print_hints(secrets, guess): if guess < 0 or guess> 101: print "Out of range!" def main(): print_description() secret = randrange(1,101) current_guess = get_guess(1) if secret == current_guess: print "Congratulations, you win!" else: print "Please play again" print "The secret number was", secret main() print_hints()___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor