SciPy organization
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:
>>> import numpy >>> scores = numpy.array([114, 100, 104, 89, 102, 91, 114, 114, 103, 105, 108, 130, 120, 132, 111, 128, 118, 119, 86, 72, 111, 103, 74, 112, 107, 103, 98, 96, 112, 112, 93])
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:
>>> import scipy >>> xmean = scipy.mean(scores) >>> sigma = scipy.std(scores) >>> n = scipy.size(scores) >>> xmean, xmean - 2.576*sigma /scipy.sqrt(n), \ xmean + 2.576*sigma / scipy.sqrt(n)
The output is shown as follows:
(105.83870967741936, 99.343223715529746, 112.33419563930897)
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:
>>> from scipy import stats >>> result=scipy.stats.bayes_mvs(scores)
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:
>>> help(scipy.stats.bayes_mvs)
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:
>>> result[0]
The output is shown as follows:
(105.83870967741936, (101.48825534263035, 110.18916401220837))
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).
- SPSS數(shù)據(jù)挖掘與案例分析應(yīng)用實(shí)踐
- Mastering Visual Studio 2017
- FFmpeg入門詳解:音視頻流媒體播放器原理及應(yīng)用
- Spring Cloud、Nginx高并發(fā)核心編程
- Access 2010數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用項(xiàng)目式教程(第3版)
- Instant QlikView 11 Application Development
- Oracle Exadata專家手冊(cè)
- Mastering React
- Machine Learning With Go
- 編程改變生活:用Python提升你的能力(進(jìn)階篇·微課視頻版)
- Practical GIS
- Java EE Web應(yīng)用開發(fā)基礎(chǔ)
- Visual C++程序設(shè)計(jì)與項(xiàng)目實(shí)踐
- Scrapy網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- Python機(jī)器學(xué)習(xí)開發(fā)實(shí)戰(zhàn)