# Authors: Jona Sassenhagen # License: BSD 3 clause import matplotlib.pyplot as plt import numpy as np from sklearn.decomposition import FactorAnalysis, PCA from sklearn.preprocessing import StandardScaler from sklearn.datasets import load_iris import gradio as gr from matplotlib import style plt.switch_backend("agg") style.use('ggplot') font1 = {'family':'DejaVu Sans','color':'#2563EB','size': 14} #load and transform the data data = load_iris() X = StandardScaler().fit_transform(data["data"]) feature_names = data["feature_names"] methods = { "PCA": PCA(), "Unrotated FA": FactorAnalysis(), "Varimax FA": FactorAnalysis(rotation="varimax") } def factor_analysis(method): #figure1 fig1, ax = plt.subplots(figsize=(10, 6), facecolor='none', dpi = 200) im = ax.imshow(np.corrcoef(X.T), cmap="Spectral", vmin=-1, vmax=1) ax.set_xticks([0, 1, 2, 3]) ax.set_xticklabels(list(feature_names), rotation=90, fontdict = font1) ax.set_yticks([0, 1, 2, 3]) ax.set_yticklabels(list(feature_names), fontdict = font1) plt.grid(False) plt.colorbar(im).ax.tick_params() ax.set_title("Iris feature correlation matrix", fontdict=font1, size = 18, color = "white", pad = 20, bbox=dict(boxstyle="round,pad=0.3", color = "#2563EB")) plt.tight_layout() plt.close('all') n_comps = 2 #figure2 fig2, axs = plt.subplots(figsize=(8, 5), facecolor='none', dpi = 200) plt.grid(False) fa = methods[method] fa.set_params(n_components=n_comps) fa.fit(X) components = fa.components_ vmax = np.abs(components).max() axs.imshow(components, cmap="Spectral", vmax=vmax, vmin=-vmax) axs.set_xticks(np.arange(len(feature_names))) axs.set_xticklabels(feature_names, fontdict=font1) axs.set_title(method, fontdict=font1, size = 18, color = "white", pad = 20, bbox=dict(boxstyle="round,pad=0.3", color = "#2563EB")) axs.set_yticks([0, 1]) axs.set_yticklabels(["Comp. 1", "Comp. 2"], fontdict=font1) plt.tight_layout() plt.close('all') return fig1, fig2, components intro = """
Made with ❤
""" link = """