![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
Rereading "Guide to NumPy" once again, I saw what I had missed all the previous times: the normal() distribution function (Chapter 10, page 173). I have several questions on using it in my application. The syntax is normal(loc=0.0, scale=1.0, size=None), but I've not seen what those represent, nor how to properly invoke this function. A clue will be much appreciated. I want to call normal() passing at least the width of the curve(at the end points where y=0.0), and the center (where y=1.0). Being able to specify the y value of the inflection point (by default 0.5) would also be helpful. In the application's function I now have: from numpy import * x = nx.arange(0, 100, 0.1) y = nx.normal(center,width) # passed to the function when called and I then pass x,y to PyX for plotting. But python responds with: y = nx.normal(center,width) AttributeError: 'module' object has no attribute 'normal' Pointers to what I should be doing are needed. TIA, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/6a1dc50b8d79fe3b9a5e9f5d8a118901.jpg?s=120&d=mm&r=g)
On Thu, Apr 24, 2008 at 10:01 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
In the application's function I now have:
from numpy import *
x = nx.arange(0, 100, 0.1) y = nx.normal(center,width) # passed to the function when called
and I then pass x,y to PyX for plotting. But python responds with:
y = nx.normal(center,width) AttributeError: 'module' object has no attribute 'normal'
It's random.normal(loc, scale). But that will give you random x values drawn from the normal distribution, not y values at your x. So you'll have to code your own normal, which will probably look something like norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Keith Goodman wrote:
It's random.normal(loc, scale). But that will give you random x values drawn from the normal distribution, not y values at your x. So you'll have to code your own normal, which will probably look something like
Keith, I overlooked that, thanks.
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
Can do. So, scale would equate to width and loc to center, yes? It's been so many years since I've dealt intimately with statistics and mathematics that I've gotten quite rusty. (However, I still retain what I need to accomplish a given objective.) Much appreciated, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/f5b423a4ceba3e115210914af848758e.jpg?s=120&d=mm&r=g)
On 24 Apr 2008, at 19:26, Rich Shepard wrote:
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
Can do. So, scale would equate to width and loc to center, yes?
Scale is half the width between the inflection points, mind the factor of 2. J. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Joris De Ridder wrote:
Scale is half the width between the inflection points, mind the factor of 2.
Joris, So, half of the full width at half the maximum height. Thank you. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Thu, Apr 24, 2008 at 1:39 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
On Thu, 24 Apr 2008, Joris De Ridder wrote:
Scale is half the width between the inflection points, mind the factor of 2.
Joris,
So, half of the full width at half the maximum height. Thank you.
No, it's not! scale is the standard deviation of the Normal distribution, not half the FWHM! -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Robert Kern wrote:
No, it's not! scale is the standard deviation of the Normal distribution, not half the FWHM!
OK. Thanks for clearing up my mis-understanding. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Keith Goodman wrote:
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
Hmm-m-m. I don't understand the source of the error: y = norm * exp(-pow((x - loc), 2) / (2 * scale**2)) TypeError: only length-1 arrays can be converted to Python scalars Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/afdaaab755ef79ac9e1374882d60ae9f.jpg?s=120&d=mm&r=g)
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
Hmm-m-m. I don't understand the source of the error:
y = norm * exp(-pow((x - loc), 2) / (2 * scale**2)) TypeError: only length-1 arrays can be converted to Python scalars
Python's built in pow() and exp() functions can't handle numpy arrays, and thus try (and fail) to convert arrays to scalar values. You want to use numpy.exp and numpy.power (or just the ** operator), to do these operations to numpy arrays elementwise.
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Zachary Pincus wrote:
Python's built in pow() and exp() functions can't handle numpy arrays, and thus try (and fail) to convert arrays to scalar values. You want to use numpy.exp and numpy.power (or just the ** operator), to do these operations to numpy arrays elementwise.
Zachary, Ah, now I understand! Thank you for the lessons, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/6a1dc50b8d79fe3b9a5e9f5d8a118901.jpg?s=120&d=mm&r=g)
On Thu, Apr 24, 2008 at 10:51 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
On Thu, 24 Apr 2008, Keith Goodman wrote:
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
Hmm-m-m. I don't understand the source of the error:
y = norm * exp(-pow((x - loc), 2) / (2 * scale**2)) TypeError: only length-1 arrays can be converted to Python scalars
I don't understand the error (I don't use arrays). Maybe try evaluating pieces of the formula, like pow(x-loc, 2) etc to see where the error is. It works for me:
x = arange(0,10) scale=1 loc=1 norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2)) y
array([ 1.46762663e-01, 3.98942280e-01, 1.46762663e-01, 5.39909665e-02, 2.68805194e-03, 1.33830226e-04, 9.01740968e-07, 6.07588285e-09, 5.54048800e-12, 5.05227108e-15])
![](https://secure.gravatar.com/avatar/afdaaab755ef79ac9e1374882d60ae9f.jpg?s=120&d=mm&r=g)
It works for me:
x = arange(0,10) scale=1 loc=1 norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2)) y
array([ 1.46762663e-01, 3.98942280e-01, 1.46762663e-01, 5.39909665e-02, 2.68805194e-03, 1.33830226e-04, 9.01740968e-07, 6.07588285e-09, 5.54048800e-12, 5.05227108e-15])
I assume you 'from numpy import *'? This is why it works -- because that import causes the python built-in exp() to be replaced (in the current namespace) by numpy.exp().
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Zachary Pincus wrote:
I assume you 'from numpy import *'? This is why it works -- because that import causes the python built-in exp() to be replaced (in the current namespace) by numpy.exp().
The equivalent (I think): import numpy as nx. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
![](https://secure.gravatar.com/avatar/ab7e74f2443b81e5175638d72be65e07.jpg?s=120&d=mm&r=g)
On 24/04/2008, Keith Goodman <kwgoodman@gmail.com> wrote:
On Thu, Apr 24, 2008 at 10:01 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
In the application's function I now have:
from numpy import *
x = nx.arange(0, 100, 0.1) y = nx.normal(center,width) # passed to the function when called
and I then pass x,y to PyX for plotting. But python responds with:
y = nx.normal(center,width) AttributeError: 'module' object has no attribute 'normal'
It's random.normal(loc, scale). But that will give you random x values drawn from the normal distribution, not y values at your x. So you'll have to code your own normal, which will probably look something like
norm = 1 / (scale * sqrt(2 * pi)) y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
I really have to recommend you instead install scipy, which contains this as well as many other probability distributions: D = scipy.stats.norm(0, 1) Then D.pdf(x) gives you the probability density function at x, D.cdf(x) gives you the probability that the function value is less than x, D.icdf(y) gives you the x value for which the probability is y that the value will be less than x, and so on. This is also available for more probability distributions than I had ever heard of. Anne
![](https://secure.gravatar.com/avatar/afdaaab755ef79ac9e1374882d60ae9f.jpg?s=120&d=mm&r=g)
The syntax is normal(loc=0.0, scale=1.0, size=None), but I've not seen what those represent, nor how to properly invoke this function. A clue will be much appreciated.
I want to call normal() passing at least the width of the curve(at the end points where y=0.0), and the center (where y=1.0). Being able to specify the y value of the inflection point (by default 0.5) would also be helpful.
The 'normal dstribution' is the Gaussian function: N(x) = 1/(std*sqrt(2*pi))*exp(-(x-mean)**2/(2*std**2)), where N(x) is the probability density at position x, given a normal distribution characterized by 'mean' and 'std' (standard deviation). http://en.wikipedia.org/wiki/Normal_distribution Now, numpy.random.normal gives random samples distributed according to the above probability density function. The only remaining mystery is how 'loc' and 'scale' -- the parameters of numpy.random.normal -- map to 'mean' and 'standard deviation', which is how a normal distribution is usually parameterized. Fortunately, the documentation reveals this:
print numpy.random.normal.__doc__ Normal distribution (mean=loc, stdev=scale).
normal(loc=0.0, scale=1.0, size=None) -> random values If you need an alternate parameterization of the normal (e.g. in terms of the y value of the inflection point), just solve that out analytically from the definition of the normal in terms of mean and std. However, it looks like you're trying to plot the normal function, not get random samples. Just evaluate the function (as above) at the x positions: mean, std = (0, 1) x = numpy.linspace(-10, 10, 200) # 200 evenly-spaced points from -10 to 10 y = 1/(std*numpy.sqrt(2*numpy.pi))*numpy.exp(-(x-mean)**2/(2*std**2)) Zach
![](https://secure.gravatar.com/avatar/fcbba89c78e42878a4f1a5dcf3d6663a.jpg?s=120&d=mm&r=g)
On Thu, 24 Apr 2008, Zachary Pincus wrote:
The only remaining mystery is how 'loc' and 'scale' -- the parameters of numpy.random.normal -- map to 'mean' and 'standard deviation', which is how a normal distribution is usually parameterized. Fortunately, the documentation reveals this:
print numpy.random.normal.__doc__ Normal distribution (mean=loc, stdev=scale).
normal(loc=0.0, scale=1.0, size=None) -> random values
Zachary, Thank you. I looked in the printed manual, not the built-in docs.
If you need an alternate parameterization of the normal (e.g. in terms of the y value of the inflection point), just solve that out analytically from the definition of the normal in terms of mean and std.
However, it looks like you're trying to plot the normal function, not get random samples. Just evaluate the function (as above) at the x positions:
mean, std = (0, 1) x = numpy.linspace(-10, 10, 200) # 200 evenly-spaced points from -10 to 10 y = 1/(std*numpy.sqrt(2*numpy.pi))*numpy.exp(-(x-mean)**2/(2*std**2))
OK. I'll work with this as well as the arange. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
participants (6)
-
Anne Archibald
-
Joris De Ridder
-
Keith Goodman
-
Rich Shepard
-
Robert Kern
-
Zachary Pincus