import numpy as np import datetime import random from libsvm.svmutil import * import matplotlib.pyplot as plt FILENAME = "satimage.scale" TEST_FILENAME = "satimage.scale.t" TARGET = 1 def new_label(y, target): ans = [] for i in y: if i == target: ans.append(1) else: ans.append(0) return ans def error(predict, gt): error_count = 0 for index in range(len(predict)): if predict[index] != gt[index]: error_count += 1 return error_count / len(predict) if __name__ == '__main__': y, x = svm_read_problem(FILENAME) y = new_label(y, TARGET) test_y, test_x = svm_read_problem(TEST_FILENAME) test_y = new_label(test_y, TARGET) for c in [0.01, 0.1, 1, 10, 100]: print("C=", c) prob = svm_problem(y, x) param = svm_parameter('-s 0 -t 2 -g 1 -c {} -q'.format(c)) m = svm_train(prob, param) p_label, p_acc, p_val = svm_predict(test_y, test_x, m) my_error = error(p_label, test_y) print("p_acc:", p_acc) print("0/1 error:", my_error) print("="*20)