Python编程系列6-Scipy库

SciPy是世界上著名的Python开源科学计算库,建立在Numpy之上。它增加的功能包括数值积分、最优化、统计和一些专用函数。

本篇文档包括以下内容:
1、文件的输入/输出;
2、统计;
3、信号处理;
4、最优化;
5、插值

1 scipy.io文件的输入和输出

In [1]:
from scipy import io as spio
import numpy as np
In [2]:
# 保存文件
a = np.ones((3, 3))
spio.savemat('file.mat', {'a': a})
#导入文件
data = spio.loadmat('file.mat', struct_as_record=True)
data
Out[2]:
{'__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Mar 15 06:47:47 2017',
 '__version__': '1.0',
 'a': array([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])}

载入txt文件:numpy.loadtxt()/numpy.savetxt()
智能导入文本/csv文件:numpy.genfromtxt()/numpy.recfromcsv()
高速,有效率但numpy特有的二进制格式:numpy.save()/numpy.load()

2 统计

2.1 分析随机数

In [3]:
import scipy.stats as stats
In [9]:
# 生成正态分析的随机数
generated = stats.norm.rvs(size = 900)
generated
Out[9]:
array([  2.91995561e-01,   4.23394920e-01,   1.51373530e+00,
         4.20375684e-01,  -1.84395848e-01,   1.47141997e-02,
         1.30986792e+00,   3.02135017e-01,   5.65506403e-01,
        -2.50445465e-01,   7.14659560e-01,  -3.54103756e-01,
         8.18393064e-01,   1.01818146e+00,  -1.83431799e+00,
        -5.31036609e-01,   3.96669365e-01,  -1.17554640e+00,
         2.88267371e-01,  -8.37390352e-01,   3.67528321e-01,
         5.28392496e-01,   4.59818832e-01,  -4.75492118e-01,
        -1.03408508e-01,   5.99794845e-01,  -1.97344168e+00,
        -8.32686424e-01,   1.10844196e+00,  -8.58263017e-01,
         5.07691243e-01,  -3.39394230e-01,   6.35276755e-01,
         1.91207057e-01,  -3.75750184e-01,  -9.86426169e-01,
        -7.34533938e-01,   6.22772785e-01,  -2.39779856e-01,
        -1.13794163e+00,   1.15743826e+00,  -8.84113057e-01,
         5.34886518e-01,  -1.53075225e+00,  -1.38786559e-01,
        -8.42618235e-01,  -4.61052835e-02,  -9.29650255e-01,
        -2.02122984e-01,  -1.82490183e-01,   5.52516981e-01,
        -6.93633624e-01,   1.14161306e-01,  -1.41196594e+00,
        -1.26506042e+00,   4.33787880e-01,   1.21457423e+00,
         6.33349592e-01,  -1.51573414e+00,   4.48902595e-01,
         1.13461537e+00,  -1.05732690e+00,   2.79508112e-01,
        -9.62079366e-01,   1.56799705e-01,  -1.03382155e-01,
        -9.71018227e-01,  -6.42283670e-01,   1.59596080e+00,
         3.74831614e-01,  -1.20843915e+00,   5.31669253e-01,
        -1.04132208e+00,   1.11071247e+00,   1.70792703e+00,
         4.86504027e-01,   1.38700100e-02,   8.79488592e-01,
        -6.23097850e-01,   3.36863717e-02,   4.11966618e-01,
        -5.58770450e-01,   1.70043439e+00,   6.65604548e-01,
         1.61423011e-01,  -2.79699897e-01,   1.16370529e+00,
         1.84674828e+00,  -6.25982430e-01,  -1.20533925e+00,
        -1.52051751e+00,  -5.47661316e-01,  -1.04164033e-01,
         3.44763447e-01,   4.25307274e-01,  -1.47418724e+00,
        -2.71553332e-01,   6.07998277e-01,  -1.16046091e+00,
        -1.49786062e+00,   2.33303965e-02,  -7.99021712e-01,
         1.55521561e+00,   1.04839313e+00,   2.35028835e-01,
         4.06620559e-01,  -2.37676781e+00,  -5.32362343e-01,
         6.65978168e-01,  -1.88208011e+00,   9.92647528e-01,
        -2.61456590e-02,   2.18597106e+00,   7.27773229e-01,
        -2.18455484e-01,   2.61960522e-01,  -5.64735241e-01,
        -2.75337183e-01,  -5.68348651e-02,   1.28498172e-01,
         9.86048697e-01,   3.27754689e-02,   9.97539226e-01,
         6.75352797e-01,  -4.85782917e-01,   4.75604589e-01,
         5.07875601e-01,  -4.47473058e-01,  -1.10468912e-02,
         3.64075234e-01,  -3.50862594e-01,  -6.82866249e-01,
        -5.51598973e-01,   1.74597091e-01,  -4.98206444e-01,
         2.54783607e+00,   1.67124799e-01,   1.77811008e+00,
        -5.38285765e-01,   5.02347283e-01,   2.57562859e+00,
         3.59365915e-01,   3.22325088e-01,   5.29080096e-01,
         1.93015426e+00,   7.94177575e-01,   8.38649032e-01,
        -1.24143551e+00,  -2.11955883e+00,   7.07511775e-01,
         6.79250959e-02,   1.44672617e+00,  -3.65403805e-01,
         7.19341024e-01,  -1.35689882e+00,  -7.68147942e-01,
        -9.34196053e-01,   1.96570805e+00,  -5.22093312e-01,
        -6.14274711e-01,  -1.00432338e+00,  -2.59400416e-01,
         1.91671021e-01,  -8.45970027e-01,  -2.26076894e+00,
        -7.55856426e-01,   2.63451620e-01,  -1.01197676e+00,
        -9.29121394e-01,   5.46592677e-01,  -2.49677269e-01,
         5.44884347e-01,   9.14766410e-01,  -1.63423532e-01,
         1.15467336e+00,  -1.97342697e+00,   9.48454655e-01,
        -6.33190860e-01,  -3.47984841e-01,  -1.72533889e+00,
         6.32462647e-01,  -1.48487434e-01,  -3.95927041e-01,
         6.07335677e-01,  -1.90501382e+00,  -8.95157261e-01,
         8.37732184e-01,   1.99336148e-01,   2.26820011e-01,
        -4.59827655e-01,  -4.41173001e-01,   4.67073858e-01,
        -5.38511290e-01,   1.55537333e+00,   1.90178328e+00,
         6.37115599e-02,  -9.99639822e-01,  -1.45572305e+00,
        -1.46465190e+00,  -1.44648823e+00,  -3.01083650e-01,
        -1.65503385e+00,  -6.02364884e-01,  -1.01994997e+00,
        -8.05571012e-01,  -7.68327328e-01,   7.66501635e-01,
        -8.44282850e-01,  -1.91511269e+00,  -1.51553597e+00,
         4.88276206e-01,  -1.15016683e-01,   1.05071288e+00,
         4.38049898e-01,  -1.25013703e+00,   1.18294853e+00,
         9.23186349e-01,   7.90603159e-01,  -3.20429751e-01,
        -1.16474516e+00,   3.37262790e-01,  -9.02638825e-01,
         3.87631478e-01,   8.90868788e-01,  -8.93111056e-02,
         7.55225987e-01,  -2.02890647e-01,  -1.27159439e+00,
         5.09285062e-01,   5.70718877e-02,   4.82013836e-01,
         9.45224113e-01,  -6.89211538e-01,   7.57038029e-01,
         1.69194907e+00,  -5.20059699e-01,   3.51822316e-01,
        -1.43756064e+00,   1.18780271e+00,   3.19834196e-01,
        -9.87963594e-03,  -9.18985833e-02,  -7.79765247e-01,
        -3.57494229e-01,   1.40374771e+00,  -2.95799856e-01,
         1.37623442e+00,   8.36728810e-01,  -1.66195444e+00,
        -1.02979350e+00,   3.88040035e-01,  -5.93790522e-01,
        -1.86725144e+00,  -5.28617010e-01,  -1.06596511e+00,
        -1.91025535e+00,   1.04931968e+00,   2.05354643e-01,
         2.94238255e+00,   9.93771874e-01,   1.94350919e+00,
         9.35079222e-01,   4.22194333e-01,  -4.95021643e-02,
        -1.02041618e+00,  -6.98780167e-01,  -1.85659646e+00,
         7.32098100e-01,  -4.05194944e-01,  -4.25548340e-01,
         8.86905188e-02,  -1.94405388e-02,   1.09956415e+00,
        -1.16668429e-03,  -2.77397016e+00,   4.99996550e-01,
        -3.15507837e-01,   1.92255350e-01,  -4.85058545e-02,
         1.07745731e-01,   1.72922518e+00,  -1.05700687e+00,
         2.94405553e-02,  -1.98347462e+00,  -1.69068231e+00,
         7.15235168e-01,  -1.09478287e+00,  -1.52698714e+00,
         1.04016666e+00,  -8.38268376e-01,   8.81294677e-02,
        -6.88028158e-01,   1.44079068e+00,  -2.31292527e-01,
         4.77711605e-01,   1.22950178e+00,  -3.00154113e-02,
         8.90433857e-01,  -2.95976354e-01,  -8.55171013e-01,
         1.12035137e+00,   2.54574196e+00,  -2.07124792e+00,
        -3.17564604e-01,   5.60210270e-01,  -8.32660461e-01,
         5.31686732e-01,   6.94718480e-01,  -1.36117377e+00,
        -9.12002352e-01,   5.78162834e-01,   8.85754309e-01,
        -2.76389009e-01,  -9.57810539e-01,  -1.35285005e+00,
        -6.59562241e-01,   3.51035160e-01,  -2.98034930e-01,
        -1.89834429e-01,  -6.83326742e-01,   3.17170732e-01,
         1.88102707e+00,  -2.15807168e-01,  -1.25498509e+00,
        -7.67792483e-01,   1.03381781e+00,   5.45088896e-01,
         7.85354217e-02,   1.68488155e+00,   1.19966821e+00,
        -1.43313416e+00,   4.25692854e-01,  -4.67574231e-01,
        -1.38909946e+00,  -3.16409933e-01,   2.53439280e+00,
        -6.35589802e-01,  -2.24045479e+00,  -1.25297077e+00,
         8.71386267e-02,  -8.96052541e-01,   4.47937846e-01,
         5.49416290e-01,   5.51129268e-02,   1.44624264e+00,
         2.26652048e+00,  -8.60302003e-01,  -6.94749904e-01,
        -7.61323474e-01,   5.36414866e-01,  -1.00750103e+00,
         2.26881091e-01,  -1.11916335e-01,   9.55137697e-02,
        -1.48777556e-01,   6.81146837e-01,   1.60159205e+00,
         6.90501130e-01,  -1.11838518e-01,  -2.39000282e-01,
        -1.51318841e+00,  -3.72614297e-01,   1.91947275e-03,
        -6.29287299e-01,  -6.36882089e-02,  -4.61046003e-02,
         4.28669644e-01,  -9.73689177e-01,   1.60372580e+00,
        -3.02382830e-01,  -1.74423270e+00,  -5.65691501e-01,
        -6.46891697e-01,   1.19726155e+00,  -3.05857433e-01,
         3.33760152e-01,   1.30723043e-02,  -1.29311743e+00,
         1.77452158e+00,   1.14396517e+00,   3.02926073e-02,
         1.25867950e+00,  -6.39505410e-01,   1.85981511e+00,
         1.42072903e+00,  -2.01662580e+00,  -7.45011233e-01,
        -1.44095905e+00,  -5.42851318e-01,  -5.16875155e-01,
        -3.81903026e-01,  -3.99804013e-01,  -1.75457850e+00,
        -1.52458945e+00,   4.78978119e-01,  -4.25635868e-01,
         8.11233019e-02,   8.10523048e-01,   2.82638674e-01,
         2.94027975e-01,  -1.19723507e+00,   6.17343133e-01,
        -2.92977095e-01,  -1.02550583e-01,   2.51344594e-01,
         4.02551535e-01,   8.41419984e-01,   3.08284248e-01,
        -1.28898076e+00,  -8.30079785e-03,  -1.94602583e+00,
         1.43469096e+00,   1.93354815e+00,   7.71414017e-01,
         5.41289795e-01,  -1.00032704e+00,   6.00377503e-01,
         3.94377979e-01,   3.65937223e-01,  -7.92549889e-01,
        -8.01496059e-01,   4.85722377e-01,   2.31439428e-01,
         6.30215223e-01,  -3.07866853e-01,  -1.86052044e+00,
         1.20320890e+00,   3.79491993e-01,   1.18208781e+00,
         1.07032289e-01,   1.78503688e+00,   1.38855255e+00,
        -4.07074179e-01,   1.69488610e-01,  -7.08733745e-01,
         2.03322749e+00,   1.54258366e+00,  -6.68710364e-01,
         1.09401195e+00,  -3.33774082e-01,   3.42718990e-01,
         2.58697046e-01,  -2.51644126e+00,   7.93648773e-01,
         2.91861881e-01,   5.66450937e-01,  -1.18350194e+00,
         4.07757155e-01,   1.88978404e-01,  -5.30166707e-02,
         3.59709433e-01,  -7.54336929e-01,  -9.38809839e-04,
         9.46623248e-01,  -6.59694800e-01,  -7.15569399e-01,
        -1.54970151e+00,  -2.95394518e-01,   1.88563324e+00,
         1.75979347e+00,   3.66580654e-02,   1.39068720e+00,
        -3.14246938e-01,  -4.45801605e-01,   6.73664592e-01,
         8.71975917e-01,   4.44875247e-01,  -1.19499471e+00,
         3.43948456e-01,   7.61230269e-01,  -2.98279714e-02,
         8.39801194e-02,   2.31128517e-01,  -4.80195898e-01,
         9.95942233e-02,  -1.28336408e+00,  -7.22953546e-02,
         2.29933763e+00,  -7.71905857e-01,   3.31509086e-02,
         1.63312408e-01,   8.39395315e-01,  -9.58912346e-02,
         4.58373033e-01,  -5.30596688e-01,   3.72705501e-01,
        -2.50615745e-01,   1.96232099e+00,   6.91774600e-01,
        -1.44025462e+00,   2.70777672e-01,  -2.77114581e+00,
        -1.84034167e+00,  -3.89198223e-01,   7.90729391e-01,
         1.97643525e+00,  -1.78388542e-01,  -2.45915837e+00,
        -1.15586746e+00,   1.16123586e+00,  -1.30091895e-01,
        -1.67656675e+00,  -4.82424896e-01,   1.14265538e+00,
        -6.15804935e-01,   7.59385933e-01,   5.11107641e-01,
        -5.21127893e-01,   1.29840819e+00,   1.56946418e+00,
        -8.40527743e-01,   6.51606205e-02,   2.24484597e-01,
         1.25383540e+00,   8.71226682e-01,  -2.22602395e+00,
         1.66709032e+00,   8.65449950e-01,  -4.88354706e-01,
         1.44633830e+00,  -7.03362113e-01,  -1.77401341e+00,
        -1.21469380e+00,   6.32348052e-01,   2.68036734e-01,
         2.97525508e-01,   4.22124198e-01,  -1.47335851e+00,
        -2.40170976e-01,  -7.56273078e-01,   6.95642532e-02,
        -1.47857252e+00,  -3.63157118e-01,  -2.84013136e-01,
         1.68955173e+00,   4.54060430e-01,   9.32620228e-01,
         7.01246564e-01,  -1.61682529e+00,   9.00878464e-02,
        -1.79274948e-01,  -2.44066931e-01,  -1.70424947e-01,
        -9.48292405e-01,   6.33594300e-01,  -7.07821112e-02,
        -1.08062797e+00,   2.15684963e+00,   7.33924846e-01,
        -2.90533035e-01,   1.26397597e-01,   1.49869712e+00,
        -1.96448195e+00,   5.73685541e-01,  -1.42726158e-01,
         3.85773636e-01,  -1.58563144e+00,   6.04552996e-01,
         8.69352876e-01,   7.67391012e-02,   1.44068945e-01,
        -6.50147870e-01,  -8.08179839e-01,   1.19762932e+00,
         3.78034184e-01,   1.98608217e-01,   4.89567438e-01,
        -1.16967972e+00,  -6.93606523e-01,  -5.39348944e-01,
         5.63370371e-02,  -1.21150246e+00,  -2.21799185e-01,
        -8.71107357e-01,  -6.19362699e-01,  -7.58579337e-01,
        -1.23508663e+00,  -8.06427971e-01,   5.12018260e-01,
         3.79676231e-01,  -8.36079356e-01,   1.12765265e+00,
         1.47735971e+00,   6.69660072e-01,  -2.04057299e-01,
        -6.61346312e-01,   1.01975742e+00,  -4.81195044e-01,
         1.33587735e-01,  -3.93705388e-01,  -3.17981117e-01,
        -1.08655243e+00,  -5.22495781e-01,  -3.05390975e-01,
         8.57244849e-01,  -1.10511575e+00,   8.35658217e-01,
        -6.99244846e-01,  -5.53952318e-01,  -7.38175031e-01,
         1.31530684e+00,   1.44175622e+00,   9.39697337e-01,
         3.46863060e-01,   1.41321504e+00,   5.10789147e-01,
         1.31812048e+00,  -8.21916393e-01,   1.42010915e+00,
         3.60781510e-01,  -1.08623949e+00,  -1.69844853e+00,
        -4.70276035e-01,  -7.10063605e-02,  -6.19846867e-01,
         7.80308032e-01,   8.56190018e-02,  -1.76932440e-01,
        -8.61069590e-01,   4.56431421e-01,  -1.11009180e+00,
        -9.64914205e-01,   4.80991563e-01,   6.30947710e-02,
         8.73772590e-01,  -3.07968156e-01,  -6.91809356e-01,
        -2.21885382e+00,   5.12569244e-02,   6.40808807e-01,
        -3.94709302e-01,   1.55626509e-01,  -6.23203167e-01,
         6.22951969e-01,   9.46187373e-01,  -1.31414943e+00,
         1.12578169e+00,   2.96379205e-01,  -8.34327048e-01,
        -4.76344059e-01,  -1.11882889e-01,  -1.31734956e+00,
         9.01391723e-02,  -2.60386094e-01,   1.12519035e+00,
        -8.56750769e-01,  -8.07724831e-01,  -9.19691927e-01,
        -1.54537887e-01,   8.99159621e-01,  -1.32420988e+00,
        -1.93096798e-01,  -3.39091205e-01,  -6.47227983e-01,
        -8.30776346e-01,  -2.24398370e+00,   1.47689039e+00,
         1.07728122e-01,  -8.84660266e-01,   1.71002846e-01,
        -1.02263287e+00,  -9.45933175e-01,   1.03253478e-01,
         1.17498962e+00,  -9.30160020e-01,  -3.34858197e-01,
        -2.24496864e+00,  -8.02485465e-01,   5.66894262e-01,
        -1.64575055e-02,  -3.62372533e-01,   1.01481327e+00,
         1.18543681e-01,  -1.02292163e+00,   1.14868753e+00,
         1.24703968e-01,   1.32776670e+00,  -1.64445184e+00,
         6.43965362e-01,   1.77276713e+00,  -1.59837766e+00,
        -1.20352682e+00,  -1.19830510e+00,  -1.06109252e+00,
         1.24609755e+00,  -1.41212661e+00,   7.38484823e-01,
         9.65754904e-01,  -7.57725287e-01,  -4.72981374e-01,
        -5.38287189e-01,  -1.03781402e+00,   2.07377483e+00,
        -7.40714794e-01,   1.63400553e+00,   1.12577127e+00,
        -4.84333930e-01,  -6.56376330e-01,   9.11918983e-01,
        -3.62675952e-01,   3.33312799e-01,  -1.26179812e+00,
         8.57636087e-01,   2.71919312e-01,  -1.17561954e+00,
         2.73632257e-01,  -2.36852513e-01,   1.41591336e+00,
        -7.45501396e-01,  -1.48882181e+00,  -1.82731499e-01,
         2.67183576e+00,  -4.11266525e-01,   5.60018984e-01,
         3.01901677e-01,   3.55424487e-01,   6.57439185e-01,
         5.28834282e-01,  -1.71861292e+00,   5.83918660e-01,
        -4.50316925e-01,  -1.50458318e+00,  -1.16338798e+00,
         7.29694514e-01,   5.32289993e-01,   1.43043276e+00,
         6.04860836e-01,  -8.70443575e-01,   1.86401749e-01,
        -2.84195622e-01,  -1.09458040e+00,  -5.20474046e-01,
        -3.15505939e-01,   3.63550794e-01,   1.19004107e+00,
         1.03042812e-01,   2.27404628e-01,  -1.17627321e-02,
         1.24803793e+00,  -1.79191799e-01,  -1.21301000e+00,
        -1.06838097e-01,  -1.94812704e-01,   1.04147692e+00,
        -1.52034640e+00,   2.00930069e+00,   1.64931997e+00,
         6.24001180e-01,   3.05858238e-01,   2.53841059e-01,
         9.09088800e-01,  -7.20850805e-01,  -1.04387930e+00,
        -2.12975889e-01,  -6.27280058e-01,  -7.60173258e-01,
        -1.22059747e-01,  -5.92602368e-01,   7.00765104e-01,
        -1.02328372e+00,   2.27484699e-01,  -2.31281555e-01,
        -1.88977680e+00,   6.48940156e-01,  -6.68271147e-01,
         3.49167134e-01,   2.16110036e-02,   2.92004848e-02,
        -1.72862274e+00,  -2.61853541e-01,   1.16760916e+00,
        -3.74831617e-01,  -5.99547985e-01,   7.38232889e-01,
         1.04677554e+00,  -1.95062931e+00,  -2.04278288e+00,
         5.84497477e-02,  -8.07539793e-01,  -2.19364752e+00,
        -5.71550725e-01,  -7.26571509e-01,   1.43371143e+00,
        -5.18859730e-02,   1.87108144e+00,  -1.24676105e-01,
        -4.11427629e-01,  -5.37066062e-01,  -4.42155250e-01,
        -9.30856991e-01,   7.37486195e-01,   8.98246411e-01,
         1.83775374e+00,  -3.05048060e-01,   7.94801709e-02,
        -2.92540689e-02,   3.32197244e-02,   6.73491566e-01,
        -7.05561898e-01,   2.68577937e-01,  -6.66434478e-01,
         1.41443303e+00,   5.12422072e-01,  -4.48721313e-01,
         7.36011003e-01,  -2.01417965e+00,  -5.23674297e-01,
        -1.31894517e+00,  -1.29212194e+00,   7.37425637e-01,
         1.46026959e+00,  -5.02240599e-01,   6.51977147e-01,
         1.13412383e+00,   9.65254204e-02,  -5.97981098e-01,
        -1.45283081e+00,  -1.44559140e+00,   5.02674699e-01,
        -8.22388619e-01,  -1.36865798e-01,   1.41644920e+00,
        -2.15360897e+00,   2.84041508e-02,   3.18504981e-01,
        -1.42761824e+00,  -4.66631461e-02,  -1.97584722e-01,
        -8.04854159e-01,   9.02099400e-02,  -2.00101306e+00,
        -1.75629102e-01,   1.07468515e+00,  -5.15195137e-01,
         2.17512336e-01,  -1.02887724e+00,   8.36688610e-01,
         4.81733615e-01,   1.86985715e-01,  -1.37262591e+00,
         8.99349430e-01,   1.51647819e+00,  -2.83399657e-01,
        -2.33745038e+00,   5.98819177e-01,  -1.00274840e+00,
         1.67953835e-02,  -1.69004184e-01,  -1.22409774e-01,
        -8.81518518e-01,  -2.92639517e-02,  -1.29616708e+00,
        -5.98594974e-01,  -7.59329140e-01,   7.59458188e-01,
        -4.01086745e-01,  -8.70314527e-01,  -7.22597598e-01,
        -1.00043696e+00,   4.51217930e-01,   2.33979130e-01,
         1.31096406e-02,  -1.00684773e+00,   5.00992021e-01,
        -1.20464815e+00,   4.57077142e-01,  -1.48003329e+00,
        -7.01909103e-01,   3.46227260e-01,   2.59535531e-01,
         8.56045074e-01,  -4.97825001e-01,   7.06507406e-01,
        -2.34246449e+00,   6.32175595e-02,   5.14811919e-01,
         1.17783385e+00,   1.12729270e+00,   4.33554234e-01,
        -8.69614810e-01,  -5.33971972e-01,   9.88704187e-01,
        -3.05081559e-01,   3.13764782e-01,   3.06294694e-01,
        -1.19265904e+00,   7.09196715e-01,   4.79351769e-01,
         5.95264270e-01,  -9.40091124e-01,  -1.31200123e+00,
        -5.75416929e-01,   1.80849918e+00,  -2.25493484e+00,
         8.81114392e-01,  -1.55665780e+00,  -7.01596238e-01,
         5.43933800e-01,   5.95387756e-01,   9.32241570e-01])
