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


Python Pipeline.inverse_transform方法代码示例

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


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

示例1: test_set_pipeline_step_none

# 需要导入模块: from sklearn.pipeline import Pipeline [as 别名]
# 或者: from sklearn.pipeline.Pipeline import inverse_transform [as 别名]
def test_set_pipeline_step_none():
    # Test setting Pipeline steps to None
    X = np.array([[1]])
    y = np.array([1])
    mult2 = Mult(mult=2)
    mult3 = Mult(mult=3)
    mult5 = Mult(mult=5)

    def make():
        return Pipeline([("m2", mult2), ("m3", mult3), ("last", mult5)])

    pipeline = make()

    exp = 2 * 3 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    pipeline.set_params(m3=None)
    exp = 2 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
    assert_dict_equal(
        pipeline.get_params(deep=True),
        {"steps": pipeline.steps, "m2": mult2, "m3": None, "last": mult5, "m2__mult": 2, "last__mult": 5},
    )

    pipeline.set_params(m2=None)
    exp = 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    # for other methods, ensure no AttributeErrors on None:
    other_methods = ["predict_proba", "predict_log_proba", "decision_function", "transform", "score"]
    for method in other_methods:
        getattr(pipeline, method)(X)

    pipeline.set_params(m2=mult2)
    exp = 2 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    pipeline = make()
    pipeline.set_params(last=None)
    # mult2 and mult3 are active
    exp = 6
    assert_array_equal([[exp]], pipeline.fit(X, y).transform(X))
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
    assert_raise_message(AttributeError, "'NoneType' object has no attribute 'predict'", getattr, pipeline, "predict")

    # Check None step at construction time
    exp = 2 * 5
    pipeline = Pipeline([("m2", mult2), ("m3", None), ("last", mult5)])
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
开发者ID:cheral,项目名称:scikit-learn,代码行数:62,代码来源:test_pipeline.py

示例2: test_pipeline_transform

# 需要导入模块: from sklearn.pipeline import Pipeline [as 别名]
# 或者: from sklearn.pipeline.Pipeline import inverse_transform [as 别名]
def test_pipeline_transform():
    # Test whether pipeline works with a transformer at the end.
    # Also test pipeline.transform and pipeline.inverse_transform
    iris = load_iris()
    X = iris.data
    pca = PCA(n_components=2)
    pipeline = Pipeline([('pca', pca)])

    # test transform and fit_transform:
    X_trans = pipeline.fit(X).transform(X)
    X_trans2 = pipeline.fit_transform(X)
    X_trans3 = pca.fit_transform(X)
    assert_array_almost_equal(X_trans, X_trans2)
    assert_array_almost_equal(X_trans, X_trans3)

    X_back = pipeline.inverse_transform(X_trans)
    X_back2 = pca.inverse_transform(X_trans)
    assert_array_almost_equal(X_back, X_back2)
开发者ID:Givonaldo,项目名称:scikit-learn,代码行数:20,代码来源:test_pipeline.py

示例3: test_set_pipeline_step_none

# 需要导入模块: from sklearn.pipeline import Pipeline [as 别名]
# 或者: from sklearn.pipeline.Pipeline import inverse_transform [as 别名]
def test_set_pipeline_step_none():
    # Test setting Pipeline steps to None
    X = np.array([[1]])
    y = np.array([1])
    mult2 = Mult(mult=2)
    mult3 = Mult(mult=3)
    mult5 = Mult(mult=5)

    def make():
        return Pipeline([('m2', mult2), ('m3', mult3), ('last', mult5)])

    pipeline = make()

    exp = 2 * 3 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    pipeline.set_params(m3=None)
    exp = 2 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
    assert_dict_equal(
        pipeline.get_params(deep=True), {
            'steps': pipeline.steps,
            'm2': mult2,
            'm3': None,
            'last': mult5,
            'm2__mult': 2,
            'last__mult': 5,
        })

    pipeline.set_params(m2=None)
    exp = 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    # for other methods, ensure no AttributeErrors on None:
    other_methods = [
        'predict_proba', 'predict_log_proba', 'decision_function', 'transform',
        'score'
    ]
    for method in other_methods:
        getattr(pipeline, method)(X)

    pipeline.set_params(m2=mult2)
    exp = 2 * 5
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))

    pipeline = make()
    pipeline.set_params(last=None)
    # mult2 and mult3 are active
    exp = 6
    assert_array_equal([[exp]], pipeline.fit(X, y).transform(X))
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
    assert_raise_message(AttributeError,
                         "'NoneType' object has no attribute 'predict'",
                         getattr, pipeline, 'predict')

    # Check None step at construction time
    exp = 2 * 5
    pipeline = Pipeline([('m2', mult2), ('m3', None), ('last', mult5)])
    assert_array_equal([[exp]], pipeline.fit_transform(X, y))
    assert_array_equal([exp], pipeline.fit(X).predict(X))
    assert_array_equal(X, pipeline.inverse_transform([[exp]]))
开发者ID:dsquareindia,项目名称:scikit-learn,代码行数:72,代码来源:test_pipeline.py

示例4: AntiClassifier

# 需要导入模块: from sklearn.pipeline import Pipeline [as 别名]
# 或者: from sklearn.pipeline.Pipeline import inverse_transform [as 别名]

