本文整理汇总了Python中sklearn.decomposition.DictionaryLearning类的典型用法代码示例。如果您正苦于以下问题:Python DictionaryLearning类的具体用法?Python DictionaryLearning怎么用?Python DictionaryLearning使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DictionaryLearning类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dict_learning_lassocd_readonly_data
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)
示例2: test_dict_learning_split
def test_dict_learning_split():
n_atoms = 5
dico = DictionaryLearning(n_atoms, transform_algorithm='threshold')
code = dico.fit(X).transform(X)
dico.split_sign = True
split_code = dico.transform(X)
assert_array_equal(split_code[:, :n_atoms] - split_code[:, n_atoms:], code)
示例3: test_dict_learning_shapes
def test_dict_learning_shapes():
n_components = 5
dico = DictionaryLearning(n_components, random_state=0).fit(X)
assert_equal(dico.components_.shape, (n_components, n_features))
n_components = 1
dico = DictionaryLearning(n_components, random_state=0).fit(X)
assert_equal(dico.components_.shape, (n_components, n_features))
assert_equal(dico.transform(X).shape, (X.shape[0], n_components))
示例4: trainLowDict
def trainLowDict(buffer):
print('Learning the dictionary...')
t0 = time()
dico = DictionaryLearning(n_components=100, alpha=1, max_iter=100,verbose=1)
V = dico.fit(buffer).components_
E = dico.error_
dt = time() - t0
print('done in %.2fs.' % dt)
return V,E
示例5: test_dict_learning_split
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)
示例6: test_dict_learning_reconstruction
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)
示例7: test_dict_learning_nonzero_coefs
def test_dict_learning_nonzero_coefs():
n_components = 4
dico = DictionaryLearning(n_components, transform_algorithm='lars',
transform_n_nonzero_coefs=3, random_state=0)
code = dico.fit(X).transform(X[np.newaxis, 1])
assert_true(len(np.flatnonzero(code)) == 3)
dico.set_params(transform_algorithm='omp')
code = dico.transform(X[np.newaxis, 1])
assert_equal(len(np.flatnonzero(code)), 3)
示例8: sparse_coding
def sparse_coding(dimension, input_x, alpha, iteration, tolerance):
#dl = DictionaryLearning(dimension)
dl = DictionaryLearning(dimension, alpha, iteration, tolerance)
dl.fit(input_x)
#np.set_printoptions(precision=3, suppress=True)
#print code
#print dl.components_
print "error:", dl.error_[-1]
return dl
示例9: test_dict_learning_reconstruction_parallel
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)
示例10: __init__
def __init__(self, model_filename=None):
if model_filename is not None:
self.load_model(model_filename)
else:
# default model params
self.n_components = SparseCoding.DEFAULT_MODEL_PARAMS['n_components']
self.n_features = SparseCoding.DEFAULT_MODEL_PARAMS['n_features']
self.max_iter = SparseCoding.DEFAULT_MODEL_PARAMS['max_iter']
self.random_state = SparseCoding.DEFAULT_MODEL_PARAMS['random_state']
self.dict_init = SparseCoding.DEFAULT_MODEL_PARAMS['dict_init']
self.code_init = SparseCoding.DEFAULT_MODEL_PARAMS['code_init']
# initialize Dictionary Learning object with default params and weights
self.DL_obj = DictionaryLearning(n_components=self.n_components,
alpha=1,
max_iter=self.max_iter,
tol=1e-08,
fit_algorithm='lars',
transform_algorithm='omp',
transform_n_nonzero_coefs=None,
transform_alpha=None,
n_jobs=1,
code_init=self.code_init,
dict_init=self.dict_init,
verbose=False,
split_sign=False,
random_state=self.random_state)
示例11: test_dict_learning_positivity
def test_dict_learning_positivity(transform_algorithm,
positive_code,
positive_dict):
n_components = 5
dico = DictionaryLearning(
n_components, transform_algorithm=transform_algorithm, random_state=0,
positive_code=positive_code, positive_dict=positive_dict).fit(X)
code = dico.transform(X)
if positive_dict:
assert_true((dico.components_ >= 0).all())
else:
assert_true((dico.components_ < 0).any())
if positive_code:
assert_true((code >= 0).all())
else:
assert_true((code < 0).any())
示例12: get_dic_per_cluster
def get_dic_per_cluster(clust_q, data_cluster, dataq, i, out_q=None, kerPCA=False):
if out_q is not None:
name = mpc.current_process().name
print name, 'Starting'
else:
print 'Starting estimation of dic %i...' % i
# parse the feature vectors for each cluster
for q in clust_q:
data_cluster = np.vstack((data_cluster, dataq[q]))
# remove useless first line
data_cluster = data_cluster[1:, :]
# learn the sparse code for that cluster
if kerPCA is False:
dict_learn = DictionaryLearning(n_jobs=10)
dict_learn.fit(data_cluster)
else:
print 'Doing kernel PCA...'
print data_cluster.shape
dict_learn = KernelPCA(kernel="rbf", gamma=10, n_components=3)
#dict_learn = PCA(n_components=10)
dict_learn.fit(data_cluster)
if out_q is not None:
res = {}
res[i] = dict_learn
out_q.put(res)
print name, 'Exiting'
else:
print 'Finished.'
return dict_learn # dict(i = dict_learn)
示例13: create_dictionary_dl
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
示例14: __init__
def __init__(self, num_components=10,
catalog_name='unknown',
alpha = 0.001,
transform_alpha = 0.01,
max_iter = 2000,
tol = 1e-9,
n_jobs = 1,
verbose = True,
random_state = None):
self._decomposition = 'Sparse Coding'
self._num_components = num_components
self._catalog_name = catalog_name
self._alpha = alpha
self._transform_alpha = 0.001
self._n_jobs = n_jobs
self._random_state = random_state
self._DL = DictionaryLearning(n_components=self._num_components,
alpha = self._alpha,
transform_alpha = self._transform_alpha,
n_jobs = self._n_jobs,
verbose = verbose,
random_state = self._random_state)
示例15: SC
class SC(object):
"""
Wrapper for sklearn package. Performs sparse coding
Sparse Coding, or Dictionary Learning has 5 methods:
- fit(waveforms)
update class instance with Sparse Coding fit
- fit_transform()
do what fit() does, but additionally return the projection onto new basis space
- inverse_transform(A)
inverses the decomposition, returns waveforms for an input A, using Z^\dagger
- get_basis()
returns the basis vectors Z^\dagger
- get_params()
returns metadata used for fits.
"""
def __init__(self, num_components=10,
catalog_name='unknown',
alpha = 0.001,
transform_alpha = 0.01,
max_iter = 2000,
tol = 1e-9,
n_jobs = 1,
verbose = True,
random_state = None):
self._decomposition = 'Sparse Coding'
self._num_components = num_components
self._catalog_name = catalog_name
self._alpha = alpha
self._transform_alpha = 0.001
self._n_jobs = n_jobs
self._random_state = random_state
self._DL = DictionaryLearning(n_components=self._num_components,
alpha = self._alpha,
transform_alpha = self._transform_alpha,
n_jobs = self._n_jobs,
verbose = verbose,
random_state = self._random_state)
def fit(self,waveforms):
# TODO make sure there are more columns than rows (transpose if not)
# normalize waveforms
self._waveforms = waveforms
self._DL.fit(self._waveforms)
def fit_transform(self,waveforms):
# TODO make sure there are more columns than rows (transpose if not)
# normalize waveforms
self._waveforms = waveforms
self._A = self._DL.fit_transform(self._waveforms)
return self._A
def inverse_transform(self,A):
# convert basis back to waveforms using fit
new_waveforms = self._DL.inverse_transform(A)
return new_waveforms
def get_params(self):
# TODO know what catalog was used! (include waveform metadata)
params = self._DL.get_params()
params['num_components'] = params.pop('n_components')
params['Decompositon'] = self._decomposition
return params
def get_basis(self):
""" Return the SPCA basis vectors (Z^\dagger)"""
return self._DL.components_