In [10]:
# 用正态分布去拟合生成的数据,得到均值和标准差
Mean,std = stats.norm.fit(generated)
print('Mean = ',Mean,',std = ',std)
Out[10]:
Mean =  -0.0530670389458 ,std =  0.977924988987

偏度(skewnes)描述的是概率分布的偏斜程度,我们需要做一个偏度检验。该检验有两个返回值,其中第二个返回值是p-value,即观察到的数据服从正态分布的概率,取值为0-1

In [11]:
stats.skewtest(generated)
Out[11]:
SkewtestResult(statistic=0.045696219506681769, pvalue=0.96355237706019969)

我们有96%把握认为其服从正态分布

峰度(kurtosis)描述的是概率分布的陡峭程度。该检验和偏度检验类似。

In [12]:
stats.kurtosistest(generated)
Out[12]:
KurtosistestResult(statistic=-1.2269819622972704, pvalue=0.21982937052710516)

正态性检验(normality test)可以检验数据服从正太分布的程度。

In [13]:
stats.normaltest(generated)
Out[13]:
NormaltestResult(statistic=1.507572880280063, pvalue=0.47058134698516707)

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

In [14]:
# 例如得到95%处的数值
stats.scoreatpercentile(generated,95)
Out[14]:
1.5560778745880202
In [15]:
# 同样,可以反过来,得到数值所在的百分比
stats.percentileofscore(generated,1)
Out[15]:
86.222222222222229

