Python易筋经-scipy

“视之不见名曰夷,听之不闻名曰希,搏之不得名曰微。
此三者不可致诘,故混而为一。
一者,其上不皦,其下不昧。
绳绳不可名,复归于无物,是谓无状之状、无物之象,是谓惚恍。
迎之不见其首,随之不见其后。
执古之道,以御今之有,能知古始,是谓道纪。”[1]

SciPy

世界上著名的Python开源科学计算库。
官网(scipy.org):能啃英文的还是经常去这里看看。

SciPy函数库在NumPy库的基础上增加了众多的数学、科学以及工程计算中常用的库函数。它的不同子模块相应于不同的应用,像插值、积分、优化、图像处理、统计、特殊函数等等。

常用子模块

子模块 描述
scipy.cluster 聚类工具:矢量量化 / K-均值
scipy.constants 物理和数学常数
scipy.fftpack 快速傅里叶变换
scipy.integrate 数值积分
scipy.interpolate 插值:进行数据处理和可视化分析的常见操作,基于Python的SciPy支持一维和二维的插值运算。
scipy.io 数据输入输出
scipy.linalg 线性代数函数库,如:解线性方程组、最小二乘解、特征值和特征向量、奇异值分解等
scipy.ndimage n维图像包,提供了有关数学形态学的方法
scipy.odr 正交距离回归
scipy.optimize 优化和拟合库
scipy.signal 信号处理
scipy.sparse 稀疏矩阵
scipy.spatial 空间数据结构和算法
scipy.special 特殊数学函数
scipy.stats 统计

使用scipy之前要先导入包,建议不能像numpy那样全部导入,使用哪个子模块导入哪个子模块,如下:

import numpy as np
import pandas as pd
from scipy import io as spio

scipy.io文件的输入和输出

numpy、pandas、scipy.io均可以对文件进行操作。

  • numpy

np.load()
np.loadtxt()
np.genfromtxt()
np.recfromcsv()
np.recfromtxt()
……

  • pandas

pd.read_csv()
pd.read_excel()
pd.read_html()
……

  • scipy.io

spio.loadmat()
spio.savemat()

对上述方法的使用,在之后的分解文章中在介绍,现在先以spio举个例子,创建一个3行4列的矩阵,保存然后读取:

a = np.arange(12).reshape(3,4)
spio.savemat('file.mat',{'a':a})
datas = spio.loadmat('file.mat',struct_as_record=True)
datas

输出:

{'__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sun Mar 18 10:41:36 2018',
 '__version__': '1.0',
 'a': array([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])}

统计:分析随机数

scipy.stats包括统计工具和随机过程的概率过程。各个随机过程的随机数生成器可以从numpy.random中找到。
生成20个正态分布的随机数:

from scipy import stats
generates = stats.norm.rvs(size=20)
generates

输出:

array([ 0.09644491, -0.07552055,  2.4406988 ,  0.22137236, -0.2266991 ,
        1.93962117,  0.57879661, -0.03147269, -0.19289511,  0.40454166,
        0.668479  , -1.27321257, -1.55462663, -0.73369392,  0.55564607,
        0.85396381, -2.17697731, -1.02549411,  0.19205101,  2.7793757 ])

还记得reshape()方法吗,现在强化记忆一下,将generates改变为一个4行5列的矩阵:

generates.reshape(4,5)

输出:

array([[ 0.09644491, -0.07552055,  2.4406988 ,  0.22137236, -0.2266991 ],
       [ 1.93962117,  0.57879661, -0.03147269, -0.19289511,  0.40454166],
       [ 0.668479  , -1.27321257, -1.55462663, -0.73369392,  0.55564607],
       [ 0.85396381, -2.17697731, -1.02549411,  0.19205101,  2.7793757 ]])

接下来,我们拟合生成的一维数据,来生成均值和标准差:

mean,std = stats.norm.fit(generates)
mean,std

输出:

(0.17201995571492329, 1.2122988177111329)

stats.norm.rvs(size=xxx,loc=xxx,scale=xxx)有三个参数,其中loc为均值,scale为标准差

