least squares algorithm------regression

Source: Internet
Author: User

Linear regression refers to the attempt to learn a linear model to predict real-value output markers as accurately as possible.

Least squares: The method of solving the model based on the minimization of mean square error.

By becoming the least squares method (perhaps not the most simplified, to improve the programming ability)

/** This is a linear regression problem with least squares*/classlinearregression {DoubleA; Doubleb; intN;  PublicLinearregression (Double[] Arrx,Double[] arry) {        //TODO auto-generated Constructor stub        DoubleT1 = 0; DoubleT2 = 0; DoubleT3 = 0; DoubleT4 = 0; N=arrx.length;  for(inti = 0; I < n; i++) {T1= Arrx[i] +T1; T2= Arry[i] +T2; T3= arry[i] * Arrx[i] +T3; T4= arrx[i] * Arrx[i] +T4; } A= (T3*N-T1 * T2)/(T4*N-T1 *t1); b= (T2-A * T1)/N;        System.out.println (a);    System.out.println (b); }    voidPrint () {System.out.println ("The result of using linear least squares is y=" + A + "*x+" +b); }    voidPredictDoublenum) {        Doubleresult = num * A +b; System.out.println ("The result of using least squares is:" +result); }} Public classRegressdemo { Public Static voidMain (string[] args) {DoubleArrx[] =New Double[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; DoubleArry[] =New Double[] {23, 44, 32, 56, 33, 34, 55, 65, 45, 55 }; linearregression LR=Newlinearregression (Arrx, Arry); Lr.        Print (); Lr.predict (1.0); }}

Least squares method for regression of algorithm------

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.