使用matplot绘制生成数据的分布直方图

In [16]:
import matplotlib.pyplot as plt
In [17]:
plt.hist(generated)
plt.show()

2.2 样本比对(比较股票对数收益率)

In [18]:
import numpy as np
price = get_price(['000001.XSHE','601398.XSHG'],start_date = '2016-01-01',end_date = '2017-01-01',fields='close')
price_001 = np.diff(np.log(np.array(price['000001.XSHE'])))
price_398 = np.diff(np.log(np.array(price['601398.XSHG'])))

均值检验可以检验两组不同的样本是否有相同的均值,返回值有两个,其中第二个为p-value,取值范围问为0~1

In [19]:
stats.ttest_ind(price_001,price_398)
Out[19]:
Ttest_indResult(statistic=-0.28934838774733274, pvalue=0.77243868777092461)

Kolmogorov-Smirnov检验可以判断两组样本同分布的可能性

In [20]:
stats.ks_2samp(price_001,price_398)
Out[20]:
Ks_2sampResult(statistic=0.094650205761316886, pvalue=0.21508957385193769)

在两支股票对数收益率的差值上运用Jarque-Bera正态性检验

In [21]:
stats.jarque_bera(price_001 -price_398)[-1]
Out[21]:
0.0

3 信号处理

3.1 检验股价的线性趋势

In [22]:
from datetime import date,datetime,time
from scipy import signal
import pandas as pd
from matplotlib.dates import DateFormatter
from matplotlib.dates import DayLocator
from matplotlib.dates import MonthLocator
price = get_price('000001.XSHE',start_date = '2016-01-01',end_date = '2017-01-01',fields='close')
In [23]:
y = signal.detrend(price)
trend = pd.Series(np.array(price) - y,index = price.index)
trend
Out[23]:
2016-01-04    8.358772
2016-01-05    8.363131
2016-01-06    8.367490
2016-01-07    8.371849
2016-01-08    8.376208
2016-01-11    8.380567
2016-01-12    8.384925
2016-01-13    8.389284
2016-01-14    8.393643
2016-01-15    8.398002
2016-01-18    8.402361
2016-01-19    8.406720
2016-01-20    8.411079
2016-01-21    8.415438
2016-01-22    8.419796
2016-01-25    8.424155
2016-01-26    8.428514
2016-01-27    8.432873
2016-01-28    8.437232
2016-01-29    8.441591
2016-02-01    8.445950
2016-02-02    8.450309
2016-02-03    8.454667
2016-02-04    8.459026
2016-02-05    8.463385
2016-02-15    8.467744
2016-02-16    8.472103
2016-02-17    8.476462
2016-02-18    8.480821
2016-02-19    8.485180
                ...   
2016-11-21    9.291571
2016-11-22    9.295930
2016-11-23    9.300289
2016-11-24    9.304648
2016-11-25    9.309007
2016-11-28    9.313366
2016-11-29    9.317725
2016-11-30    9.322083
2016-12-01    9.326442
2016-12-02    9.330801
2016-12-05    9.335160
2016-12-06    9.339519
2016-12-07    9.343878
2016-12-08    9.348237
2016-12-09    9.352596
2016-12-12    9.356954
2016-12-13    9.361313
2016-12-14    9.365672
2016-12-15    9.370031
2016-12-16    9.374390
2016-12-19    9.378749
2016-12-20    9.383108
2016-12-21    9.387467
2016-12-22    9.391825
2016-12-23    9.396184
2016-12-26    9.400543
2016-12-27    9.404902
2016-12-28    9.409261
2016-12-29    9.413620
2016-12-30    9.417979
dtype: float64
In [24]:
# 可以看下面去除趋势的作用
help(signal.detrend)
Out[24]:
Help on function detrend in module scipy.signal.signaltools:

detrend(data, axis=-1, type='linear', bp=0)
    Remove linear trend along axis from data.
    
    Parameters
    ----------
    data : array_like
        The input data.
    axis : int, optional
        The axis along which to detrend the data. By default this is the
        last axis (-1).
    type : {'linear', 'constant'}, optional
        The type of detrending. If ``type == 'linear'`` (default),
        the result of a linear least-squares fit to `data` is subtracted
        from `data`.
        If ``type == 'constant'``, only the mean of `data` is subtracted.
    bp : array_like of ints, optional
        A sequence of break points. If given, an individual linear fit is
        performed for each part of `data` between two break points.
        Break points are specified as indices into `data`.
    
    Returns
    -------
    ret : ndarray
        The detrended input data.
    
    Examples
    --------
    >>> from scipy import signal
    >>> randgen = np.random.RandomState(9)
    >>> npoints = 1000
    >>> noise = randgen.randn(npoints)
    >>> x = 3 + 2*np.linspace(0, 1, npoints) + noise
    >>> (signal.detrend(x) - noise).max() < 0.01
    True

