我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用sklearn.decomposition.DictionaryLearning()。
def create_dictionary_dl(lmbd, K=100, N=10000, dir_mnist='save_exp/mnist'): import os.path as osp fname = osp.join(dir_mnist, "D_mnist_K{}_lmbd{}.npy".format(K, lmbd)) if osp.exists(fname): D = np.load(fname) else: from sklearn.decomposition import DictionaryLearning mnist = input_data.read_data_sets('MNIST_data', one_hot=True) im = mnist.train.next_batch(N)[0] im = im.reshape(N, 28, 28) im = [imresize(a, (17, 17), interp='bilinear', mode='L')-.5 for a in im] X = np.array(im).reshape(N, -1) print(X.shape) dl = DictionaryLearning(K, alpha=lmbd*N, fit_algorithm='cd', n_jobs=-1, verbose=1) dl.fit(X) D = dl.components_.reshape(K, -1) np.save(fname, D) return D
def learn_dictionary(patches, n_c=512, a=1, n_i=100, n_j=3, es=5, fit_algorithm='lars'): dic = DictionaryLearning(n_components=n_c, alpha=a, max_iter=n_i, n_jobs=n_j, fit_algorithm=fit_algorithm) print ("Start learning dictionary: n_c: "+str(n_c)+", alpha: "+str(a)+", n_i: " + str(n_i)+", es: "+str(es)+", n_j: "+str(n_j)) v2 = dic.fit(patches).components_ d2 = v2.reshape(n_c, es, es, es) # e.g. 512x5x5x5 return d2
def test_DictionaryLearning(): ''' test the DL method :return: None ''' X=[[1,2,3,4,5], [6,7,8,9,10], [10,9,8,7,6,], [5,4,3,2,1] ] print("before transform:",X) dct=DictionaryLearning(n_components=3) dct.fit(X) print("components is :",dct.components_) print("after transform:",dct.transform(X))
def test_dict_learning_shapes(): n_components = 5 dico = DictionaryLearning(n_components, random_state=0).fit(X) assert_true(dico.components_.shape == (n_components, n_features))
def test_dict_learning_overcomplete(): n_components = 12 dico = DictionaryLearning(n_components, random_state=0).fit(X) assert_true(dico.components_.shape == (n_components, n_features))
def test_dict_learning_reconstruction(): n_components = 12 dico = DictionaryLearning(n_components, transform_algorithm='omp', transform_alpha=0.001, random_state=0) code = dico.fit(X).transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X) dico.set_params(transform_algorithm='lasso_lars') code = dico.transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X, decimal=2) # used to test lars here too, but there's no guarantee the number of # nonzero atoms is right.
def test_dict_learning_reconstruction_parallel(): # regression test that parallel reconstruction works with n_jobs=-1 n_components = 12 dico = DictionaryLearning(n_components, transform_algorithm='omp', transform_alpha=0.001, random_state=0, n_jobs=-1) code = dico.fit(X).transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X) dico.set_params(transform_algorithm='lasso_lars') code = dico.transform(X) assert_array_almost_equal(np.dot(code, dico.components_), X, decimal=2)
def test_dict_learning_lassocd_readonly_data(): n_components = 12 with TempMemmap(X) as X_read_only: dico = DictionaryLearning(n_components, transform_algorithm='lasso_cd', transform_alpha=0.001, random_state=0, n_jobs=-1) code = dico.fit(X_read_only).transform(X_read_only) assert_array_almost_equal(np.dot(code, dico.components_), X_read_only, decimal=2)
def test_dict_learning_unknown_fit_algorithm(): n_components = 5 dico = DictionaryLearning(n_components, fit_algorithm='<unknown>') assert_raises(ValueError, dico.fit, X)
def test_dict_learning_split(): n_components = 5 dico = DictionaryLearning(n_components, transform_algorithm='threshold', random_state=0) code = dico.fit(X).transform(X) dico.split_sign = True split_code = dico.transform(X) assert_array_equal(split_code[:, :n_components] - split_code[:, n_components:], code)