Stanford University Machine Learning assignment problem Set #1 regression for denoising Quasar spectra next article

Source: Internet
Author: User

(i) Processing of documents

#-*-Coding:utf-8-*-import numpy as NP import math import matplotlib.pyplot as PLT import csv def read (): Fr=open ( ' Quasar_test.csv ', ' R ') Arrayline=fr.readlines () Y=arrayline[1].strip (). Split (', ') M=len (arrayline) N=len (y ) y2 = Np.zeros ((m-1, n)) x = Np.zeros ((2, N)) x[0] = Arrayline[0].strip (). Split (', ') x[1] = 1 Y=map (Lambda y:float (y), y) y2[0] = weighted_linear_regression (x, Y, 5) q=[] for I in range (len (y2[0)): Q . Append (Y2[0][i]) f1 = open (' Result1.csv ', ' W ') f1.write (str (q)) f1.write (' \ n ') I=1 for line in Arrayl Ine[2:]: y = Line.strip (). Split (', ') y = map (lambda y:float (y), y) Y2[i]=weighted_linear_regressi On (x, Y, 5) q = [] for j in Range (Len (y2[0))): Q.append (Y2[i][j]) print Q i=i+
    1 f1.write (str (q)) f1.write (' \ n ') return y2 def weighted_linear_regression (x,y,t): #加权线性回归 y2=[] For I in range (Len (x[0])): W=np.zeros ((Len (x[0), Len (x[0))) for J in Range (Len (x[0)): W[j][j]=math.exp ((x[0][i)- X[0][J]) * (X[0][i]-x[0][j])/( -2 * t * t)) Xwx=np.dot (Np.dot (x,w), Np.transpose (x)) xwx_inverse=np.linal G.INV (XWX) Xwx_inverse_x=np.dot (xwx_inverse,x) xwx_inverse_x_w = Np.dot (xwx_inverse_x,w) xwx_inver Se_x_w_y=np.dot (Xwx_inverse_x_w,np.transpose (y)) theta=xwx_inverse_x_w_y y2.append (x[0][i] * theta[0] + th
 ETA[1]) return y2 Y2=read ()
(ii)

#-*-Coding:utf-8-*-import numpy as NP import HEAPQ import csv def read (): Fr=open (' result.csv ') Arrayline = f
    R.readlines () x = Arrayline[0].strip (). Split (', ') X=map (lambda x:float (x), X) M=len (arrayline) N=len (x) Y=np.zeros ((m-1,n)) i=0 for line in arrayline[1:: Num=line.strip (). Split (', ') num = map (lambd A num:float (num), num) y[i]=num i=i+1 return X,y x,y=read () def predict (x,y,m): dis=[] for I 
        In range (len (y)): sum = 0 to J in range (150,450): sum=sum+ (Y[m][j]-y[i][j]) * (Y[M][J]-Y[I][J))
    Dis.append (SUM) smallest_four=heapq.nsmallest (4, dis) smallest_three=smallest_four[1:] location=[]
    For I in range (len (smallest_three)): Location.append (Dis.index (smallest_three[i)) H=max (Smallest_three)
            F_left=[] for I in range (m): Top=0 bottom=0 for J in Range (Len (smallest_three)):
         N=LOCATION[J]   top = top + (1-smallest_three[j]/h) * y[n][i] bottom = bottom + (1-smallest_three[j]/h) sum=to  P/bottom f_left.append (sum) return f_left def error (Y): f_left_predict=[] sum_error=[] to M in Range (len (y)): error=0 f_left=predict (x, Y, M) for N in range (len (f_left)): Error=err  or+ (F_left[n]-y[m][n]) * (F_left[n]-y[m][n]) f_left_predict.append (f_left) sum_error.append (Error) Print SUM (sum_error)/len (sum_error) error (Y)

(iii)


#-*-Coding:utf-8-*-import numpy as NP import HEAPQ import Matplotlib.pyplot as Plt def read1 (): #读取数据 Fr=op
    En (' result.csv ') Arrayline = Fr.readlines () x = Arrayline[0].strip (). Split (', ') X=map (lambda x:float (x), X) M=len (Arrayline) N=len (x) Y=np.zeros ((m-1,n)) i=0 for line in arrayline[1:: Num=line.strip ().       Split (', ') num = map (lambda num:float (num), num) Y[i]=num i=i+1 return x,y def read2 (): #读取预测数据 fr=open (' result1.csv ') Arrayline = Fr.readlines () x= Arrayline[0].strip (). Split (', ') X=map (lambd
        A x:float (x), X) M=len (arrayline) N=len (x) Y=np.zeros ((m-1,n)) i=0 for line in Arrayline[1:]:

Num=line.strip (). Split (', ') num = map (lambda num:float (num), num) y[i]=num i=i+1 return x,y
            def predict (x,y,y1,m): #y1为预测数据 dis=[] for I in range (len (y)): sum = 0 for J in Range (150,450): sum=sum+ (y1[M][J]-Y[I][J]) * (Y1[m][j]-y[i][j]) dis.append (sum) smallest_three=heapq.nsmallest (3, dis) location=[] F or I in range (len (smallest_three)): Location.append (Dis.index (smallest_three[i)) H=max (Smallest_three) f_ Left=[] for I in range (m): Top=0 bottom=0 for J in Range (Len (smallest_three)): n= LOCATION[J] top = top + (1-smallest_three[j]/h) * y[n][i] bottom = bottom + (1-smallest_three[
    j]/h) sum=top/bottom f_left.append (sum) return F_left def error (X,Y,Y1): f_left_predict=[] Sum_error=[] for M in range (len (y1)): F_left=predict (x,y,y1,m) error = 0 to n in range (Len (f) _left)): error=error+ (F_left[n]-y1[m][n]) * (f_left[n]-y1[m][n) f_left_predict.append (f_left) s Um_error.append (Error) return F_left_predict[0],f_left_predict[5],sum (Sum_error)/len (sum_error) def figure (X,        EXAMPLE_1,EXAMPLE_6,Y1):#画图 plt.figure (1) plt.xlabel (' Wavelength ') plt.ylabel (' Flux ') plt.scatter (x[0:50],example_1, marker= '. ') Olor= ' B ', label= ' predict value ', s=10) Plt.scatter (x[0:50], y1[0][0:50], marker= '. ', color= ' g ', label= ' real value ', s= Plt.legend (loc= ' upper right ') plt.figure (2) plt.xlabel (' Wavelength ') plt.ylabel (' Flux ') Plt.scatte R (X[0:50], example_6, marker= '. ', color= ' B ', label= ' predict value ', s=10) Plt.scatter (x[0:50], y1[5][0:50], marker= '. ' , color= ' g ', label= ' real value ', s=10-plt.legend (loc= ' upper right ') plt.show () def main (): X,y=read1 () x 1,y1=read2 () example_1, example_6, Error1 = Error (x,y, y1) figure (x,example_1,example_6,y1) If __name__ = ' __mai N__ ': Main ()




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.