The calculation of the partial derivative can be obtained by calculating the formula of W:
Assuming that the input data is stored in matrix x, the regression coefficients are stored in the vector W. So for a given data 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 "/> The forecast results will be this.width=650 by 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 "/> Given. For x and Y, how do I find w? The common method is to find the W with the least squared error.
The squared error can be written:
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"/>
With a matrix representation can also be written 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"/>. To the W derivative, the solution is as follows:
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 "/>
The data used is a data set that is loaded on the UCI to return the car's msg performance;
Because the data format of the download is not standard, so here wrote a piece of Java code to the format of the data set has been re-structured, the code is as follows:
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); &nBsp; 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 (); } }
The output file is a dataset with two spaces between each variable, where the first item is the dependent variable, or MSG.
The following is a linear regression of a dataset using the Python method:
Import numpy as npimport matplotlib.pyplot as pltnumfeat = len (Open (' Result.txt '). ReadLine (). Split (' ')) datamat = []; labelmat = []fr = open (' result.txt ')//The data in each row is segmented, extracting the data for each row 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])//convert the sequence to matrix Xmat = np.mat (Datamat) Ymat = np.mat (Labelmat). txtx = xmat.t*xmat/determines whether the determinant value is 0if np.linalg.det (xTx) == 0.0: print "wrong"//Use the formula to find the parameters ws = xtx.i* (Xmat.t*ymat)//Use matplotlib drawing, developed in Fig fig = Plt.figure () Ax = fig.add_subplot (111) xcopy = xmat.copy () xcopy.sort (0) yhat = xcopy*ws//here is the relationship between an item in the X matrix and Yhat, as this is the second ax.plot (Xcopy[:,1],yhat) Display Image plt.show ()//Here is the function to find the correlation coefficient, the closer the 1 the better Yhat = xmat*wsprint yhat.t.shapeprint ymat.shapeprint np.corrcoef (YHAT.T,&NBSP;YMAT.T)
Study Log---linear regression implementation