In [25]:
trend.plot()
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f47f0480518>

3.2 傅里叶分析

对去除趋势后的信号进行滤波

In [26]:
from scipy import fftpack
In [27]:
# 运用傅里叶变换,得到信号的频谱
amps = np.abs(fftpack.fftshift(fftpack.rfft(y)))
In [28]:
# 过滤噪音,如果某一频率分量的大小低于最强分贝的10%,过滤
amps[amps < 0.1*amps.max()] = 0
In [29]:
# 将过滤后的信号返回时域,并和去除趋势后的信号一起绘制出来
plt.plot(price.index,y,label = 'datrended')
plt.plot(price.index,-fftpack.irfft(fftpack.ifftshift(amps)),label = 'filtrend')
Out[29]:
[<matplotlib.lines.Line2D at 0x7fae0347c828>]

4 数学优化

  优化算法尝试寻求某一问题的最优解,例如找到函数的最大值或最小值,函数可以是线性的也可以是非线性的。解可能也有一定的约束,例如大于1,。在scipy.optimize模块中提供了一些优化算法,包括最小二乘法。

4.1拟合正弦波

  在上一章节,我们为去除趋势后的数据创建了一个简单的滤波器。我们可以回忆一下,一个正弦波优四个参数决定 Asin(ωx+φ)+k。
