Loess local smoothing

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.