make some fix

This commit is contained in:
yanqingmen
2015-06-10 20:09:49 -07:00
parent f91a098770
commit c110111f52
22 changed files with 234 additions and 162 deletions

View File

@@ -24,6 +24,7 @@ import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.demo.util.DataLoader;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* a simple example of java wrapper for xgboost
@@ -53,22 +54,23 @@ public class BasicWalkThrough {
//specify parameters
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
put("objective", "binary:logistic");
}
};
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//set round
int round = 2;
//train a boost model
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, round, watchs, null, null);
//predict
float[][] predicts = booster.predict(testMat);
@@ -107,8 +109,11 @@ public class BasicWalkThrough {
DMatrix trainMat2 = new DMatrix(spData.rowHeaders, spData.colIndex, spData.data, DMatrix.SparseType.CSR);
trainMat2.setLabel(spData.labels);
dmats = new DMatrix[] {trainMat2, testMat};
Booster booster3 = Trainer.train(param, trainMat2, round, dmats, evalNames, null, null);
//specify watchList
WatchList watchs2 = new WatchList();
watchs2.put("train", trainMat2);
watchs2.put("test", testMat);
Booster booster3 = Trainer.train(param, trainMat2, round, watchs2, null, null);
float[][] predicts3 = booster3.predict(testMat2);
//check predicts

View File

@@ -19,6 +19,7 @@ import org.dmlc.xgboost4j.Booster;
import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* example for start from a initial base prediction
@@ -35,19 +36,20 @@ public class BoostFromPrediction {
//specify parameters
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
put("objective", "binary:logistic");
}
};
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//train xgboost for 1 round
Booster booster = Trainer.train(param, trainMat, 1, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, 1, watchs, null, null);
float[][] trainPred = booster.predict(trainMat, true);
float[][] testPred = booster.predict(testMat, true);
@@ -56,6 +58,6 @@ public class BoostFromPrediction {
testMat.setBaseMargin(testPred);
System.out.println("result of running from initial prediction");
Booster booster2 = Trainer.train(param, trainMat, 1, dmats, evalNames, null, null);
Booster booster2 = Trainer.train(param, trainMat, 1, watchs, null, null);
}
}

View File

@@ -32,12 +32,12 @@ public class CrossValidation {
//set params
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "3");
put("silent", "1");
put("nthread", "6");
put("eta", 1.0);
put("max_depth", 3);
put("silent", 1);
put("nthread", 6);
put("objective", "binary:logistic");
put("gamma", "1.0");
put("gamma", 1.0);
put("eval_metric", "error");
}
};

View File

@@ -16,7 +16,6 @@
package org.dmlc.xgboost4j.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.dmlc.xgboost4j.Booster;
import org.dmlc.xgboost4j.IEvaluation;
@@ -24,6 +23,7 @@ import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.IObjective;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* an example user define objective and eval
@@ -130,18 +130,19 @@ public class CustomObjective {
//set params
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
}
};
//set round
int round = 2;
//set evaluation data
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "eval"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//user define obj and eval
IObjective obj = new LogRegObj();
@@ -149,6 +150,6 @@ public class CustomObjective {
//train a booster
System.out.println("begin to train the booster model");
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, obj, eval);
Booster booster = Trainer.train(param, trainMat, round, watchs, obj, eval);
}
}

View File

@@ -19,6 +19,7 @@ import org.dmlc.xgboost4j.Booster;
import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* simple example for using external memory version
@@ -35,25 +36,26 @@ public class ExternalMemory {
//specify parameters
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
put("objective", "binary:logistic");
}
};
//performance notice: set nthread to be the number of your real cpu
//some cpu offer two threads per core, for example, a 4 core cpu with 8 threads, in such case set nthread=4
//param.put("nthread", "num_real_cpu");
//param.put("nthread", num_real_cpu);
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//set round
int round = 2;
//train a boost model
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, round, watchs, null, null);
}
}

View File

@@ -20,6 +20,7 @@ import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.demo.util.CustomEval;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* this is an example of fit generalized linear model in xgboost
@@ -39,8 +40,8 @@ public class GeneralizedLinearModel {
//you can also set lambda_bias which is L2 regularizer on the bias term
Params param = new Params() {
{
put("alpha", "0.0001");
put("silent", "1");
put("alpha", 0.0001);
put("silent", 1);
put("objective", "binary:logistic");
put("booster", "gblinear");
}
@@ -52,13 +53,14 @@ public class GeneralizedLinearModel {
//param.put("eta", "0.5");
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//train a booster
int round = 4;
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, round, watchs, null, null);
float[][] predicts = booster.predict(testMat);

View File

@@ -21,6 +21,7 @@ import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.demo.util.CustomEval;
import org.dmlc.xgboost4j.util.WatchList;
/**
* predict first ntree
@@ -35,20 +36,21 @@ public class PredictFirstNtree {
//specify parameters
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
put("objective", "binary:logistic");
}
};
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//train a booster
int round = 3;
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, round, watchs, null, null);
//predict use 1 tree
float[][] predicts1 = booster.predict(testMat, false, 1);

View File

@@ -20,6 +20,7 @@ import org.dmlc.xgboost4j.Booster;
import org.dmlc.xgboost4j.DMatrix;
import org.dmlc.xgboost4j.util.Params;
import org.dmlc.xgboost4j.util.Trainer;
import org.dmlc.xgboost4j.util.WatchList;
/**
* predict leaf indices
@@ -34,20 +35,21 @@ public class PredictLeafIndices {
//specify parameters
Params param = new Params() {
{
put("eta", "1.0");
put("max_depth", "2");
put("silent", "1");
put("eta", 1.0);
put("max_depth", 2);
put("silent", 1);
put("objective", "binary:logistic");
}
};
//specify evaluate datasets and evaluate names
DMatrix[] dmats = new DMatrix[] {trainMat, testMat};
String[] evalNames = new String[] {"train", "test"};
//specify watchList
WatchList watchs = new WatchList();
watchs.put("train", trainMat);
watchs.put("test", testMat);
//train a booster
int round = 3;
Booster booster = Trainer.train(param, trainMat, round, dmats, evalNames, null, null);
Booster booster = Trainer.train(param, trainMat, round, watchs, null, null);
//predict using first 2 tree
float[][] leafindex = booster.predict(testMat, 2, true);