A——振幅,当物体作轨迹符合正弦曲线的直线往复运动时,其值为行程的1/2。
(ωx+φ)——相位,反映变量y所处的状态。
φ——初相,x=0时的相位;反映在坐标系上则为图像的左右移动。
k——偏距,反映在坐标系上则为图像的上移或下移。
ω——角速度, 控制正弦周期(单位角度内震动的次数)。

In [30]:
# 定义正弦波模型
def residuals(p,y,x):
    A,k,theta,b = p
    err = y-A*np.sin(2*np.pi*k*x+theta)+b
    return err
In [31]:
# 将滤波后的信号变换为时域
filtered = -fftpack.irfft(fftpack.ifftshift(amps))
pd.Series(filtered,index = price.index).plot()
Out[31]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f47eff59470>
In [32]:
N = len(filtered)
f = np.linspace(-N/2,N/2,N)
p0 = [filtered.max(),f[amps.argmax()]/(2*N),0,0]
p0
Out[32]:
[0.54062009323472304, 0.013374485596707843, 0, 0]
In [33]:
# 调用leastsp函数
from scipy import optimize
plsq = optimize.leastsq(residuals,p0,args = (filtered,f))
p = plsq[0]
In [34]:
p
Out[34]:
array([-0.21649664,  0.01171864, -0.76821357,  0.0065715 ])
In [35]:
fig = plt.figure()
plt.plot(price.index,y,'o',label = 'detrended')
plt.plot(price.index,filtered,label = 'filtered')
plt.plot(price.index,p[0]*np.sin(2*np.pi*f*p[1]+p[2])+p[3],'^',label = 'fit')
plt.legend(prop = {'size':'x-large'})
Out[35]:
<matplotlib.legend.Legend at 0x7f47efebd550>

