Created
October 10, 2016 14:25
-
-
Save sat0b/dd4c1d00c18cf5ad657a01ef153e5c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.preprocessing import OneHotEncoder | |
| from sklearn.cross_validation import train_test_split | |
| np.random.seed(3) | |
| center0 = 3 * np.random.randint(-2, 2, 2) | |
| center1 = 3 * np.random.randint(-2, 2, 2) | |
| n_samples = 3000 | |
| X = np.zeros((n_samples, 3)) | |
| X[:, 0] = np.tile(1, n_samples) | |
| X[:n_samples//2, 1:] = center0 + 1.5 * np.random.randn(n_samples//2, 2) | |
| X[n_samples//2:, 1:] = center1 + 1.5 * np.random.randn(n_samples//2, 2) | |
| t = [0] * (n_samples//2) + [1] * (n_samples//2) | |
| t = np.array(t)[:, np.newaxis] | |
| enc = OneHotEncoder() | |
| T = enc.fit_transform(t).toarray() | |
| X_train, X_test, T_train, T_test = train_test_split(X, T) | |
| def linear_weight(X, T): | |
| return np.linalg.solve(np.dot(X.T, X), np.dot(X.T, T)) | |
| W_mse = linear_weight(X_train, T_train) | |
| Y_test = np.dot(X_test, W_mse) | |
| # accuracy | |
| acc = np.mean(np.argmax(Y_test, axis=1) == np.argmax(T_test, axis=1)) | |
| print(acc) | |
| # plot | |
| label0 = T_test[:, 0] == 1 | |
| label1 = T_test[:, 1] == 1 | |
| plt.scatter(X_test[label0, 1], X_test[label0, 2], c='b') | |
| plt.scatter(X_test[label1, 1], X_test[label1, 2], c='r') | |
| # y = w0 + w1 x | |
| w0 = (W_mse[0, 1] - W_mse[0, 0]) / (W_mse[2, 0] - W_mse[2, 1]) | |
| w1 = (W_mse[1, 1] - W_mse[1, 0]) / (W_mse[2, 0] - W_mse[2, 1]) | |
| x = np.linspace(X_test[:, 1].min(), X_test[:, 1].max(), 1000) | |
| plt.plot(x, w0+w1*x) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment