SciPy is organized as a family of modules. We like to think of each module as a different field of mathematics. And as such, each has its own particular techniques and tools. You can find a list of some of the different modules included in SciPy at http://docs.scipy.org/doc/scipy-0.14.0/reference/py-modindex.html.
Let's use some of its functions to solve a simple problem.
The following table shows the IQ test scores of 31 individuals:
A stem plot of the distribution of these 31 scores (refers to the IPython Notebook for this chapter) shows that there are no major departures from normality, and thus we assume the distribution of the scores to be close to normal. Now, estimate the mean IQ score for this population, using a 99 percent confidence interval.
We start by loading the data into memory, as follows:
At this point, if we type dir(scores)
, hit the return key followed by a dot (.
), and press the tab key ;the system lists all possible methods inherited by the data from the NumPy library, as it is customary in Python. Technically, we could go ahead and compute the required mean
, xmean
, and corresponding confidence interval according to the formula, xmean ± zcrit * sigma / sqrt(n), where sigma
and n
are respectively the standard deviation and size of the data, and zcrit is the critical value corresponding to the confidence (http://en.wikipedia.org/wiki/Confidence_interval). In this case, we could look up a table on any statistics book to obtain a crude approximation to its value, zcrit = 2.576. The remaining values may be computed in our session and properly combined, as follows:
The output is shown as follows:
We have thus computed the estimated mean IQ score (with value 105.83870967741936
) and the interval of confidence (from about 99.34
to approximately 112.33
). We have done so using purely SciPy-based operations while following a known formula. But instead of making all these computations by hand and looking for critical values on tables, we could just ask SciPy.
Note how the scipy.stats
module needs to be loaded before we use any of its functions:
The variable result
contains the solution to our problem with some additional information. Note that result is a tuple with three elements as the help
documentation suggests:
The output of this command will depend on the installed version of SciPy. It might look like this (run the companion IPython Notebook for this chapter to see how the actual output from your system is, or run the command in a Python console):
Our solution is the first element of the tuple result
; to see its contents, type:
The output is shown as follows:
Note how this output gives us the same average as before, but a slightly different confidence interval, due to more accurate computations through SciPy (the output might be different depending on the SciPy version available on your computer).