4.2积分(以高斯积分为例)

高斯积分在概率论和连续傅里叶变换等的统一化等计算中有广泛的应用。在误差函数的定义中它也出现。虽然误差函数没有初等函数,但是高斯积分可以通过微积分学的手段解析求解。

In [365]:
from scipy import integrate
integrate.quad(lambda x: np.exp(-x**2),-np.inf,np.inf)
Out[36]:
(1.7724538509055159, 1.4202636780944923e-08)
In [37]:
np.sqrt(np.pi)
Out[37]:
1.7724538509055159

最常用的数值积分方法是quad,其它可用的积分方案有fixed_quad,quadrature,romberg.

插值

In [38]:
# 创建数据并添加噪音
x = np.linspace(-18,18,36)
noise = 0.1*np.random.random(len(x))
signal = np.sin(x) +noise
In [39]:
from scipy import interpolate
# 创造一个线性插值函数
interpreted = interpolate.interp1d(x,signal)
x2 = np.linspace(-18,18,180)
y = interpreted(x2)
In [40]:
# 执行前一步相同的操作,这里使三次插值
cubic = interpolate.interp1d(x,signal,kind = 'cubic')
y2 = cubic(x2)
In [41]:
plt.plot(x,signal,'o',label = 'data')
plt.plot(x2,y,'-',label = 'lenear')
plt.plot(x2,y2,'-',lw = 2,label = 'cubic')
Out[41]:
[<matplotlib.lines.Line2D at 0x7fae031877b8>]

 

Python编程系列目录:
1. Python编程系列1-变量、字符串与列表
2. Python编程系列2-字典、元组与集合
3. Python编程系列3-条件与循环
4. Python编程系列4-函数
5. Python编程系列5-Numpy库
当前阅读> 6. Python编程系列6-Scipy库
7. Python编程系列7-Pandas库
8. Python编程系列8-Matplotlib库

发表评论

邮箱地址不会被公开。 必填项已用*标注