我已经在Python中建立了XGBoost模型,以便根据各种变量预测零件的价格。该模型相当准确,准确率为91%。
如何通过手动输入不同于数据集的值来预测一个零件的价格?我想传递所有变量的值,然后预测该零件的价格。
private static String filepath = "...............";
public static void main(String[] args) {
String tmpFileName = "tmp.dat";
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(filepath));
bw = new BufferedWriter(new FileWriter(tmpFileName));
String line;
while ((line = br.readLine()) != null) {
line = workOnLine(line);
bw.write(line+"\n");
}
} catch (Exception e) {
return;
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
//
}
try {
if(bw != null)
bw.close();
} catch (IOException e) {
//
}
}
// Once everything is complete, delete old file..
File oldFile = new File(filepath);
oldFile.delete();
// And rename tmp file's name to old file name
File newFile = new File(tmpFileName);
newFile.renameTo(oldFile);
}
private static String workOnLine(String line) {
if (line.contains("Builder()")) {
int builderIndex = line.indexOf("Builder()");
String begin = line.substring(0, builderIndex);
if (begin.contains("new ")) {
int newIndex = begin.lastIndexOf("new ");
begin = begin.substring(0, newIndex) + begin.substring(newIndex + 4);
line = begin + ".builder" + line.substring(builderIndex + 7);
} else {
log.error("Builder with no new. Wtf ?");
}
}
if (line.contains(".with")) {
int withIndex = line.indexOf(".with");
String startChar = line.substring(withIndex + 5, withIndex + 6);
String replaceChar = startChar.toLowerCase();
line = line.substring(0, withIndex) + "." + replaceChar + line.substring(withIndex + 6);
}
return line;
}
这是我在R中所做的,但是我想在Python中做同样的事情。谢谢。