void linear_regression(String ss[],double a, double theta[]) throws Exception
{
int ssl=ss.length;
double para[]=new double[ssl+1];
para[0]=1;
for(int i=0;i<ss.length;i++)
{
para[i+1]=Double.parseDouble(ss[i]);
}
double y=para[ssl];
double Y = 0.0;
for (int i = 0; i < theta.length; i++)
{
Y =Y+ theta[i] * para[i];
}
for (int j = 0; j < theta.length; j++)
{
theta[j] = theta[j] + a* (y-Y)*para[j];
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
double theta []={1.0,1.0};
String [][] s;
reader re=new reader();
s=re.reader();
for(int i=0;i<s.length;i++)
{
String ss[]=s[i];
linear_regression l=new linear_regression();
l.linear_regression(ss,0.0001,theta);
}
System.out.println("theta1="+theta[0]);
System.out.println("theta2="+theta[1]);
/* for(int k=2000;k<2030;k++)
{
System.out.print(k+":");
System.out.println(theta[0]+k*theta[1]);
}*/
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
public class reader
{
String [][] reader() throws Exception
{
File file=new File("d:/desktop/a.txt");
BufferedReader re=new BufferedReader(new FileReader(file));
int i=0;
String s;
String a[][]=new String[14][];
for(int p=0;p<14;p++)
{
a[p]=new String[2];
}
while((s=re.readLine())!= null)
{
String ss[];
ss=s.split(",");
a[i++]=ss;
}
return a;
}
}