def get_model(method, n=None, n_neighbors=None, max_iter=None, random_state=None, n_jobs=None, method_args=None): model = None kwargs = {} if method_args is not None: for arg_pair in method_args: arg_par = arg_pair.split('=') kwargs[arg_pair[0].trim()] = arg_pair[1].trim() if method == 'ICA': model = FastICA(whiten=True, **kwargs) elif method == 'ICAexp': model = FastICA(whiten=True, fun='exp', **kwargs) elif method == 'ICAcube': model = FastICA(whiten=True, fun='cube', **kwargs) elif method == 'PCA': model = PCA() elif method == 'SPCA': model = SparsePCA() if n_jobs is not None: model.n_jobs = n_jobs elif method == 'NMF': model = NMF(solver='cd') elif method == 'ISO': model = Isomap() elif method == 'KPCA': model = KernelPCA(kernel='rbf', fit_inverse_transform=False, gamma=1, alpha=0.0001) elif method == 'FA': #model = FactorAnalysis(svd_method='lapack') #(tol=0.0001, iterated_power=4) model = FactorAnalysis(tol=0.0001, iterated_power=4) elif method == 'DL': model = DictionaryLearning(split_sign=True, fit_algorithm='cd', alpha=1) if n_jobs is not None: model.n_jobs = n_jobs if n is not None: model.n_components = n if max_iter is not None: model.max_iter = max_iter if random_state is not None: model.random_state = random_state if n_neighbors is not None: model.n_neighbors = n_neighbors return model
def optimize_components(X, feature_names, label, abbrev): # model selection (optimal number of components) # choose number of components by highest average kurtosis n_components = np.arange(1, len(feature_names) + 1) ica = FastICA(random_state=SEED) ica_scores = [] for n in n_components: ica.n_components = n result = pd.DataFrame(ica.fit_transform(X)) ica_scores.append(result.kurtosis().mean()) n_components_ica = n_components[np.argmax(ica_scores)] print(label + ": best n_components by average kurtosis = %d" % n_components_ica) # create plot plt.figure() plt.plot(n_components, ica_scores, 'b', label='average kurtosis by number of components') plt.axvline(n_components_ica, color='b', label='chosen number of components: %d' % n_components_ica, linestyle='--') ax = plt.gca() ax.xaxis.set_major_locator(MaxNLocator(integer=True)) plt.xlabel('number of components') plt.ylabel('average kurtosis') plt.legend(loc='lower right') plt.title(label + ": ICA model selection") plt.savefig(path.join(PLOT_DIR, abbrev + "_ica_components.png"), bbox_inches='tight') plt.show() plt.close() return n_components_ica
def compute_scores(X): pca = PCA(svd_solver='auto') kpca = KernelPCA(fit_inverse_transform=True) ica = FastICA() nmf = NMF(init='nndsvda') pca_scores, ica_scores, nmf_scores, kpca_scores = [], [], [], [] for n in n_components: pca.n_components = n ica.n_components = n nmf.n_components = n kpca.n_components = n print(n) Xpca = pca.inverse_transform(pca.fit_transform(Xs)) pca_scores.append(explained_variance_score(Xs, Xpca)) Xica = ica.inverse_transform(ica.fit_transform(Xs)) ica_scores.append(explained_variance_score(Xs, Xica)) Xkpca = kpca.inverse_transform(kpca.fit_transform(Xs)) kpca_scores.append(explained_variance_score(Xs, Xkpca)) Xnmf = nmf.inverse_transform(nmf.fit_transform(X)) nmf_scores.append(explained_variance_score(X, Xnmf)) return pca_scores, ica_scores, nmf_scores, kpca_scores
x = A @ sig # x += 0.1*np.random.randn(*x.shape) xm = x - x.mean(axis=1)[:, None] # #### Perform PCA u, s, v = np.linalg.svd(xm, full_matrices=False) nm = np.sum(s > 1e-6) s_pca = v[:nm, :].T * s[:nm] # #### Apply ICA xw = (u / s).T[:sig.shape[0]] @ xm xw *= np.sqrt(xm.shape[1]) ica = FastICA(algorithm='deflation', tol=1e-12) ica.whiten = True ica.n_components = sig.shape[0] s_ica = ica.fit_transform(xm.T) # ica.whiten = False # s_ica = ica.fit_transform(xw.T) s_ica *= s[:nm].mean() # #### Plot data f = mplt.figure(figsize=(10, 10)) gs = mgs.GridSpec(4, 3) gs.update(top=0.96, bottom=0.05, left=0.08, right=0.98, hspace=0.3, wspace=0.35)