標籤:機器學習
由對偏導數的計算可以得到w的計算公式:如下
假定輸入資料存放在矩陣x中,而迴歸係數存放在向量w中。那麼對於給定的資料650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://img.blog.csdn.net/20150204222354655?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmRpYW5rZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />,預測結果將會通過650) this.width=650;" src="http://img.blog.csdn.net/20150204222359803?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmRpYW5rZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />給出。對於x和y,如何找到w?常用的方法是找到平方誤差最小的w。
平方誤差可以寫做:
650) this.width=650;" src="http://img.blog.csdn.net/20150204222405668?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmRpYW5rZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
用矩陣表示還可以寫做650) this.width=650;" src="http://img.blog.csdn.net/20150204222409694?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmRpYW5rZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />。對w求導,解得w如下:
650) this.width=650;" src="http://img.blog.csdn.net/20150204222415029?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmRpYW5rZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />
採用的資料是在UCI上下載的迴歸汽車msg效能的資料集;
由於下載的資料格式不標準,因此這裡自己寫了一段java代碼將資料集的格式進行了重新的規整,代碼如下:
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class MyMaze { public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream(new File("E:\\DataRegression.txt")); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); File file = new File("E:\\result.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream)); String line; String newline = null; while((line = bufferedReader.readLine())!=null) { if(line == null) { break; } int length = line.length(); for(int i = 0; i<length; i++) { while(line.charAt(i)==‘ ‘) { if(line.charAt(i+1)!=‘ ‘) { newline = newline + " "; break; } i++; } newline = newline + line.charAt(i); } newline = newline + "\r\n"; newline = newline.substring(4); bufferedWriter.write(newline); newline = null; } bufferedWriter.close(); } }
輸出的檔案是每個變數之間都有兩個空格的資料集,其中第一項是因變數,也就是msg。
下面是採用python方法對資料集進行線性迴歸:
import numpy as npimport matplotlib.pyplot as pltnumFeat = len(open(‘result.txt‘).readline().split(‘ ‘))dataMat = []; labelMat = []fr = open(‘result.txt‘)//這裡對每行的資料進行分割,提取每行的資料for line in fr.readlines(): lineArr=[] curline = line.split(‘ ‘) for i in range(1,numFeat): lineArr.append(float(curline[i])) dataMat.append(lineArr) labelMat.append(float(curline[0]))//將序列轉為矩陣xMat = np.mat(dataMat)yMat = np.mat(labelMat).TxTx = xMat.T*xMat/判斷行列式的值是否為0if np.linalg.det(xTx) == 0.0: print "wrong"//利用公式求參數ws = xTx.I*(xMat.T*yMat)//利用matplotLib畫圖,制定在fig中fig = plt.figure()ax = fig.add_subplot(111)xCopy = xMat.copy()xCopy.sort(0)yHat = xCopy*ws//這裡是找x矩陣中某一項與yHat的關係,如這裡是第二項ax.plot(xCopy[:,1],yHat)//展示映像plt.show()//這裡是求出相關係數的函數,越接近1越好yHat = xMat*wsprint yHat.T.shapeprint yMat.shapeprint np.corrcoef(yHat.T, yMat.T)
學習日誌---線性迴歸實現