#.........这里部分代码省略.........
        self.anticlassifier.fit(self.xtrain, self.ytrain)

        # used to periodically update / retrain the anticlassifier
        self.new_rows = pd.DataFrame(columns=self.xtrain.columns)

    def _retrain(self):
        """Retrain the anticlassifier using the guesses that it generated"""
        y = self.classifier.predict(self.new_rows)
        self.xtrain = pd.concat([self.xtrain, self.new_rows])
        self.ytrain = np.append(self.ytrain, y)
        self.anticlassifier.fit(self.xtrain, self.ytrain)
        self.new_rows = pd.DataFrame(columns=self.xtrain.columns)

    def _lg_coefs(self):
        """Get the coefficients of the logisitical regression classifier used
        by the anticlassifier."""
        return self.anticlassifier.get_params()["logistic"].coef_[0]

    def _lg_intercept(self):
        """Get the intercept term of the logisitical regression classifier used
        by the anticlassifier."""
        return self.anticlassifier.get_params()["logistic"].intercept_

    def _lg_classes(self):
        """Get the class labels of the logisitical regression classifier
        used by the anticlassifier."""
        return self.anticlassifier.get_params()["logistic"].classes_

    def _lg_predict_proba(self, x):
        """Return the predict_proba method evaluated at x of the logistical
        regression classifier used by the anticlassifier.

        x is not normalized or transformed before computing the function.
        """
        assert isinstance(x, np.ndarray), "x must be a numpy ndarray"
        l = self.anticlassifier.get_params()["logistic"]
        return l.predict_proba(x)[0][1]

    def _lg_predict_proba_gradient(self, x):
        """Return the gradient evaluated at x of the logistic regression
        classifier used by the anticlassifier.

        x is not normalized or transformed before computing the gradient.
        """
        assert isinstance(x, np.ndarray), "x must be a numpy ndarray"
        coefs = self._lg_coefs()
        inter = self._lg_intercept()
        g = [
            # the sigmoid partial derivative
            (
                (c * np.exp(inter + x.dot(coefs))) /
                ((1.0 + np.exp(inter + x.dot(coefs)))**2.0)
            )
            for c in coefs
        ]
        return np.array(g)

    def _minimize(self, constraints):
        """Find the feature vector which minimizes the decision function of the
        anticlassifier under constraints. Return this feature vector.

        The anticlassifier performs a normalization on the training data. This
        function runs the inverse transform on the feature vector before
        returning it, i.e. we return the "raw", unnormalized feature vector.
        """

        # the constraints are expressed as boundaries, not values
        # we're minimizing within a region

        result = scipy.optimize.minimize(
            self._lg_predict_proba,
            self._transform.transform(self._guess(constraints)),
            method="SLSQP",
            jac=self._lg_predict_proba_gradient,
            bounds=[(x["min"], x["max"]) for x in self.feature_specs],
            constraints=constraints,
            tol=0.001
        )
        return self._transform.inverse_transform(result.x)[0]

    def _guess(self, constraints):
        """Take an initial guess at the a feature vector under constraints"""
        r = [
            f["type"](uniform(f["min"], f["max"])) for f in self.feature_specs
        ]
        for c in constraints:
            c["init"](r)
        return np.array(r)

    def get(self, constraints):
        """Get a feature vector which satisfies the provided constraints and
        which the classifier is expected to accept.

        Also retrain the anticlassifier if enough feature vectors have been
        generated."""
        v = self._minimize(constraints)
        self.new_rows.loc[len(self.new_rows) + 1] = v
        if len(self.new_rows) >= self.retrain_interval:
            self._retrain()
        return v
开发者ID:mfournierca,项目名称:beating-classifiers,代码行数:104,代码来源:anticlassifier.py

示例5: NiftiMasker

# 需要导入模块: from sklearn.pipeline import Pipeline [as 别名]
# 或者: from sklearn.pipeline.Pipeline import inverse_transform [as 别名]
target = np.vstack(target)

masker = NiftiMasker(mask='mask.nii.gz',
                     standardize=True,
                     memory=Memory(cache_dir))
selector = SelectPercentile(f_classif, percentile=50)
loader = Pipeline([('masker', masker), ('selector', selector)])

le = LabelEncoder()
y = le.fit_transform(target[:, 1])

X = loader.fit_transform(niimgs, y)


clf = LogisticRegression()

cv = StratifiedKFold(y=y[target[:, 0] == 'ds105'], n_folds=5)

# clf.fit(X[target[:, 0] == 'pinel2009twins'], y[target[:, 0] == 'pinel2009twins'])

# print clf.score(X[target[:, 0] != 'pinel2009twins'], y[target[:, 0] != 'pinel2009twins'])

# scores = cross_val_score(
#     clf,
#     X[target[:, 0] == 'ds105'],
#     y[target[:, 0] == 'ds105'], cv=cv, n_jobs=-1)
# print scores
clf.fit(X, y)
coef = loader.inverse_transform(clf.coef_)
coef.to_filename('/tmp/coef.nii.gz')
开发者ID:schwarty,项目名称:contrasts_repr,代码行数:32,代码来源:classif.py


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