def learn_sparse_components3(shapes, n_components, lmbda, batch_size, transform_n_nonzero_coefs, fit_algorithm, n_iter=5000): """Learn sparse components from a dataset of shapes.""" n_shapes = len(shapes) # Learn sparse components and predict coefficients for the dataset dl = MiniBatchDictionaryLearning( n_components=n_components, alpha=lmbda, batch_size=batch_size, n_iter=n_iter, transform_n_nonzero_coefs=transform_n_nonzero_coefs, verbose=1, fit_algorithm=fit_algorithm, transform_algorithm='lasso_cd', positive_code=True) dl.coefficients = dl.fit_transform(shapes) # Compute frequency of activations and argsort # (but do not apply argsort as we would also need to sort coefficients and all inner # stats of the sklearn object) dl.frequencies = np.count_nonzero(dl.coefficients.T, axis=1) / n_shapes dl.argsort_freqs = np.argsort(-dl.frequencies) return dl