result_file(string):存储最终预测结果的文件名 ''' m = len(prediction) n = len(data_test[0]) f_result = open(result_file, "w") for i in xrange(m): tmp = [] for j in xrange(n - 1): tmp.append(str(data_test[i][j])) tmp.append(str(prediction[i])) f_result.writelines("\t".join(tmp) + "\n") f_result.close() if __name__ == "__main__": # 1、导入测试数据集 print "--------- 1、load test data --------" data_test = load_data("test_data.txt") # 2、导入随机森林模型 print "--------- 2、load random forest model ----------" trees_result, trees_feature = load_model("result_file", "feature_file") # 3、预测 print "--------- 3、get prediction -----------" prediction = get_predict(trees_result, trees_feature, data_test) # 4、保存最终的预测结果 print "--------- 4、save result -----------" save_result(data_test, prediction, "final_result")
def main(): data_test = load_data('test_data.txt') trees_result, trees_feature = load_model('result_file', 'feature_file') prediction = get_predict(trees_result, trees_feature, data_test) print(prediction) save_result(data_test, prediction, "final_result")
result_file(string):存储最终预测结果的文件名 ''' m = len(prediction) n = len(data_test[0]) f_result = open(result_file, "w") for i in xrange(m): tmp = [] for j in xrange(n -1): tmp.append(str(data_test[i][j])) tmp.append(str(prediction[i])) f_result.writelines("\t".join(tmp) + "\n") f_result.close() if __name__ == "__main__": # 1、导入测试数据集 print "--------- 1、load test data --------" data_test = load_data("test_data.txt") # 2、导入随机森林模型 print "--------- 2、load random forest model ----------" trees_result, trees_feature = load_model("result_file", "feature_file") # 3、预测 print "--------- 3、get prediction -----------" prediction = get_predict(trees_result, trees_feature, data_test) # 4、保存最终的预测结果 print "--------- 4、save result -----------" save_result(data_test, prediction, "final_result")