def post(self): try: jsonByte = self.request.body jsonStr = jsonByte.decode("utf-8") svmVo = json.loads(jsonStr, object_hook=svm.dictToSvmVo) file_name = svmVo.trainDataPath model = svmVo.modelPath # 1、导入训练数据 dataSet, labels = load_data_libsvm(file_name) # 2、训练SVM模型 C = 0.6 toler = 0.001 maxIter = 500 svm_model = svm.SVM_training(dataSet, labels, C, toler, maxIter) # 3、计算训练的准确性 svm.cal_accuracy(svm_model, dataSet, labels) # 4、保存最终的SVM模型 svm.save_svm_model(svm_model, model) commonResponse = com.CommonResponse(com.successCode, com.successMsg, dict([("obj", 1)])) except BaseException as e: print("执行支持向量机-生成模型失败:{}".format(e)) commonResponse = com.CommonResponse(com.errorCode, "{}".format(e), dict([("obj", 1)])) finally: # 返回数据 self.write(commonResponse.__dict__)
while (int(li[0]) - 1 > index): tmp.append(0) index += 1 # 前面填充为0,之后,现在的这个索引所指向的位置,继续写上它对应的value tmp.append(float(li[1])) index += 1 while len(tmp) < 13: tmp.append(0) data.append(tmp) f.close() return np.mat(data), np.mat(label).T if __name__ == "__main__": # 1、导入训练数据 print("------------ 1、load data --------------") dataSet, labels = load_data_libsvm("heart_scale") # 2、训练SVM模型 print("------------ 2、training ---------------") C = 0.6 toler = 0.001 maxIter = 500 svm_model = svm.SVM_training(dataSet, labels, C, toler, maxIter) # 3、计算训练的准确性 print("------------ 3、cal accuracy --------------") accuracy = svm.cal_accuracy(svm_model, dataSet, labels) print("The training accuracy is: %.3f%%" % (accuracy * 100)) # 4、保存最终的SVM模型 print("------------ 4、save model ----------------") svm.save_svm_model(svm_model, "model_file")