X_all = np.matrix(np.zeros([d, 2])) X_all[:, 0] = np.matrix(x0[:, i]).T X_all[:, 1] = np.matrix(x1[:, i]).T Y_all[:, i] = clf.predict(X_all) plt.contour(np.array(x0), np.array(x1), np.array(Y_all), levels=[0.0], colors='red') if __name__ == '__main__': # set rbf_kernel.gamma = 0.1 X_1, Y_1 = read_dense_data(open('data/sample_data_1.txt')) X_2, Y_2 = read_dense_data(open('data/sample_data_2.txt')) X_3, Y_3 = read_dense_data(open('data/sample_data_3.txt')) X_1 = np.matrix(X_1) X_2 = np.matrix(X_2) X_3 = np.matrix(X_3) Y_1 = np.matrix(map_label(Y_1)).T Y_2 = np.matrix(map_label(Y_2)).T Y_3 = np.matrix(map_label(Y_3)).T Y_1[np.where(Y_1 == 0)] = -1 Y_2[np.where(Y_2 == 0)] = -1 Y_3[np.where(Y_3 == 0)] = -1
[x0, x1] = plt.meshgrid(x0_plot, x1_plot); Y_all = np.matrix(np.zeros([d, d])) for i in range(d): X_all = np.matrix(np.zeros([d, 2])) X_all[:, 0] = np.matrix(x0[:, i]).T X_all[:, 1] = np.matrix(x1[:, i]).T Y_all[:, i] = clf.predict(X_all) plt.contour(np.array(x0), np.array(x1), np.array(Y_all), levels = [0.0], colors = 'red') if __name__ == '__main__': # set rbf_kernel.gamma = 0.1 X_1, Y_1 = read_dense_data(open('data/sample_data_1.txt')) X_2, Y_2 = read_dense_data(open('data/sample_data_2.txt')) X_3, Y_3 = read_dense_data(open('data/sample_data_3.txt')) X_1 = np.matrix(X_1) X_2 = np.matrix(X_2) X_3 = np.matrix(X_3) Y_1 = np.matrix(map_label(Y_1)).T Y_2 = np.matrix(map_label(Y_2)).T Y_3 = np.matrix(map_label(Y_3)).T Y_1[np.where(Y_1 == 0)] = -1 Y_2[np.where(Y_2 == 0)] = -1 Y_3[np.where(Y_3 == 0)] = -1
m = len(X) D = (X * w - Y) fx = float((1.0 / (2 * m)) * (D.T * D) + (lamb / 2.0) * (w.T * w)) df = (1.0 / m) * X.T * (X * w - Y) + 1.0 * lamb * w self.c += 1 return fx, df if __name__ == '__main__': train_path = 'data/housing.train' test_path = 'data/housing.test' X_train, Y_train = read_dense_data(open(train_path)) X_test, Y_test = read_dense_data(open(test_path)) X_train = np.matrix(X_train) Y_train = [float(y) for y in Y_train] Y_train = np.matrix(Y_train).T X_test = np.matrix(X_test) Y_test = [float(y) for y in Y_test] Y_test = np.matrix(Y_test).T reg = LinearRegression() reg.train(X_train, Y_train) reg.test(X_train, Y_train) reg.test(X_test, Y_test)
err = P.sum() if update % 100 == 0: print >>sys.stderr, "Update : %d\ttraining loss: %lf" % (update, 1.0 * err / len(Y)) if err < min_err: min_err = err self.w = w_test if __name__ == "__main__": train_path = "data/heart_scale.train" test_path = "data/heart_scale.test" X_train, Y_train = read_dense_data(open(train_path)) X_test, Y_test = read_dense_data(open(test_path)) X_train = np.matrix(X_train) Y_train = [int(y) for y in Y_train] Y_train = np.matrix(Y_train).T X_test = np.matrix(X_test) Y_test = [int(y) for y in Y_test] Y_test = np.matrix(Y_test).T clf = Perceptron() clf.train(X_train, Y_train) acc_train = clf.test(X_train, Y_train) acc_test = clf.test(X_test, Y_test)