本文整理汇总了Python中sklearn.decomposition.DictionaryLearning.get_params方法的典型用法代码示例。如果您正苦于以下问题:Python DictionaryLearning.get_params方法的具体用法?Python DictionaryLearning.get_params怎么用?Python DictionaryLearning.get_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.DictionaryLearning
的用法示例。
在下文中一共展示了DictionaryLearning.get_params方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SC
# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import get_params [as 别名]
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_
示例2: __init__
# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import get_params [as 别名]
class SparseCoding:
DEFAULT_MODEL_PARAMS = {
'n_components' : 10,
'n_features' : 64,
'max_iter' : 5,
'random_state' : 1,
'dict_init' : None,
'code_init' : None
}
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)
def save_model(self, filename):
# save DL object to file, compress is also to prevent multiple model files.
joblib.dump(self.DL_obj, filename, compress=3)
def load_model(self, filename):
# load DL Object from file
self.DL_obj = joblib.load(filename)
# set certain model params as class attributes. Get values from DL Obj.get_params() or use default values.
DL_params = self.DL_obj.get_params()
for param in SparseCoding.DEFAULT_MODEL_PARAMS:
if param in DL_params:
setattr(self, param, DL_params[param])
else:
setattr(self, param, SparseCoding.DEFAULT_MODEL_PARAMS[param])
def learn_dictionary(self, whitened_patches):
# assert correct dimensionality of input data
if whitened_patches.ndim == 3:
whitened_patches = whitened_patches.reshape((whitened_patches.shape[0], -1))
assert whitened_patches.ndim == 2, "Whitened patches ndim is %d instead of 2" %whitened_patches.ndim
# learn dictionary
self.DL_obj.fit(whitened_patches)
def get_dictionary(self):
try:
return self.DL_obj.components_
except AttributeError:
raise AttributeError("Feature extraction dictionary has not yet been learnt for this model. " \
+ "Train the feature extraction model at least once to prevent this error.")
def get_sparse_features(self, whitened_patches):
# assert correct dimensionality of input data
if whitened_patches.ndim == 3:
whitened_patches = whitened_patches.reshape((whitened_patches.shape[0], -1))
assert whitened_patches.ndim == 2, "Whitened patches ndim is %d instead of 2" %whitened_patches.ndim
try:
sparse_code = self.DL_obj.transform(whitened_patches)
except NotFittedError:
raise NotFittedError("Feature extraction dictionary has not yet been learnt for this model, " \
+ "therefore Sparse Codes cannot be extracted. Train the feature extraction model " \
+ "at least once to prevent this error.")
return sparse_code
def get_sign_split_features(self, sparse_features):
n_samples, n_components = sparse_features.shape
sign_split_features = np.empty((n_samples, 2 * n_components))
sign_split_features[:, :n_components] = np.maximum(sparse_features, 0)
sign_split_features[:, n_components:] = -np.minimum(sparse_features, 0)
return sign_split_features
def get_pooled_features(self, input_feature_map, filter_size=(19,19)):
# assuming square filters and images
#.........这里部分代码省略.........