Code
#-*-Coding: GBK -*-#################################### ##################################### copyright (c) 2013 All Rights Reserved. # file name: loess. PY # Creator: Liu Yu finallyly
# Creation Date: July 22, January 27, 2013 # Description: # backup note: ######################################## ###################################! /Usr/bin/Python # Please add your code here! Import re; import sys; import time; import matplotlibmatplotlib. Use ('authorization') # Must be before importing matplotlib. pyplot or pylab! From pylab import * # x location; H: bandwith; XP, YP: data points (vectors) def loess (x, H, XP, YP ): W = exp (-0.5 * (X-XP)/h) ** 2)/SQRT (2 * pI * H ** 2 ); B = sum (w * XP) * sum (w * YP)-sum (W) * sum (w * XP * YP); B/= sum (w * XP) ** 2-sum (W) * sum (w * XP ** 2); A = (sum (w * YP)-B * sum (w * XP )) /sum (w); return a + B * X; def printusage (): SYS. stderr. write ("program [in] \ n"); exit (1); If (_ name __= = "_ main _"): If (LEN (sys. argv )! = 2): printusage (); starttime = time. clock (); D = loadtxt (STR (sys. argv [1]); S1 = []; S2 = []; for K in D [:, 0]: s1.append (loess (K, 5, d [:, 0], d [:, 1]); s2.append (loess (K, 100, d [:, 0], d [:, 1]); xlabel ("Day in year"); ylabel ("draf number"); GCA (). set_aspect ('equal'); plot (d [:, 0], d [:, 1], 'O', color = "white", markersize = 7, linewidth = 3); plot (d [:, 0], S1, 'K-', d [:, 0], S2, 'K --'); q = 4; axis ([1-Q, 366 + Q, 1-Q, 366 + q]); savefig ("draf Tlottery.png "); endtime = time. Clock (); interval = endtime-starttime; SYS. stderr. Write (" % s has finished congratulations! \ N "% STR (SYS. argv [0]); SYS. stderr. Write (" time elapse: % F \ n "% interval );
Data address shard
Run ../python2.7 loess. py draft70yr.dat.txt
Result: