sklearn-docs/Dimensionality-Reduction-with-Neighborhood-Components-Analysis
1
1# Gradio Implementation: Lenix Carter2# License: BSD 3-Clause or CC-03 4import gradio as gr5import numpy as np6import matplotlib7import matplotlib.pyplot as plt8 9from sklearn import datasets10from sklearn.model_selection import train_test_split11from sklearn.decomposition import PCA12from sklearn.discriminant_analysis import LinearDiscriminantAnalysis13from sklearn.neighbors import KNeighborsClassifier, NeighborhoodComponentsAnalysis14from sklearn.pipeline import make_pipeline15from sklearn.preprocessing import StandardScaler16 17matplotlib.use('agg')18 19def reduce_dimensions(n_neighbors, random_state):20 # Load Digits dataset21 X, y = datasets.load_digits(return_X_y=True)22 23 # Split into train/test24 X_train, X_test, y_train, y_test = train_test_split(25 X, y, test_size=0.5, stratify=y, random_state=random_state26 )27 28 dim = len(X[0])29 n_classes = len(np.unique(y))30 31 # Reduce dimension to 2 with PCA32 pca = make_pipeline(StandardScaler(), PCA(n_components=2, random_state=random_state))33 34 # Reduce dimension to 2 with LinearDiscriminantAnalysis35 lda = make_pipeline(StandardScaler(), LinearDiscriminantAnalysis(n_components=2))36 37 # Reduce dimension to 2 with NeighborhoodComponentAnalysis38 nca = make_pipeline(39 StandardScaler(),40 NeighborhoodComponentsAnalysis(n_components=2, random_state=random_state),41 )42 43 # Use a nearest neighbor classifier to evaluate the methods44 knn = KNeighborsClassifier(n_neighbors=n_neighbors)45 46 # Make a list of the methods to be compared47 dim_reduction_methods = [("PCA", pca), ("LDA", lda), ("NCA", nca)]48 49 dim_red_graphs = []50 51 for i, (name, model) in enumerate(dim_reduction_methods):52 new = plt.figure()53 54 # Fit the method's model55 model.fit(X_train, y_train)56 57 # Fit a nearest neighbor classifier on the embedded training set58 knn.fit(model.transform(X_train), y_train)59 60 # Compute the nearest neighbor accuracy on the embedded test set61 acc_knn = knn.score(model.transform(X_test), y_test)62 63 # Embed the data set in 2 dimensions using the fitted model64 X_embedded = model.transform(X)65 66 # Plot the projected points and show the evaluation score67 plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=y, s=30, cmap="Set1")68 plt.title(69 "{}, KNN (k={})\nTest accuracy = {:.2f}".format(name, n_neighbors, acc_knn)70 )71 dim_red_graphs.append(new)72 return dim_red_graphs73 74title = "Dimensionality Reduction with Neighborhood Components Analysis"75with gr.Blocks() as demo:76 gr.Markdown(f" # {title}")77 gr.Markdown("""78 This example performs and displays the results of Principal Component Analysis, Linear Descriminant Analysis, and Neighborhood Component Analysis on the Digits dataset. 79 80 The result shows that NCA produces visually meaningful clustering.81 82 This based on the example [here](https://scikit-learn.org/stable/auto_examples/neighbors/plot_nca_dim_reduction.html#sphx-glr-auto-examples-neighbors-plot-nca-dim-reduction-py)83 """)84 with gr.Row():85 n_neighbors = gr.Slider(2, 10, 3, step=1, label="Number of Neighbors for KNN")86 random_state = gr.Slider(0, 100, 0, step=1, label="Random State")87 with gr.Row():88 pca_graph = gr.Plot(label="PCA")89 lda_graph = gr.Plot(label="LDA")90 nca_graph = gr.Plot(label="NCA")91 n_neighbors.change(92 fn=reduce_dimensions,93 inputs=[n_neighbors, random_state],94 outputs=[pca_graph, lda_graph, nca_graph]95 )96 random_state.change(97 fn=reduce_dimensions,98 inputs=[n_neighbors, random_state],99 outputs=[pca_graph, lda_graph, nca_graph]100 )101 102if __name__ == '__main__':103 demo.launch()104 105 