azalea says

Python下的lowess拟合

前段时间用R的lowess function用得很爽,于是想用python也lowess一下。找到了这个东东:

http://mail.python.org/pipermail/python-list/2004-September/281365.html

发现竟然是我崇拜的Istvan写的,是把R里lowess function的C代码包装成python代码。

下载后解压,安装:

Linux下:

sudo apt-get install swig

#因为这个module依赖于swig (Generate scripting interfaces to C/C++ code)

python setup.py build python setup.py install

Windows下: 把win32/2.3 子目录下的2个文件copy到python/site_packages目录

用法:

import lowess ys = lowess.fit(x=X, y=Y, F=F, NSTEPS=NSTEPS, DELTA=DELTA)

具体请参考解压后文件夹中的 lowess_readme.txt

举例: [python] from pylab import * import lowess # 需要matplotlib module, 详见http://matplotlib.sourceforge.net/ length=[176, 218, 113, 102, 250, 309, 135, 150, 200, 263, 135, 114, 123, 118, 100, 109, 100, 94, 184, 303, 117, 80, 110, 108, 123, 85, 85, 138, 163, 184, 196, 225, 172, 199, 206, 118, 108, 98, 123, 99, 125, 124, 133, 95, 102, 93, 102, 85, 89, 89, 237, 236, 331, 173, 85, 257, 331, 111, 107, 120, 117, 116, 123, 111, 140, 221, 182, 111, 337, 191, 178, 177, 191, 205, 186, 135, 151, 195, 400, 249, 264, 207, 113, 153, 102, 208, 184, 107, 109, 187, 187, 333, 138, 160, 109, 377, 337, 213, 221, 206, 190, 215, 211, 411, 181, 226, 379, 167, 217, 154, 106, 73, 193, 295, 118, 117, 121, 168, 138, 124, 689, 163, 105, 122, 113, 143, 120, 139, 170, 198, 112, 100, 95, 100, 100, 93, 126, 92, 142, 103, 126, 132, 212, 221, 86, 124, 157, 174, 189, 133, 160, 111, 100, 106, 123, 104, 183, 107, 118, 103, 132, 226, 212, 199, 185, 359, 230, 195, 205, 221, 105, 115, 116, 221, 221, 377, 271, 272, 290, 126, 221, 132, 217, 98] n, bins, patches = hist(length, 100, normed=0) # Draw a histogram with 100 bins # n is a list of frequency of each bin; bin is a list of the leftmost position of bins # hist()具体参数详见http://matplotlib.sourceforge.net/matplotlib.pyplot.html#-hist ys = lowess.fit(x=list(bins), y=list(n), F=0.2, NSTEPS=2, DELTA=0.2) plot(list(bins),ys,c=’r’,linewidth=2) # plot()具体参数详见http://matplotlib.sourceforge.net/matplotlib.pyplot.html#-plot show() [/python]

图:

此外,Biopython的Statistics包也有lowess模块,不会用-,-

programming python 统计 · Tweet Edit