Logic regression (LR) algorithm Java implementation of <转> __ algorithm

Source: Internet
Author: User
Tags readline
Follow the machine to learn the actual Python code, with the Java rewrite LR gradient rise algorithm:

Package com.log;

Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import Java.io.InputStreamReader;
Import Java.io.File;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import Jama.matrix;

public class Logprocess {
/**
* Get Feature data
*/
Public Matrix Getdatamat () {
try {
arraylist<double[]> list = new arraylist<double[]> ();
String pathname = "C:\\testset.txt";
File filename = new file (pathname);

InputStreamReader reader = new InputStreamReader (
New FileInputStream (filename));
BufferedReader br = new BufferedReader (reader);

String line = "";
line = Br.readline ();

while (line!= null) {
String [] Tmp=line.split ("T");
Double [] value=new double[3];
value[0]=1.0;
Value[1]=double.parsedouble (Tmp[0]);
Value[2]=double.parsedouble (tmp[1]);
List.add (value);

line = Br.readline (); Read one row of data at a time
}


Feature into a two-dimensional array
iterator<double[]> datalit = List.iterator ();
Double [] data = new Double[list.size ()][3];
int i=0;
while (Datalit.hasnext ()) {
Double [] tmp = Datalit.next ();
DATA[I][0]=TMP[0];
DATA[I][1]=TMP[1];
DATA[I][2]=TMP[2];
i++;
}

Matrix Datamatrix = new matrix (data);

return Datamatrix;
catch (Exception e) {
E.printstacktrace ();
return null;
}
}

/**
* Get Tag data
*/
Public Matrix Getlabelmat () {
try {
arraylist<int[]> list = new arraylist<int[]> ();
String pathname = "C:\\testset.txt";
File filename = new file (pathname);

InputStreamReader reader = new InputStreamReader (
New FileInputStream (filename));
BufferedReader br = new BufferedReader (reader);

String line = "";
line = Br.readline ();

while (line!= null) {
String [] Tmp=line.split ("T");
int [] value=new int[1];
Value[0]=integer.parseint (tmp[2]);
List.add (value);

line = Br.readline (); Read one row of data at a time
}

Convert a label to a two-dimensional array
iterator<int[]> labelit = List.iterator ();
Double [] label = new Double[list.size ()][1];
int j=0;
while (Labelit.hasnext ()) {
Label[j][0]=labelit.next () [0];
j + +;
}

Matrix Labelmatrix = new matrix (label);

return Labelmatrix;
catch (Exception e) {

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.