Created
November 28, 2017 23:24
-
-
Save comtom/2e5636f5595e959405054cf5610e5918 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 matplotlib.pyplot as plt | |
| from sklearn.datasets.base import load_data | |
| from sklearn.utils import Bunch | |
| from sklearn.decomposition import PCA | |
| from sklearn.discriminant_analysis import LinearDiscriminantAnalysis | |
| def load_netflix(return_X_y=False): | |
| data, target, target_names = load_data('/home/comtom/extra/netflix/', 'file1.csv') | |
| if return_X_y: | |
| return data, target | |
| return Bunch(data=data, target=target, | |
| target_names=[1, 2, 3, 4, 5], | |
| DESCR='', | |
| feature_names=['movieId', 'userID', 'ratingDate']) | |
| netflix = load_netflix() | |
| X = netflix.data | |
| y = netflix.target | |
| target_names = netflix.target_names | |
| pca = PCA(n_components=2) | |
| X_r = pca.fit(X).transform(X) | |
| lda = LinearDiscriminantAnalysis(n_components=2) | |
| X_r2 = lda.fit(X, y).transform(X) | |
| print('Explained variance ratio (primeros dos componentes): %s' % str(pca.explained_variance_ratio_)) | |
| plt.figure() | |
| colors = ['navy', 'turquoise', 'darkorange', 'red', 'yellow'] | |
| lw = 1 | |
| for color, i, target_name in zip(colors, [1, 2, 3, 4, 5], target_names): | |
| plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.4, lw=lw, | |
| label=target_name) | |
| plt.legend(loc='best', shadow=False, scatterpoints=1) | |
| plt.title('PCA del dataset de netflix') | |
| plt.figure() | |
| for color, i, target_name in zip(colors, [1, 2, 3, 4, 5], target_names): | |
| plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.4, color=color, | |
| label=target_name) | |
| plt.legend(loc='best', shadow=False, scatterpoints=1) | |
| plt.title('LDA del dataset de netflix') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment