def _get_model(path): with open(path, "rb") as fp: contents = pickle.load(fp) Js, Ks, gJs, gKs, converged, meta = contents # Get best model. J_best_mml, K_best_mml = grid_search.best(Js, Ks, meta["message_length"]) model = meta["best_models"]["mml"] return model
def _get_model(path): with open(path, "rb") as fp: contents = pickle.load(fp) Js, Ks, gJs, gKs, converged, meta, X_H, label_names = contents #pickle.dump((Js, Ks, gJs, gKs, converged, meta, X_H, label_names), fp) print(f"label_names from {path}: {label_names}") # Get best model. J_best_mml, K_best_mml = grid_search.best(Js, Ks, meta["message_length"]) model = meta["best_models"]["mml"] return model
A_target = A_astrophysical #A_target = A_original #A_target[:-2, 0] = 0 #A_target[-2:, 0] = 1 stored_models = [] for i, comparison_model_path in enumerate(comparison_model_paths): with open(comparison_model_path, "rb") as fp: contents = pickle.load(fp) Js, Ks, gJs, gKs, converged, meta, X_H, label_names = contents # Get best model. J_best_mml, K_best_mml = grid_search.best(Js, Ks, meta["message_length"]) comparison_model = meta["best_models"]["mml"] # Perform rotation. A_est = comparison_model.theta_[comparison_model.parameter_names.index("A")] if A_est.shape == A_target.shape: for rotation in range(max_rotations): A_est = comparison_model.theta_[comparison_model.parameter_names.index("A")] R, p_opt, cov, *_ = utils.find_rotation_matrix(A_target, A_est, full_output=True) R_opt = utils.exact_rotation_matrix(A_target, A_est, p0=np.random.uniform(-np.pi, np.pi, comparison_model.n_latent_factors**2))
if config["subtract_mean"]: X = X - np.nanmean(X, axis=0) gJs, gKs, converged, meta = grid_search.grid_search(Js, Ks, X, N_inits=N_inits, mcfa_kwds=mcfa_kwds) # Plot the grid space. ll = meta["ll"] bic = meta["bic"] mml = meta["message_length"] J_best_ll, K_best_ll = grid_search.best(Js, Ks, -ll) J_best_bic, K_best_bic = grid_search.best(Js, Ks, bic) J_best_mml, K_best_mml = grid_search.best(Js, Ks, mml) print( f"Best log likelihood for N = {size} at J = {J_best_ll} and K = {K_best_ll}" ) print( f"Best BIC value found for N = {size} at J = {J_best_bic} and K = {K_best_bic}" ) print( f"Best MML value found for N = {size} at J = {J_best_mml} and K = {K_best_mml}" ) try: # Plot some contours.
max_n_latent_factors = gs_options["max_n_latent_factors"] max_n_components = gs_options["max_n_components"] N_inits = gs_options["n_inits"] Js = 1 + np.arange(max_n_latent_factors) Ks = 1 + np.arange(max_n_components) Jg, Kg, converged, meta = grid_search.grid_search(Js, Ks, X, N_inits=N_inits, mcfa_kwds=mcfa_kwds) ll = meta["ll"] bic = meta["bic"] pseudo_bic = meta["pseudo_bic"] message_length = meta["message_length"] J_best_ll, K_best_ll = grid_search.best(Js, Ks, -ll) J_best_bic, K_best_bic = grid_search.best(Js, Ks, bic) J_best_mml, K_best_mml = grid_search.best(Js, Ks, message_length) print(f"Best log likelihood at J = {J_best_ll} and K = {K_best_ll}") print(f"Best BIC value found at J = {J_best_bic} and K = {K_best_bic}") print(f"Best MML value found at J = {J_best_mml} and K = {K_best_mml}") # Plot some contours. plot_filled_contours_kwds = dict(converged=converged, marker_function=np.nanargmin, cmap="Spectral_r") fig_ll = mpl_utils.plot_filled_contours(Jg, Kg, -ll, colorbar_label=r"$-\log\mathcal{L}$", **plot_filled_contours_kwds) savefig(fig_ll, "gridsearch-ll")