[jvm-packages] support specified feature names when getModelDump and getFeatureScore (#3733)
* [jvm-packages] support specified feature names for jvm when get ModelDump and get FeatureScore (#3725) * typo and style fix
This commit is contained in:
@@ -656,6 +656,56 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModel
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: ml_dmlc_xgboost4j_java_XGBoostJNI
|
||||
* Method: XGBoosterDumpModelExWithFeatures
|
||||
* Signature: (JLjava/lang/String;I[[Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelExWithFeatures
|
||||
(JNIEnv *jenv, jclass jcls, jlong jhandle, jobjectArray jfeature_names, jint jwith_stats,
|
||||
jstring jformat, jobjectArray jout) {
|
||||
|
||||
BoosterHandle handle = (BoosterHandle) jhandle;
|
||||
bst_ulong feature_num = (bst_ulong)jenv->GetArrayLength(jfeature_names);
|
||||
|
||||
std::vector<std::string> feature_names;
|
||||
std::vector<char*> feature_names_char;
|
||||
|
||||
std::string feature_type_q = "q";
|
||||
std::vector<char*> feature_types_char;
|
||||
|
||||
for (bst_ulong i = 0; i < feature_num; ++i) {
|
||||
jstring jfeature_name = (jstring)jenv->GetObjectArrayElement(jfeature_names, i);
|
||||
const char *s = jenv->GetStringUTFChars(jfeature_name, 0);
|
||||
feature_names.push_back(std::string(s, jenv->GetStringLength(jfeature_name)));
|
||||
if (s != nullptr) jenv->ReleaseStringUTFChars(jfeature_name, s);
|
||||
if (feature_names.back().length() == 0) feature_names.pop_back();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < feature_names.size(); ++i) {
|
||||
feature_names_char.push_back(&feature_names[i][0]);
|
||||
feature_types_char.push_back(&feature_type_q[0]);
|
||||
}
|
||||
|
||||
const char *format = jenv->GetStringUTFChars(jformat, 0);
|
||||
bst_ulong len = 0;
|
||||
char **result;
|
||||
|
||||
int ret = XGBoosterDumpModelExWithFeatures(handle, feature_num,
|
||||
(const char **) dmlc::BeginPtr(feature_names_char),
|
||||
(const char **) dmlc::BeginPtr(feature_types_char),
|
||||
jwith_stats, format, &len, (const char ***) &result);
|
||||
|
||||
jsize jlen = (jsize) len;
|
||||
jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF(""));
|
||||
for(int i=0 ; i<jlen; i++) {
|
||||
jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[i]));
|
||||
}
|
||||
jenv->SetObjectArrayElement(jout, 0, jinfos);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: ml_dmlc_xgboost4j_java_XGBoostJNI
|
||||
* Method: XGBoosterLoadRabitCheckpoint
|
||||
|
||||
Reference in New Issue
Block a user