当前位置: 首页>>代码示例>>Python>>正文


Python DictionaryLearning.transform方法代码示例

本文整理汇总了Python中sklearn.decomposition.DictionaryLearning.transform方法的典型用法代码示例。如果您正苦于以下问题:Python DictionaryLearning.transform方法的具体用法?Python DictionaryLearning.transform怎么用?Python DictionaryLearning.transform使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.decomposition.DictionaryLearning的用法示例。


在下文中一共展示了DictionaryLearning.transform方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_dict_learning_split

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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)
开发者ID:boersmamarcel,项目名称:scikit-learn,代码行数:10,代码来源:test_dict_learning.py

示例2: test_dict_learning_shapes

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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))
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:11,代码来源:test_dict_learning.py

示例3: test_dict_learning_nonzero_coefs

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:12,代码来源:test_dict_learning.py

示例4: test_dict_learning_reconstruction

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:12,代码来源:test_dict_learning.py

示例5: test_dict_learning_split

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:12,代码来源:test_dict_learning.py

示例6: test_dict_learning_reconstruction_parallel

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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)
开发者ID:Lavanya-Basavaraju,项目名称:scikit-learn,代码行数:13,代码来源:test_dict_learning.py

示例7: test_dict_learning_positivity

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]
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())
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:18,代码来源:test_dict_learning.py

示例8: DictionaryLearning

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [as 别名]

#Decomposition分解 to classify分类 with DictionaryLearning

from sklearn.decomposition import DictionaryLearning
dl = DictionaryLearning(3)
transformed = dl.fit_transform(iris_data[::2])
transformed[:5]
#array([[ 0. , 6.34476574, 0. ],
#[ 0. , 5.83576461, 0. ],
#[ 0. , 6.32038375, 0. ],
#[ 0. , 5.89318572, 0. ],
#[ 0. , 5.45222715, 0. ]])

#Next, let's fit (not fit_transform) the testing set:
transformed = dl.transform(iris_data[1::2])


#Putting it all together with Pipelines

#Let's briefly load the iris dataset and seed it with some missing values:
from sklearn.datasets import load_iris
import numpy as np
iris = load_iris()
iris_data = iris.data
mask = np.random.binomial(1, .25, iris_data.shape).astype(bool)
iris_data[mask] = np.nan
iris_data[:5]
#array([[ 5.1, 3.5, 1.4, nan],
#[ nan, 3. , 1.4, 0.2],
#[ 4.7, 3.2, 1.3, 0.2],
开发者ID:chenzhongtao,项目名称:source,代码行数:32,代码来源:premodel.py

示例9: __init__

# 需要导入模块: from sklearn.decomposition import DictionaryLearning [as 别名]
# 或者: from sklearn.decomposition.DictionaryLearning import transform [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
#.........这里部分代码省略.........
开发者ID:nitred,项目名称:sparsex,代码行数:103,代码来源:feature_extraction.py


注:本文中的sklearn.decomposition.DictionaryLearning.transform方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。