package utils;
|
|
import java.util.HashMap;
|
|
|
/**
|
* @Auther: Forrest
|
* @Date: 2020/6/8 14:05
|
* @Description:
|
*/
|
public class AlgsUtils {
|
private HashMap<String, Object> model = new HashMap<String, Object>();
|
|
public HashMap<String, Object> createPredictHashmap(HashMap<String, Object> models) {
|
if ((models.containsKey("iail/mdk/model"))) {
|
if (((String) ((HashMap) models.get("iail/mdk/model")).get("param1")).isEmpty()) {
|
String aaa = "error";
|
model.put("param1", aaa);
|
} else {
|
String model_train = (String) ((HashMap) models.get("iail/mdk/model")).get("param1");
|
model.put("param1", model_train);
|
}
|
} else {
|
model = models;
|
}
|
return model;
|
}
|
|
public HashMap<String, Object> createPredictHashmapplus(HashMap<String, Object> models) {
|
if (models != null && models.containsKey("models")) {
|
if (((String) ((HashMap) models.get("models")).get("paramFile")).isEmpty()) {
|
String aaa = "error";
|
model.put("param1", aaa);
|
} else {
|
String model_train = (String) ((HashMap) models.get("models")).get("paramFile");
|
model.put("paramFile", model_train);
|
if (((HashMap) models.get("models")).containsKey("dim")) {
|
Object dim = ((HashMap) models.get("models")).get("dim");
|
model.put("dim", dim);
|
}
|
}
|
} else {
|
model = models;
|
}
|
return model;
|
}
|
|
private HashMap<String, Object> eval_pre = new HashMap<String, Object>();
|
|
/**
|
* 对返回码进行转换
|
*
|
* @param models
|
* @return
|
*/
|
public int reverseResultCode(HashMap<String, Object> models) {
|
if ((models.containsKey("result_code"))) {
|
return Integer.parseInt((String) models.get("result_code"));
|
}
|
return -2;
|
}
|
|
/**
|
* 对评价指标进行转换
|
*
|
* @param models
|
* @return
|
*/
|
public HashMap<String, Object> reverseEval(HashMap<String, Object> models) {
|
if ((models.containsKey("eval"))) {
|
if (((HashMap) models.get("eval")).containsKey("MAE")) {
|
double MAE = Double.parseDouble((String) ((HashMap) models.get("eval")).get("MAE"));
|
eval_pre.put("MAE", MAE);
|
}
|
if (((HashMap) models.get("eval")).containsKey("MAPE")) {
|
double MAPE = Double.parseDouble((String) ((HashMap) models.get("eval")).get("MAPE"));
|
eval_pre.put("MAPE", MAPE);
|
}
|
if (((HashMap) models.get("eval")).containsKey("RMSE")) {
|
double MAE = Double.parseDouble((String) ((HashMap) models.get("eval")).get("RMSE"));
|
eval_pre.put("RMSE", MAE);
|
}
|
}
|
|
return eval_pre;
|
}
|
|
/**
|
* 对models里面的参数进行转换
|
*/
|
private HashMap<String, Object> train_result_models = new HashMap<String, Object>();
|
|
public HashMap<String, Object> reverseModels(HashMap<String, Object> train_result) {
|
if (train_result.containsKey("models")) {
|
train_result_models = (HashMap) train_result.get("models");
|
if (((HashMap) train_result.get("models")).containsKey("dim")) {
|
double dim = Double.parseDouble((String) ((HashMap) train_result.get("models")).get("dim"));
|
train_result_models.put("dim", dim);
|
}
|
train_result.put("models", train_result_models);
|
}
|
return train_result;
|
}
|
|
|
/**
|
* 获取二维数组行列数
|
*
|
* @param arr
|
* @return
|
*/
|
public int[] getColAndRow(double[][] arr) {
|
int row = arr.length;
|
int col = arr[0].length;
|
int[] result = new int[2];
|
result[0] = row;
|
result[1] = col;
|
return result;
|
}
|
|
/**
|
* 两个二维数组进行合并
|
*
|
* @param data
|
* @param refs
|
* @return
|
*/
|
public double[][] getMathergeArr(double[][] data, double[][] refs) {
|
|
int[] dataRowAndCol = getColAndRow(data);
|
int rowData = dataRowAndCol[0];
|
int colData = dataRowAndCol[1];
|
|
int[] refsRowAndCol = getColAndRow(refs);
|
int rowrefs = refsRowAndCol[0];
|
int colrefs = refsRowAndCol[1];
|
|
double[][] newData = new double[rowData + rowrefs][colData];
|
for (int i = 0; i < rowData; i++) {
|
for (int j = 0; j < colData; j++) {
|
newData[i][j] = data[i][j];
|
}
|
}
|
|
for (int i = 0; i < rowrefs; i++) {
|
for (int j = 0; j < colrefs; j++) {
|
newData[i + rowData][j] = refs[i][j];
|
}
|
}
|
return newData;
|
}
|
|
/**
|
* 对训练方法进行处理,实现评价指标的转换
|
*/
|
public HashMap<String, Object> trainUtil(HashMap<String, Object> train_result, HashMap<String, Object> eval, String time) {
|
if (train_result.containsKey("eval")) {
|
eval = (HashMap<String, Object>) train_result.get("eval");
|
eval.put("time", time);
|
train_result.put("eval", eval);
|
}
|
train_result.put("result_code", reverseResultCode(train_result));
|
return train_result;
|
}
|
|
/**
|
* 对预测方法进行处理
|
*/
|
// public HashMap<String,Object> predictUtil(){
|
//
|
// }
|
}
|