Merge pull request #967 from CodingCat/master

[jvm-packages] change the API name
This commit is contained in:
Nan Zhu 2016-03-11 10:59:16 -05:00
commit acdd23e789
7 changed files with 11 additions and 9 deletions

View File

@ -25,6 +25,7 @@ object DistTrainWithFlink {
// read trainining data // read trainining data
val trainData = val trainData =
MLUtils.readLibSVM(env, "/path/to/data/agaricus.txt.train") MLUtils.readLibSVM(env, "/path/to/data/agaricus.txt.train")
val testData = MLUtils.readLibSVM(env, "/path/to/data/agaricus.txt.test")
// define parameters // define parameters
val paramMap = List( val paramMap = List(
"eta" -> 0.1, "eta" -> 0.1,
@ -34,7 +35,7 @@ object DistTrainWithFlink {
val round = 2 val round = 2
// train the model // train the model
val model = XGBoost.train(paramMap, trainData, round) val model = XGBoost.train(paramMap, trainData, round)
val predTrain = model.predict(trainData.map{x => x.vector}) val predTest = model.predict(testData.map{x => x.vector})
model.saveModelToHadoop("file:///path/to/xgboost.model") model.saveModelAsHadoopFile("file:///path/to/xgboost.model")
} }
} }

View File

@ -40,6 +40,6 @@ object DistTrainWithSpark {
"objective" -> "binary:logistic").toMap "objective" -> "binary:logistic").toMap
val xgboostModel = XGBoost.train(trainRDD, paramMap, numRound) val xgboostModel = XGBoost.train(trainRDD, paramMap, numRound)
// save model to HDFS path // save model to HDFS path
xgboostModel.saveModelToHadoop(outputModelPath) xgboostModel.saveModelAsHadoopFile(outputModelPath)
} }
} }

View File

@ -70,7 +70,7 @@ object XGBoost {
* @param modelPath The path that is accessible by hadoop filesystem API. * @param modelPath The path that is accessible by hadoop filesystem API.
* @return The loaded model * @return The loaded model
*/ */
def loadModelFromHadoop(modelPath: String) : XGBoostModel = { def loadModelFromHadoopFile(modelPath: String) : XGBoostModel = {
new XGBoostModel( new XGBoostModel(
XGBoostScala.loadModel( XGBoostScala.loadModel(
FileSystem FileSystem

View File

@ -31,7 +31,7 @@ class XGBoostModel (booster: Booster) extends Serializable {
* *
* @param modelPath The model path as in Hadoop path. * @param modelPath The model path as in Hadoop path.
*/ */
def saveModelToHadoop(modelPath: String): Unit = { def saveModelAsHadoopFile(modelPath: String): Unit = {
booster.saveModel(FileSystem booster.saveModel(FileSystem
.get(new Configuration) .get(new Configuration)
.create(new Path(modelPath))) .create(new Path(modelPath)))

View File

@ -128,7 +128,8 @@ object XGBoost extends Serializable {
* @param modelPath The path of the file representing the model * @param modelPath The path of the file representing the model
* @return The loaded model * @return The loaded model
*/ */
def loadModelFromHadoop(modelPath: String)(implicit sparkContext: SparkContext): XGBoostModel = { def loadModelFromHadoopFile(modelPath: String)(implicit sparkContext: SparkContext):
XGBoostModel = {
val path = new Path(modelPath) val path = new Path(modelPath)
val dataInStream = path.getFileSystem(sparkContext.hadoopConfiguration).open(path) val dataInStream = path.getFileSystem(sparkContext.hadoopConfiguration).open(path)
val xgBoostModel = new XGBoostModel(SXGBoost.loadModel(dataInStream)) val xgBoostModel = new XGBoostModel(SXGBoost.loadModel(dataInStream))

View File

@ -49,7 +49,7 @@ class XGBoostModel(booster: Booster)(implicit val sc: SparkContext) extends Seri
* *
* @param modelPath The model path as in Hadoop path. * @param modelPath The model path as in Hadoop path.
*/ */
def saveModelToHadoop(modelPath: String): Unit = { def saveModelAsHadoopFile(modelPath: String): Unit = {
val path = new Path(modelPath) val path = new Path(modelPath)
val outputStream = path.getFileSystem(sc.hadoopConfiguration).create(path) val outputStream = path.getFileSystem(sc.hadoopConfiguration).create(path)
booster.saveModel(outputStream) booster.saveModel(outputStream)

View File

@ -150,8 +150,8 @@ class XGBoostSuite extends FunSuite with BeforeAndAfter {
"objective" -> "binary:logistic").toMap "objective" -> "binary:logistic").toMap
val xgBoostModel = XGBoost.train(trainingRDD, paramMap, 5) val xgBoostModel = XGBoost.train(trainingRDD, paramMap, 5)
assert(eval.eval(xgBoostModel.predict(testSetDMatrix), testSetDMatrix) < 0.1) assert(eval.eval(xgBoostModel.predict(testSetDMatrix), testSetDMatrix) < 0.1)
xgBoostModel.saveModelToHadoop(tempFile.toFile.getAbsolutePath) xgBoostModel.saveModelAsHadoopFile(tempFile.toFile.getAbsolutePath)
val loadedXGBooostModel = XGBoost.loadModelFromHadoop(tempFile.toFile.getAbsolutePath) val loadedXGBooostModel = XGBoost.loadModelFromHadoopFile(tempFile.toFile.getAbsolutePath)
val predicts = loadedXGBooostModel.predict(testSetDMatrix) val predicts = loadedXGBooostModel.predict(testSetDMatrix)
assert(eval.eval(predicts, testSetDMatrix) < 0.1) assert(eval.eval(predicts, testSetDMatrix) < 0.1)
} }