def svm2(): train_data = np.load('hw3_data/nonlinsep/train_data.npy') train_labels = np.load('hw3_data/nonlinsep/train_labels.npy') k_list = ['linear', 'rbf', 'poly', 'sigmoid'] for k in k_list: clf = svm.SVC(kernel=k) clf.fit(train_data, train_labels) draw_svm(clf, train_data, train_labels, -3, 3, -3, 3, 'svm1_' + k)
def svm1(): train_data = np.load('hw3_data/linsep/train_data.npy') train_labels = np.load('hw3_data/linsep/train_labels.npy') c_list = [0.01, 0.1, 1, 10, 100] for i in range(len(c_list)): clf = svm.SVC(kernel='linear', C=c_list[i]) clf.fit(train_data, train_labels) draw_svm(clf, train_data, train_labels, -3, 3, -3, 3, 'svm1_' + str(i))
def svm_part_1(c: float, data: np.array, labels: np.array, save: bool = True): model = SVC(C=c, kernel='linear') model.fit(data, labels) x_values = [x[0] for x in data] y_values = [x[1] for x in data] min_x, min_y = min(x_values), min(y_values) max_x, max_y = max(x_values), max(y_values) if save is True: draw_svm(model, data, labels, min_x, max_x, min_y, max_y, f"plots/svm/q1-{c}.png") else: draw_svm(model, data, labels, min_x, max_x, min_y, max_y)
def task2(): kernel = sys.argv[2] train_data = np.load('hw3_data/nonlinsep/train_data.npy') train_labels = np.load('hw3_data/nonlinsep/train_labels.npy') cf = SVC(kernel=kernel) cf.fit(train_data, train_labels) x_min, x_max, y_min, y_max = min(train_data[:, 0]), max( train_data[:, 0]), min(train_data[:, 1]), max(train_data[:, 1]) draw.draw_svm(cf, train_data, train_labels, x_min, x_max, y_min, y_max)
def task1(): c = float(sys.argv[2]) train_data = np.load('hw3_data/linsep/train_data.npy') train_labels = np.load('hw3_data/linsep/train_labels.npy') cf = SVC(c, kernel='linear') cf.fit(train_data, train_labels) x_min, x_max, y_min, y_max = min(train_data[:, 0]), max( train_data[:, 0]), min(train_data[:, 1]), max(train_data[:, 1]) draw.draw_svm(cf, train_data, train_labels, x_min, x_max, y_min, y_max)
def svm_part_2(kernel: str, data: np.array, labels: np.array, save: bool = True): model = SVC(C=1, kernel=kernel) model.fit(data, labels) x_values = [x[0] for x in data] y_values = [x[1] for x in data] min_x, min_y = min(x_values), min(y_values) max_x, max_y = max(x_values), max(y_values) if save is True: draw_svm(model, data, labels, min_x, max_x, min_y, max_y, f"plots/svm/q2-{kernel}.png") else: draw_svm(model, data, labels, min_x, max_x, min_y, max_y)
def part1(): train_data = np.load('hw3_data/linsep/train_data.npy') train_labels = np.load('hw3_data/linsep/train_labels.npy') c_values = [0.01, 0.1, 1, 10, 100] for c in c_values: clf = SVC(C=c, kernel='linear') clf.fit(train_data, train_labels) draw_svm(clf, train_data, train_labels, -3, 3, -3, 3, target='part1_' + str(c) + ".png")
def part2(): train_data = np.load('hw3_data/nonlinsep/train_data.npy') train_labels = np.load('hw3_data/nonlinsep/train_labels.npy') kernels = ["linear", "rbf", "poly", "sigmoid"] for k in kernels: clf = SVC(C=1, kernel=k) clf.fit(train_data, train_labels) draw_svm(clf, train_data, train_labels, -3, 3, -3, 3, target='part2_' + str(k) + ".png")