上述生成的正态分布的数据数量太小,我们增加到size=1000,来看几个校验方法:

array([  5.17903592e-01,  -4.84819825e-01,  -2.17331819e+00,
        -2.71978120e-03,   3.43398473e-01,  -1.53672137e+00,
        -7.80230954e-01,  -1.38493510e-01,   8.64678941e-01,
        -5.23193564e-01,   1.00355735e+00,   9.93139686e-02,
         4.46330129e-01,  -1.95042024e-01,  -7.78248002e-01,
         4.94678411e-01,   9.24668576e-01,   9.91269886e-01,
        -3.34111264e-01,   5.35049208e-01,   4.66624215e-01,
         8.74805164e-01,   1.21823570e-01,   5.81397820e-01,
        -4.45018479e-01,   1.04672648e-01,   7.56344139e-01,
         1.60198846e-01,  -1.46477267e-01,  -5.80502049e-01,
         3.99681665e-02,   2.67920793e+00,  -1.49332545e+00,
        -1.39485255e+00,  -1.62184979e-01,  -3.05899731e-02,
         4.97657829e-02,  -2.30222899e-01,  -9.49411984e-01,
        -1.31184285e+00,  -1.41967163e-01,  -4.34813954e-01,
         7.38725143e-01,   4.00598883e-01,   4.36436960e-01,
        -4.09986450e-01,  -2.07197222e-01,  -3.25044931e-01,
        -7.43910151e-01,  -6.13570624e-01,  -1.26727223e+00,
        -3.73803851e-01,  -8.48694345e-01,  -2.75244179e+00,
        -9.64321271e-01,   7.00534558e-01,   1.86793945e-01,
         7.46039130e-01,  -8.10518097e-01,   1.67235474e+00,
        -1.50856260e+00,   5.20941828e-01,  -7.67878811e-02,
         2.32584136e+00,   1.49835809e+00,  -9.05440571e-01,
        -1.24178395e+00,   2.43911036e-01,  -1.20667049e+00,
        -9.16536280e-01,  -4.49705301e-02,   2.30834507e-01,
        -6.92527685e-02,  -5.85348401e-02,   4.23071961e-01,
        -1.07815974e-01,   5.28821852e-01,  -3.22449511e-01,
        -7.12439336e-01,  -1.11656371e+00,   1.20476418e+00,
         2.19543599e+00,  -2.04023860e+00,   1.83482542e+00,
        -1.49249174e+00,  -2.68834446e-01,   1.95818755e-01,
        -3.16677583e-01,  -3.52805238e-01,   7.55090436e-01,
         2.11162353e+00,  -1.53078202e+00,   8.45645296e-01,
        -5.20684353e-01,   8.41674068e-01,   1.27422749e+00,
        -7.00928060e-01,  -5.80290714e-01,   1.00358607e+00,
         3.58235810e-01,  -8.06690609e-01,  -1.97432494e-01,
        -1.85253776e+00,   1.21471527e+00,  -1.32575833e+00,
         2.20498984e-01,  -7.59459595e-01,   6.54102341e-01,
        -1.91811635e-01,  -2.17006655e+00,   1.22757153e+00,
        -1.46243654e-01,  -6.36676291e-01,   3.02682605e-01,
         1.36671612e+00,   1.57930512e+00,   4.04263941e-01,
         1.28662572e+00,   2.49968315e-01,   4.06608797e-01,
        -8.93212197e-01,   1.63066508e+00,   1.23779907e+00,
        -7.84025604e-01,   3.12265366e-02,   6.51420464e-01,
         1.78509544e-01,  -2.68839046e-01,   1.15181648e-01,
        -7.85364176e-01,   1.25247067e+00,   7.72137479e-01,
        -1.18110763e+00,  -1.78637662e-01,  -1.13501810e+00,
        -2.69113774e+00,   2.43586397e-01,   1.07776230e+00,
        -1.33571329e+00,   1.56412663e+00,   7.68541531e-01,
        -3.88497111e-01,  -4.13440012e-01,   1.48619082e+00,
        -2.81005932e+00,   4.76974689e-01,  -4.83140098e-02,
        -2.09960292e+00,  -1.21598418e+00,   5.96742283e-01,
         9.15847172e-01,   2.25765097e-03,   1.54535341e+00,
        -1.47931509e+00,  -2.10377317e+00,  -1.45037025e+00,
         4.57123894e-01,  -1.94574739e+00,  -6.80606280e-01,
         4.76905629e-01,  -1.23517489e+00,  -6.88355573e-01,
         9.43119808e-01,   1.67302968e+00,   7.11875143e-01,
         1.64127917e+00,  -2.94041677e-01,   1.54103126e-01,
        -5.85416826e-01,   2.05999042e+00,   1.18738940e+00,
         1.99531518e-01,  -2.11101357e+00,  -1.09363755e+00,
         1.67983335e+00,   2.99285489e-01,   1.27744550e+00,
         7.36959152e-01,   1.39125694e+00,  -7.56751182e-01,
        -5.90793756e-01,  -3.86542077e-02,   1.81986351e+00,
        -1.06014546e+00,  -1.32346285e+00,  -7.39927880e-01,
         1.38212086e+00,  -5.88060104e-01,  -2.88610199e+00,
         6.45039997e-01,   8.05223241e-01,   1.50847871e+00,
         5.66306325e-01,  -2.22879103e-02,  -5.32065838e-01,
         9.29596147e-01,  -1.00062920e+00,  -7.22940989e-01,
        -6.88290586e-01,   1.49974723e-01,  -3.69190936e-01,
         6.43570526e-01,   1.40973583e+00,  -1.09694233e+00,
        -1.04788396e+00,   1.19138652e+00,  -2.64696938e-01,
         5.66860287e-01,   1.94722056e+00,   7.82355235e-01,
        -9.22911261e-01,  -7.43748090e-01,  -7.18312223e-01,
         3.61035218e-01,  -8.02051485e-01,  -1.25629229e+00,
        -1.11254930e+00,  -2.17500089e+00,  -5.61117834e-01,
         7.75492006e-01,   2.35203990e-01,   4.92911832e-01,
        -4.41602973e-01,  -1.43134294e+00,   1.24202334e+00,
        -1.27805897e+00,  -9.99866924e-01,  -1.71438632e+00,
        -1.79810070e+00,  -1.03384505e+00,  -7.38840610e-01,
         2.59881883e+00,   7.58599225e-01,  -2.91227055e-03,
        -1.08525899e+00,  -5.77098348e-01,   6.44084281e-02,
         1.75831702e+00,  -5.28591510e-01,   7.91843955e-01,
        -1.61140464e-01,   5.41977031e-01,  -2.29088856e-01,
        -1.94228622e-01,   7.53444460e-01,  -1.88985082e-01,
         1.58162684e+00,  -3.77083620e-01,   2.17144773e-01,
         1.02065186e+00,   4.34620425e-01,  -1.39747820e+00,
        -1.61820516e+00,   1.56519909e+00,   1.17291478e+00,
         9.61580854e-01,  -1.12329763e+00,  -7.24531012e-01,
        -8.60033707e-01,  -4.65461266e-01,   2.43657234e-01,
         1.74302788e+00,   1.45065256e-01,   5.57160946e-01,
        -3.26106670e-01,   2.32550557e-01,   1.06177286e+00,
        -6.52730310e-01,   5.12634301e-02,   7.23664099e-03,
        -5.97796521e-01,   2.94120803e-01,  -2.00167304e+00,
        -1.67558293e+00,  -6.90594381e-01,   8.05322517e-01,
         5.99708010e-01,   1.39946854e+00,  -9.12745996e-01,
         1.75628885e-01,  -9.52621164e-01,  -1.63553595e+00,
        -5.73909001e-02,  -2.13752327e-01,   9.08843189e-01,
         7.54338387e-01,   6.86448693e-01,   6.37414903e-01,
        -1.31553804e-01,  -6.14035005e-02,   3.17722855e-01,
         1.00010564e+00,   5.66189109e-01,  -3.31229887e-01,
        -1.26784197e+00,   5.65865115e-01,  -1.34827415e+00,
        -2.51836250e-02,   2.35383999e+00,  -1.25618785e+00,
        -8.50225265e-01,   1.85219586e-02,  -1.19282292e+00,
         1.79456724e-01,  -7.46712449e-01,   2.14327724e+00,
        -1.88544573e+00,  -9.50946030e-01,   1.29378603e-01,
        -2.16246079e+00,   1.50783186e+00,   2.31836239e-01,
         3.67135954e-01,   9.25395949e-01,  -7.43659199e-01,
        -4.63261176e-01,   9.17578865e-01,   6.31962811e-01,
         9.28821246e-01,   2.66833595e-01,   8.04378911e-01,
         3.66038122e-01,   6.52047153e-01,   1.56307516e+00,
        -8.89518014e-01,   3.75768229e-01,  -1.09131872e+00,
         4.55578487e-01,  -7.76866845e-01,   1.80204670e-02,
        -2.21759004e+00,   8.42415305e-01,   3.01510583e-02,
        -7.76409734e-02,  -4.11547372e-01,  -7.55102539e-01,
         2.10526334e-01,  -1.58522602e+00,  -1.35755283e+00,
        -8.58103811e-01,  -6.07561434e-01,   4.98475784e-01,
        -4.65715879e-01,   6.33148206e-01,   2.60342758e-01,
         5.99091782e-01,  -2.26842082e+00,   1.40177396e+00,
        -1.10198446e+00,   1.97454968e+00,   1.08095331e+00,
         1.02132766e+00,   1.17832728e+00,   9.30481505e-01,
        -1.38002873e+00,  -6.23477342e-01,   7.77572604e-01,
        -4.16849947e-02,   5.85935975e-02,  -3.43723693e-01,
        -1.29803389e+00,  -7.85437549e-01,   2.09918513e+00,
        -8.32242053e-02,   1.34756860e+00,   4.20666337e-01,
        -6.23077815e-01,   3.83333826e-01,   1.07637533e+00,
        -5.36798669e-01,   2.26184723e-01,  -1.71843124e+00,
         2.88628271e+00,  -1.66953870e+00,  -1.04128898e+00,
         7.29756517e-02,   9.16548743e-01,   1.00785434e+00,
         8.87834244e-01,   8.51919889e-01,  -3.92304884e-01,
        -2.90512265e-01,   5.32853057e-01,  -7.79351441e-01,
        -3.38995321e-01,  -1.78560587e-01,  -1.47718052e-01,
        -1.22337547e+00,  -4.14363367e-01,   1.78478385e+00,
         8.81224994e-01,  -2.86509296e-01,  -1.47140331e+00,
         2.00232111e-01,  -1.08666985e+00,   8.80514936e-01,
         8.45693941e-01,  -1.26209911e+00,  -1.43224161e+00,
         1.29518395e+00,   7.95465444e-01,   5.34646568e-01,
        -1.54851329e-01,  -1.34519089e-01,   8.43642647e-01,
        -7.02888200e-01,   1.10989002e+00,   7.10434593e-01,
         1.27288284e+00,  -7.02412620e-01,  -2.47260071e-02,
         1.86315225e+00,   3.18732128e-01,   2.85852319e-01,
        -2.89281323e-02,   1.64308914e+00,   6.78905592e-01,
        -2.23898207e+00,   6.45227361e-01,  -2.10722006e-01,
        -5.15334081e-02,   5.21718062e-01,  -4.28589133e-01,
        -3.32994850e-01,  -9.96564936e-01,   4.43139233e-02,
         8.34243415e-01,   2.17024275e-01,   6.48832946e-01,
         1.39552210e+00,   5.45378253e-01,   1.23680500e+00,
         1.36330855e+00,  -6.91938875e-01,  -1.32466443e+00,
         1.40977288e-01,   1.87758626e+00,   3.10942400e-01,
        -5.41959267e-01,   1.65385704e+00,   9.33377795e-01,
         8.91428028e-02,  -8.52805953e-01,   1.58607271e-01,
        -5.52939995e-01,   3.30720367e-01,  -9.75222073e-02,
         9.08906746e-01,  -4.77320009e-01,  -6.70020450e-01,
        -1.72473895e+00,   1.25450455e-02,   1.54247857e-02,
         4.77084668e-01,   9.74026348e-01,   4.66092420e-01,
        -1.86742467e-01,   5.19324577e-02,  -1.79982729e+00,
        -3.79270866e-01,   1.94272791e-02,  -2.28700755e-02,
        -1.44835613e+00,  -6.77897503e-01,  -3.76212765e-01,
         8.90054522e-01,   1.25702761e+00,  -1.17640677e-01,
         1.73785564e+00,   3.47620655e-01,  -9.67679848e-01,
         9.38381020e-01,  -2.02956574e+00,   1.03903973e-01,
         6.89777724e-01,   1.15719338e+00,   5.02387689e-01,
         1.65414778e+00,  -4.79422190e-02,   4.71465797e-01,
         4.71210862e-01,  -2.95445178e-01,  -9.27952481e-01,
        -2.84518822e-01,   4.50762651e-01,   3.57838417e-01,
         1.05556381e+00,  -1.25461973e+00,   5.25710748e-01,
         1.18346547e+00,  -6.36131832e-02,   1.99996624e+00,
        -4.05626343e-01,   5.02440229e-01,   3.06722159e-01,
         1.12961182e+00,   2.71252139e-01,   1.02751704e+00,
         6.58981355e-01,  -1.33754483e+00,  -1.88208047e+00,
         9.65909726e-02,   2.17877879e+00,   1.25656355e-01,
         1.97683689e+00,   1.88306505e+00,   3.93711759e-01,
         1.11480906e+00,   5.43163950e-02,  -9.70974509e-01,
         5.91343120e-01,   6.03472480e-01,  -1.41207708e-01,
        -3.57354046e-01,   8.83769732e-01,  -7.98277396e-01,
         5.34234315e-01,  -1.76017958e-01,  -6.70988950e-01,
        -9.92681894e-01,   1.02535613e+00,   7.86056043e-01,
         1.08465761e+00,   9.28624272e-01,  -1.29234470e-01,
        -8.71688683e-01,  -1.43193619e+00,  -9.92240568e-01,
         4.50317041e-01,  -7.83975597e-01,   3.25406806e-01,
         2.16974723e+00,  -8.98345345e-01,  -1.21821660e+00,
        -2.05393549e-01,  -6.97514681e-01,  -9.84930653e-01,
         2.92589293e-01,   1.75465316e+00,   2.89391096e-02,
        -5.70833465e-01,  -8.26878492e-01,   9.38516497e-02,
        -1.10404029e+00,   4.79417673e-01,   9.06859139e-01,
         2.84728627e-01,   7.81807537e-01,   5.94857977e-01,
        -2.39704052e+00,  -3.95317906e-01,   6.00154490e-01,
        -2.21365308e+00,   9.59143039e-01,   6.61087230e-01,
        -2.11076654e-01,  -6.53571930e-01,   2.17956383e+00,
         1.16704615e-01,   9.98712389e-01,  -1.62361914e+00,
        -2.50632371e-02,  -4.45459791e-01,  -2.28138155e-02,
        -3.59760026e-01,  -1.25357919e+00,   4.71340851e-02,
         1.40328071e+00,  -4.12591851e-01,   7.85809019e-01,
        -1.70616337e+00,   1.88482833e+00,   1.96424743e+00,
        -1.76543942e-01,  -3.80571853e-01,   6.87730233e-01,
        -1.71387963e-01,  -2.61269664e-01,   1.94983128e+00,
         7.46702909e-01,  -1.55802291e-01,   1.54348840e-01,
         4.84993880e-01,  -6.36064763e-01,  -7.77626949e-01,
         6.92906103e-01,   5.86780454e-01,  -1.71396865e+00,
         1.12970593e+00,  -7.31194601e-01,   1.44478155e+00,
         6.40973401e-01,  -3.27758206e-01,  -4.14741504e-01,
        -5.04916600e-01,  -9.43490941e-01,   1.74154128e+00,
        -7.45088483e-01,  -4.93447353e-02,  -5.05840112e-01,
         7.32234476e-01,   3.59136225e-02,   2.33032593e-01,
         6.78306052e-01,   5.33781323e-01,  -3.08066212e+00,
         5.36753627e-01,   2.21618896e+00,  -5.49002253e-01,
        -3.96205884e-02,   9.45672191e-01,   2.69695877e-02,
         1.86118132e-01,  -7.54374668e-02,  -6.00106159e-01,
         4.73807193e-02,   4.10719535e-02,  -2.88734543e-01,
        -1.52709149e+00,   8.63269201e-01,   7.52445801e-01,
         9.91226325e-01,  -1.65014451e-01,  -6.19173218e-01,
         1.24788844e+00,   1.98489106e+00,  -1.06515959e+00,
        -8.80476915e-01,   1.78981587e-01,   1.83692612e-01,
         1.53652830e+00,   5.00586066e-01,   2.78319035e-01,
         8.13336479e-01,  -8.53280967e-01,   6.95101671e-01,
        -7.84522637e-01,   6.21645705e-01,  -6.90717051e-01,
        -1.34331132e-02,  -4.63942849e-01,   8.46034390e-01,
         7.79139532e-01,   1.11506948e+00,  -7.58189523e-01,
         3.55010113e-01,   1.93401630e-02,   1.33945655e+00,
        -1.70378430e+00,   6.07595306e-01,   9.49078710e-01,
         1.65709736e+00,  -1.80847756e+00,   1.32234976e+00,
         2.31073785e+00,   2.47285661e-01,   4.83819946e-02,
         1.20939114e+00,   1.20967340e-01,   1.37374500e+00,
         4.33215652e-01,   5.92812032e-01,  -1.65824855e+00,
         7.62256866e-01,  -1.55651842e-01,   7.17398184e-01,
         9.81556110e-01,  -1.54787315e-01,  -1.20975829e+00,
         9.23432661e-01,   3.00827852e-01,  -6.14500486e-01,
        -3.26059019e-01,   1.41971663e+00,  -5.59087951e-01,
        -8.85611495e-01,  -3.77072359e-01,   1.90769576e+00,
        -1.27281422e+00,  -2.57577623e+00,  -1.29056980e+00,
         1.04105636e+00,   3.83955638e-01,  -2.41923810e-01,
         1.13747024e-01,   6.61993215e-01,   1.21725708e+00,
         5.53782077e-01,   5.93063386e-01,  -1.63011322e-01,
        -1.78736820e+00,   2.08120622e+00,  -3.66627082e-02,
         1.10356977e+00,  -3.66057113e-01,  -6.43033926e-01,
        -2.22016112e-02,   2.72905811e-01,  -2.67741983e-01,
         3.01415027e-01,   2.09089119e+00,   8.84618006e-01,
        -1.11558291e+00,   5.01184306e-01,   8.17462556e-01,
         1.23994641e+00,  -9.55828715e-01,   3.50830796e-01,
         1.13987551e+00,  -1.03647418e+00,  -3.39220874e-01,
        -1.21505253e+00,  -7.92370242e-01,  -3.92509918e-01,
        -3.54354051e-01,   7.32386956e-01,   1.49858239e+00,
        -1.40571944e+00,   9.71382936e-01,   1.84319717e-01,
        -2.62560983e+00,   3.15488537e-01,  -4.80518437e-01,
         8.72448418e-01,   6.51341675e-01,  -2.84050110e-02,
         3.51781188e-01,   7.02565779e-01,  -6.07502248e-01,
         6.52757394e-01,  -6.66621271e-01,   5.82922370e-02,
        -9.46398829e-01,  -6.71252481e-01,   1.33902920e+00,
         3.93403612e-02,  -1.52342841e+00,   9.93710825e-01,
        -2.86315569e-01,  -1.39171884e+00,   1.02611414e+00,
         2.18283569e+00,   1.42696284e+00,   5.15848836e-01,
         1.15392975e+00,  -5.34657374e-01,  -2.01754365e-01,
        -7.68709837e-01,   3.85433257e-01,  -6.97352436e-01,
         2.58712465e-01,  -2.49628821e-01,   1.84068402e-01,
        -6.25925483e-01,  -1.81143334e-01,   7.18039153e-01,
         5.59074589e-01,   6.65999999e-01,  -1.57075929e+00,
         5.58054042e-01,   7.44764249e-01,  -2.83785763e-01,
        -8.82562234e-02,   1.92806935e+00,  -9.68349222e-01,
         1.77906671e-01,  -8.25029579e-01,  -6.01310896e-01,
         1.95236866e-01,  -7.26205428e-01,  -6.06617076e-01,
        -1.66194008e-01,  -6.91522476e-01,   6.84314550e-01,
         5.83863823e-01,   4.93299245e-01,   9.82188985e-01,
         5.98147329e-01,  -5.18005846e-01,   1.58550046e-02,
         9.56595947e-03,  -2.45461199e-01,  -1.59207521e-01,
        -6.58198078e-01,  -2.13816850e+00,   1.59670116e+00,
        -6.09477093e-01,  -8.68643240e-02,  -4.41217685e-01,
        -4.11676773e-01,  -3.86013225e-01,  -3.92845894e-01,
        -2.52556017e+00,  -4.70652232e-01,   7.13473499e-01,
         1.94145904e-01,   7.56379495e-01,   4.09956072e-01,
         1.01246574e+00,  -5.18557610e-01,  -1.00192343e+00,
         4.41193746e-01,   2.82296893e-01,  -4.04582442e-01,
         1.71627030e-01,  -2.89539391e-01,   2.07682050e-03,
         1.04429805e+00,   5.40968294e-01,   1.34123694e+00,
        -2.80530112e+00,  -1.78870093e-02,  -5.97238191e-01,
         1.51157855e+00,  -7.27499261e-01,   9.12291671e-01,
        -5.35387214e-01,  -7.87289968e-01,  -1.82503774e-01,
         5.80673936e-01,   5.29634071e-01,  -6.96241271e-01,
        -1.56308494e+00,  -1.34181311e+00,   1.88487286e-01,
         1.40328626e+00,   6.00158404e-03,   6.55402421e-01,
        -1.61848068e-01,  -5.56679709e-01,  -2.26336297e-01,
        -5.73583832e-01,   1.96308115e+00,  -2.27547535e+00,
         8.43547860e-02,  -1.02630303e+00,  -7.40963015e-01,
        -3.29592672e-01,  -5.69506261e-01,   8.91549259e-01,
         3.67141173e-02,  -1.82231172e-01,  -9.51808458e-01,
        -4.75730687e-02,  -1.35256178e+00,   1.70960760e+00,
        -3.07172061e-01,   6.10801830e-01,   8.84296909e-01,
        -7.41061578e-01,   8.67690161e-01,   3.97588182e-01,
         8.21544341e-01,   3.30951455e-01,  -9.64367490e-01,
        -2.32152527e+00,   2.06939047e-01,   5.17611759e-02,
         2.22478518e+00,   4.40548750e-01,   1.39511688e-01,
         9.11680945e-01,  -1.12968322e+00,   9.10339662e-02,
        -9.90348342e-01,  -3.21560548e-01,  -1.33334300e+00,
        -1.01528637e+00,   3.44899432e-01,  -1.23835928e-01,
        -2.42770452e-02,  -9.05990669e-02,  -2.23922132e-01,
         7.47437385e-02,   7.53188446e-01,   1.82319928e+00,
        -2.66017608e-01,  -6.68749559e-02,   5.28398887e-02,
        -2.51901320e+00,   1.95600370e-01,  -4.77062768e-01,
         5.67450564e-01,  -7.38470031e-01,  -3.24464380e-01,
         3.63784870e-01,   3.75991861e-02,  -5.36347649e-01,
        -4.76756279e-01,   8.01781848e-01,   9.37678614e-01,
        -1.04848674e+00,  -1.19848372e+00,  -2.26439384e-01,
         1.36731011e+00,  -5.55136003e-01,  -2.60192909e+00,
        -1.54447063e-01,   1.96487305e+00,   1.35678183e-01,
        -1.67608204e+00,   1.12327576e+00,   5.81903509e-01,
         1.99026842e-01,   9.63293877e-01,   3.97108619e-01,
        -1.36076612e+00,  -3.22396458e-01,  -2.15067999e-01,
         6.18030436e-01,  -7.79655733e-03,   9.92891584e-01,
        -2.05604724e-03,  -8.96901674e-01,  -1.06635153e-01,
        -1.32070422e+00,  -7.53853714e-01,   5.09336526e-01,
         4.85341330e-01,   2.20445035e-01,  -1.78342545e-01,
         4.49018230e-02,  -3.36686870e-01,  -4.37369163e-02,
        -2.76415754e-02,  -9.74971071e-01,  -3.21277080e-01,
        -7.29857321e-01,  -2.71822112e+00,  -1.40805153e-01,
        -6.31972103e-01,  -1.18447030e+00,   4.70073687e-01,
         3.75218129e-01,   2.01854049e-02,   1.15084772e+00,
         1.37046917e+00,   2.19092559e-01,  -2.16799568e+00,
        -1.38993800e+00,  -1.46832196e+00,   6.74797051e-01,
        -2.23831431e+00,   7.86267279e-01,  -1.01850491e-02,
         5.23336585e-01,   5.73172679e-01,  -1.42427763e+00,
         9.78055988e-01,  -3.46729387e-01,  -4.18502508e-01,
        -7.61408845e-01,   1.00407787e-01,   9.71959636e-01,
         5.27960428e-01,  -1.36139193e+00,  -1.61909351e+00,
        -3.08090260e-01,  -7.25495879e-01,  -4.17010258e-01,
        -7.57455300e-01,  -9.88841992e-01,  -1.47721728e-01,
        -4.17639882e-02,   1.93209520e+00,  -2.90412408e-01,
         1.55586602e-01,   1.37128322e+00,   1.98776904e+00,
        -3.36005361e-01,   9.97719209e-01,   1.30942021e+00,
        -1.46843593e+00,   1.48923333e+00,  -9.11947452e-01,
        -1.17175494e+00,   6.09295874e-01,  -3.37116459e+00,
        -1.41109046e+00,   3.76436330e-01,   1.22230034e+00,
         3.13684661e-01,  -1.84230369e+00,   3.54782080e-01,
        -1.52693323e-01,  -1.82877549e+00,  -4.33652228e-01,
        -2.92262709e-01,  -4.26154081e-02,   1.20177625e+00,
         8.34324054e-01,   2.33224691e-01,  -4.94941496e-01,
        -1.30788518e+00,   5.31515478e-01,  -8.20831293e-01,
        -1.13579394e+00,   1.45329400e-01,  -5.20324531e-01,
         1.00047710e+00])
  • 偏度 skewnes
    概率分布的偏斜程度,我们需要做一个偏度检验。该检验有两个返回值,其中第二个返回值是p-value,即观察到的数据服从正态分布的概率,取值为0-1
stats.skewtest(generates)

输出:

SkewtestResult(statistic=0.73500197175279103, pvalue=0.4623382932849327)
  • 峰度 kurtosis
    概率分布的陡峭程度。该检验和偏度检验类似。
stats.skewtest(generates)

输出:

KurtosistestResult(statistic=0.45424860606802309, pvalue=0.64964990231896591)
  • 正态性检验 normality test
    检验数据服从正太分布的程度。
stats.normaltest(generates)

输出:

NormaltestResult(statistic=5.2884112715980525, pvalue=0.07106178025936169)

使用Scipy我们很方便的得到数据所在区域中某一百分比处的数值:
例如:得到75%处的值:

stats.scoreatpercentile(generates,75)

输出:

0.66404134087859246

或者反过来,给定一个值,获取处在的百分位:

stats.percentileofscore(generates,0.66)

输出:

74.8888888888889

文到此处,可以用matplotlib库画个图看看了,关于matlotlib我单独出一篇笔记来记录。

import matplotlib.pyplot as plt
plt.hist(generates)
plt.show()

输出图形:


normaltest.png

先写到这儿,scipy分多篇来记录吧。


  1. 老子《道德经》第十四章,老子故里,中国鹿邑。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容