|
| 1 | +from sklearn.decomposition import SparsePCA |
1 | 2 | import numpy as np
|
2 |
| -import matplotlib.pyplot as plt |
3 |
| -import pandas as pd |
4 |
| -from sklearn import datasets |
5 |
| -from sklearn.decomposition import (PCA, IncrementalPCA, KernelPCA, TruncatedSVD, FastICA, MiniBatchDictionaryLearning, SparsePCA) |
6 |
| -from sklearn.manifold import (Isomap, |
7 |
| -LocallyLinearEmbedding) |
8 |
| -from sklearn.discriminant_analysis import LinearDiscriminantAnalysis |
9 |
| -from sklearn.random_projection import (GaussianRandomProjection, |
10 |
| -SparseRandomProjection) |
11 |
| -from sklearn.neighbors import (KNeighborsClassifier, |
12 |
| -NeighborhoodComponentsAnalysis) |
13 |
| -from sklearn.pipeline import make_pipeline |
14 |
| -from sklearn.preprocessing import StandardScaler |
15 |
| -from sklearn.model_selection import train_test_split |
16 |
| -%matplotlib inline |
17 |
| -# Load Digits dataset |
18 |
| -digits = datasets.load_digits() |
19 |
| -X, y = digits.data, digits.target |
20 |
| -# Parameters |
21 |
| -dim = len(X[0]) |
22 |
| -n_classes = len(np.unique(y)) |
23 |
| -n_neighbors = 3 |
24 |
| -random_state = 0 |
25 |
| -# Split into train/test |
26 |
| -X_train, X_test, y_train, y_test = \ |
27 |
| -train_test_split(X, y, test_size=0.5, stratify=y, |
28 |
| -random_state=random_state) |
29 | 3 |
|
| 4 | +# Generate example data |
| 5 | +X = np.array([[1, 2, 3], |
| 6 | + [4, 5, 6], |
| 7 | + [7, 8, 9]]) |
30 | 8 |
|
31 |
| -pca = make_pipeline(StandardScaler(), |
32 |
| -PCA(n_components=2, |
33 |
| -random_state=random_state)) |
| 9 | +# Perform Sparse PCA |
| 10 | +spca = SparsePCA(n_components=2, alpha=1) |
| 11 | +X_spca = spca.fit_transform(X) |
34 | 12 |
|
| 13 | +# Components |
| 14 | +print("Components:", spca.components_) |
35 | 15 |
|
36 |
| -inc_pca = make_pipeline(StandardScaler(), |
37 |
| -IncrementalPCA(n_components=2)) |
| 16 | +# Transformed data |
| 17 | +print("Transformed data:", X_spca) |
38 | 18 |
|
39 | 19 |
|
40 |
| -# kernel : “linear” | “poly” | “rbf” | “sigmoid” | “cosine” | “precomputed” |
41 |
| -kpca = make_pipeline(StandardScaler(), |
42 |
| -KernelPCA(kernel="cosine", |
43 |
| -n_components=2, |
44 |
| -gamma=None, |
45 |
| -fit_inverse_transform=True, |
46 |
| -random_state=random_state, |
47 |
| -n_jobs=1)) |
| 20 | +X_spca.shape, X.shape |
48 | 21 |
|
49 | 22 |
|
50 |
| -sparsepca = make_pipeline(StandardScaler(), |
51 |
| -SparsePCA(n_components=2, |
52 |
| -alpha=0.0001, |
53 |
| -random_state=random_state, |
54 |
| -n_jobs=-1)) |
55 |
| - |
56 |
| - |
57 |
| -SVD = make_pipeline(StandardScaler(), |
58 |
| -TruncatedSVD(n_components=2, |
59 |
| -algorithm='randomized', |
60 |
| -random_state=random_state, |
61 |
| -n_iter=5)) |
62 |
| - |
63 |
| - |
64 |
| -GRP = make_pipeline(StandardScaler(), |
65 |
| -GaussianRandomProjection(n_components=2, |
66 |
| -eps = 0.5, |
67 |
| -random_state=random_state)) |
68 |
| - |
69 |
| - |
70 |
| -lda = make_pipeline(StandardScaler(), |
71 |
| -LinearDiscriminantAnalysis(n_components=2)) |
72 |
| - |
73 |
| - |
74 |
| -nca = make_pipeline(StandardScaler(), |
75 |
| -NeighborhoodComponentsAnalysis(n_components=2, |
76 |
| -random_state=random_state)) |
77 |
| - |
78 |
| - |
79 |
| -SRP = make_pipeline(StandardScaler(), |
80 |
| -SparseRandomProjection(n_components=2, |
81 |
| -density = 'auto', |
82 |
| -eps = 0.5, |
83 |
| -random_state=random_state, |
84 |
| -dense_output = False)) |
85 |
| - |
86 |
| - |
87 |
| -isomap = make_pipeline(StandardScaler(), |
88 |
| -Isomap(n_components=2, |
89 |
| -n_jobs = 4, |
90 |
| -n_neighbors = 5)) |
91 |
| - |
92 |
| - |
93 |
| -miniBatchDictLearning = make_pipeline(StandardScaler(), |
94 |
| -MiniBatchDictionaryLearning(n_components=2, |
95 |
| -batch_size = 200, |
96 |
| -alpha = 1, |
97 |
| -n_iter = 25, |
98 |
| -random_state=random_state)) |
99 |
| - |
100 |
| - |
101 |
| -FastICA = make_pipeline(StandardScaler(), |
102 |
| -FastICA(n_components=2, |
103 |
| -algorithm = 'parallel', |
104 |
| -whiten = True, |
105 |
| -max_iter = 100, |
106 |
| -random_state=random_state)) |
107 |
| - |
108 |
| - |
109 |
| -lle = make_pipeline(StandardScaler(), |
110 |
| -LocallyLinearEmbedding(n_components=2, |
111 |
| -n_neighbors = 10, |
112 |
| -method = 'modified', |
113 |
| -n_jobs = 4, |
114 |
| -random_state=random_state)) |
115 |
| - |
116 |
| - |
117 |
| -knn = KNeighborsClassifier(n_neighbors=n_neighbors) |
118 |
| - |
119 |
| - |
120 |
| -import warnings |
121 |
| -warnings.filterwarnings('ignore') |
122 |
| -# Make a list of the methods to be compared |
123 |
| -dim_reduction_methods = {'PCA': pca, |
124 |
| -'LDA': lda, |
125 |
| -'NCA': nca, |
126 |
| -'INC PCA': inc_pca, |
127 |
| -'KPCA':kpca, |
128 |
| -'Sparced PCA': sparsepca, |
129 |
| -'SVD': SVD, |
130 |
| -'GRP' : GRP, |
131 |
| -'SRP': SRP, |
132 |
| -'IsoMap': isomap, |
133 |
| -'MBD': miniBatchDictLearning, |
134 |
| -'ICA': FastICA, |
135 |
| -'LLE': lle} |
136 |
| -plt.figure(figsize=(24, 36)) |
137 |
| -for j,(name, model) in enumerate(dim_reduction_methods.items()): |
138 |
| - plt.subplot(5, 3, j + 1, aspect='auto') |
139 |
| - # Fit the method's model |
140 |
| - model.fit(X_train, y_train) |
141 |
| - # Fit a nearest neighbor classifier on the embedded training set |
142 |
| - knn.fit(model.transform(X_train), y_train) |
143 |
| - # Compute the nearest neighbor accuracy on the embedded test set |
144 |
| - acc_knn = knn.score(model.transform(X_test), y_test) |
145 |
| - # Fit the methods using the fitted model |
146 |
| - X_embedded = model.transform(X) |
147 |
| - # Creating a dataframe to easily plot the sample label |
148 |
| - df = pd.DataFrame(np.concatenate((X_embedded, np.reshape(y, (-1, 1))), axis=1)) |
149 |
| - # Plot the projected points and show the evaluation score |
150 |
| - plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=y, s=20, cmap='Set1') |
151 |
| - plt.title("{}, KNN (k={})\nTest accuracy = {:.2f}".format(name, |
152 |
| - n_neighbors, |
153 |
| - acc_knn)) |
154 |
| - plt.colorbar() |
155 |
| -plt.show() |
156 |
| -# for i, number in enumerate(y_test): |
157 |
| -# plt.annotate(number, |
158 |
| -# df.loc[df[2]==number,[0,1]].mean(), |
159 |
| -# horizontalalignment='center', |
160 |
| -# verticalalignment='center', |
161 |
| -# weight='bold', |
162 |
| -# size='20') |
163 |
| -# plt.show() |
| 23 | +X_spca, X |
164 | 24 |
|
165 | 25 |
|
166 | 26 |
|
0 commit comments