def trainSVM(): svm = ml.SVM_create() svm.setType(ml.SVM_C_SVC) svm.setKernel(ml.SVM_RBF) td = ml.TrainData_create(trainData, ml.ROW_SAMPLE, np.array(trainLabels)) # svm.train(td) svm.trainAuto(trainData, ml.ROW_SAMPLE, np.array(trainLabels)) svm.save("../svm_data.yaml") return svm
def __init__(self, type=ml.SVM_C_SVC, kernel=ml.SVM_LINEAR, c=2., gamma=1., nu=0.5, degree=1): self.model = ml.SVM_create() self.model.setType(type) self.model.setKernel(kernel) self.model.setC(c) self.model.setGamma(gamma) self.model.setNu(nu) self.model.setDegree(degree)
print(len(classes[classes == 'Class 4'])) print(len(classes[classes == 'Class 5'])) print(len(classes[classes == 'Class 6'])) print(len(classes[classes == 'Class 7'])) for x, y in enumerate(base): oDataSet.add_sample_of_attribute( np.array(list(np.float32(y)) + [classes[x]])) oDataSet.attributes = oDataSet.attributes.astype(float) oDataSet.normalize_data_set() for j in range(50): print(j) oData = Data(7, 11, samples=47) oData.random_training_test_by_percent([352, 382, 378, 382, 376, 360, 361], 0.8) svm = ml.SVM_create() svm.setKernel(ml.SVM_RBF) oData.params = dict(kernel=ml.SVM_RBF, kFold=10) svm.trainAuto(np.float32(oDataSet.attributes[oData.Training_indexes]), ml.ROW_SAMPLE, np.int32(oDataSet.labels[oData.Training_indexes]), kFold=10) # svm.train_auto(np.float32(oDataSet.attributes[oData.Training_indexes]), # np.float32(oDataSet.labels[oData.Training_indexes]), None, None, params=oData.params) results = [ ] # svm.predict_all(np.float32(oDataSet.attributes[oData.Testing_indexes])) for i in (oDataSet.attributes[oData.Testing_indexes]): res, cls = svm.predict(np.float32([i])) results.append(cls[0]) oData.set_results_from_classifier(results, oDataSet.labels[oData.Testing_indexes])