官术网_书友最值得收藏!

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):

SciPy organization

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).

主站蜘蛛池模板: 绥中县| 鹿泉市| 永康市| 浦县| 蛟河市| 吉林市| 西盟| 竹北市| 综艺| 合作市| 永靖县| 贞丰县| 遂平县| 辽宁省| 化德县| 图木舒克市| 灵武市| 松滋市| 深泽县| 洛阳市| 馆陶县| 公安县| 重庆市| 苍山县| 积石山| 九江市| 雷山县| 长治市| 昌邑市| 济宁市| 巩义市| 博白县| 峨眉山市| 嘉峪关市| 淮南市| 汤原县| 张家口市| 武川县| 景宁| 遵义县| 舒兰市|