本文整理匯總了Python中sklearn.base.TransformerMixin方法的典型用法代碼示例。如果您正苦於以下問題:Python base.TransformerMixin方法的具體用法?Python base.TransformerMixin怎麽用?Python base.TransformerMixin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.base
的用法示例。
在下文中一共展示了base.TransformerMixin方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_pandas_only_svm_classifier
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def create_pandas_only_svm_classifier(X, y, probability=True):
class PandasOnlyEstimator(TransformerMixin):
def fit(self, X, y=None, **fitparams):
return self
def transform(self, X, **transformparams):
dataset_is_df = isinstance(X, pd.DataFrame)
if not dataset_is_df:
raise Exception("Dataset must be a pandas dataframe!")
return X
pandas_only = PandasOnlyEstimator()
clf = svm.SVC(gamma=0.001, C=100.0, probability=probability, random_state=777)
pipeline = Pipeline([("pandas_only", pandas_only), ("clf", clf)])
return pipeline.fit(X, y)
示例2: test_template_1
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_template_1():
"""Assert that TPOT template option generates pipeline when each step is a type of operator."""
tpot_obj = TPOTClassifier(
random_state=42,
verbosity=0,
template='Selector-Transformer-Classifier'
)
tpot_obj._fit_init()
pop = tpot_obj._toolbox.population(n=10)
for deap_pipeline in pop:
operator_count = tpot_obj._operator_count(deap_pipeline)
sklearn_pipeline = tpot_obj._toolbox.compile(expr=deap_pipeline)
assert operator_count == 3
assert issubclass(sklearn_pipeline.steps[0][1].__class__, SelectorMixin)
assert issubclass(sklearn_pipeline.steps[1][1].__class__, TransformerMixin)
assert issubclass(sklearn_pipeline.steps[2][1].__class__, ClassifierMixin)
示例3: test_template_2
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_template_2():
"""Assert that TPOT template option generates pipeline when each step is operator type with a duplicate main type."""
tpot_obj = TPOTClassifier(
random_state=42,
verbosity=0,
template='Selector-Selector-Transformer-Classifier'
)
tpot_obj._fit_init()
pop = tpot_obj._toolbox.population(n=10)
for deap_pipeline in pop:
operator_count = tpot_obj._operator_count(deap_pipeline)
sklearn_pipeline = tpot_obj._toolbox.compile(expr=deap_pipeline)
assert operator_count == 4
assert issubclass(sklearn_pipeline.steps[0][1].__class__, SelectorMixin)
assert issubclass(sklearn_pipeline.steps[1][1].__class__, SelectorMixin)
assert issubclass(sklearn_pipeline.steps[2][1].__class__, TransformerMixin)
assert issubclass(sklearn_pipeline.steps[3][1].__class__, ClassifierMixin)
示例4: test_template_3
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_template_3():
"""Assert that TPOT template option generates pipeline when one of steps is a specific operator."""
tpot_obj = TPOTClassifier(
random_state=42,
verbosity=0,
template='SelectPercentile-Transformer-Classifier'
)
tpot_obj._fit_init()
pop = tpot_obj._toolbox.population(n=10)
for deap_pipeline in pop:
operator_count = tpot_obj._operator_count(deap_pipeline)
sklearn_pipeline = tpot_obj._toolbox.compile(expr=deap_pipeline)
assert operator_count == 3
assert sklearn_pipeline.steps[0][0] == 'SelectPercentile'.lower()
assert issubclass(sklearn_pipeline.steps[0][1].__class__, SelectorMixin)
assert issubclass(sklearn_pipeline.steps[1][1].__class__, TransformerMixin)
assert issubclass(sklearn_pipeline.steps[2][1].__class__, ClassifierMixin)
示例5: test_template_4
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_template_4():
"""Assert that TPOT template option generates pipeline when one of steps is a specific operator."""
tpot_obj = TPOTClassifier(
population_size=5,
generations=2,
random_state=42,
verbosity=0,
config_dict = 'TPOT light',
template='SelectPercentile-Transformer-Classifier'
)
tpot_obj.fit(pretest_X, pretest_y)
assert isinstance(tpot_obj._optimized_pipeline, creator.Individual)
assert not (tpot_obj._start_datetime is None)
sklearn_pipeline = tpot_obj.fitted_pipeline_
operator_count = tpot_obj._operator_count(tpot_obj._optimized_pipeline)
assert operator_count == 3
assert sklearn_pipeline.steps[0][0] == 'SelectPercentile'.lower()
assert issubclass(sklearn_pipeline.steps[0][1].__class__, SelectorMixin)
assert issubclass(sklearn_pipeline.steps[1][1].__class__, TransformerMixin)
assert issubclass(sklearn_pipeline.steps[2][1].__class__, ClassifierMixin)
示例6: track_selected_features
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def track_selected_features(pipeline_stages, num_features):
"""
Args:
pipeline_stages (list [tuple[str, TransformerMixin]]): list of steps. each step is a tuple of Name and
Transformer Object.
num_features (int):
Returns:
np.ndarray:
"""
selected_features = np.arange(num_features)
for p_name, p in pipeline_stages:
if not isinstance(p, BaseFeatureSelector):
continue
p_features = p.selected_features
selected_features = selected_features[p_features]
return selected_features
示例7: create_pandas_only_svm_classifier
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def create_pandas_only_svm_classifier(X, y, probability=True):
class PandasOnlyEstimator(TransformerMixin):
def fit(self, X, y=None, **fitparams):
return self
def transform(self, X, **transformparams):
dataset_is_df = isinstance(X, pd.DataFrame)
if not dataset_is_df:
raise Exception("Dataset must be a pandas dataframe!")
return X
pandas_only = PandasOnlyEstimator()
clf = svm.SVC(gamma=0.001, C=100., probability=probability, random_state=777)
pipeline = Pipeline([('pandas_only', pandas_only), ('clf', clf)])
return pipeline.fit(X, y)
示例8: metric_wrapper
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def metric_wrapper(metric, scaler: Optional[TransformerMixin] = None):
"""
Ensures that a given metric works properly when the model itself returns
a y which is shorter than the target y, and allows scaling the data
before applying the metrics.
Parameters
----------
metric
Metric which must accept y_true and y_pred of the same length
scaler : Optional[TransformerMixin]
Transformer which will be applied on y and y_pred before the metrics is
calculated. Must have method `transform`, so for most scalers it must already
be fitted on `y`.
"""
@functools.wraps(metric)
def _wrapper(y_true, y_pred, *args, **kwargs):
if scaler:
logger.debug(
"Transformer provided to metrics wrapper, scaling y and y_pred before "
"passing to metrics"
)
y_true = scaler.transform(y_true)
y_pred = scaler.transform(y_pred)
return metric(y_true[-len(y_pred) :], y_pred, *args, **kwargs)
return _wrapper
示例9: __init__
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def __init__(
self,
base_estimator: BaseEstimator = KerasAutoEncoder(kind="feedforward_hourglass"),
scaler: TransformerMixin = RobustScaler(),
require_thresholds: bool = True,
window=None,
):
"""
Classifier which wraps a ``base_estimator`` and provides a diff error
based approach to anomaly detection.
It trains a ``scaler`` to the target **after** training, purely for
error calculations. The underlying ``base_estimator`` is trained
with the original, unscaled, ``y``.
Parameters
----------
base_estimator: sklearn.base.BaseEstimator
The model to which normal ``.fit``, ``.predict`` methods will be used.
defaults to py:class:`gordo.machine.model.models.KerasAutoEncoder` with
``kind='feedforward_hourglass``
scaler: sklearn.base.TransformerMixin
Defaults to ``sklearn.preprocessing.RobustScaler``
Used for transforming model output and the original ``y`` to calculate
the difference/error in model output vs expected.
require_thresholds: bool
Requires calculating ``thresholds_`` via a call to :func:`~DiffBasedAnomalyDetector.cross_validate`.
If this is set (default True), but :func:`~DiffBasedAnomalyDetector.cross_validate`
was not called before calling :func:`~DiffBasedAnomalyDetector.anomaly` an ``AttributeError``
will be raised.
window: int
Window size for smoothed thresholds
"""
self.base_estimator = base_estimator
self.scaler = scaler
self.require_thresholds = require_thresholds
self.window = window
示例10: test_clone_pandas_dataframe
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_clone_pandas_dataframe():
class DummyEstimator(BaseEstimator, TransformerMixin):
"""This is a dummy class for generating numerical features
This feature extractor extracts numerical features from pandas data
frame.
Parameters
----------
df: pandas data frame
The pandas data frame parameter.
Notes
-----
"""
def __init__(self, df=None, scalar_param=1):
self.df = df
self.scalar_param = scalar_param
def fit(self, X, y=None):
pass
def transform(self, X):
pass
# build and clone estimator
d = np.arange(10)
df = MockDataFrame(d)
e = DummyEstimator(df, scalar_param=1)
cloned_e = clone(e)
# the test
assert (e.df == cloned_e.df).values.all()
assert_equal(e.scalar_param, cloned_e.scalar_param)
示例11: vectorizer_factory
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def vectorizer_factory(self) -> TransformerMixin:
raise NotImplementedError
示例12: transformer_factory
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def transformer_factory(self) -> TransformerMixin:
return LatentDirichletAllocation(n_components=self.width, learning_method='online', random_state=71)
示例13: _generate_bases_test
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def _generate_bases_test(est, pd_est):
def test(self):
self.assertTrue(isinstance(pd_est, FrameMixin), pd_est)
self.assertFalse(isinstance(est, FrameMixin))
self.assertTrue(isinstance(pd_est, base.BaseEstimator))
try:
mixins = [
base.ClassifierMixin,
base.ClusterMixin,
base.BiclusterMixin,
base.TransformerMixin,
base.DensityMixin,
base.MetaEstimatorMixin,
base.ClassifierMixin,
base.RegressorMixin]
except:
if _sklearn_ver > 17:
raise
mixins = [
base.ClassifierMixin,
base.ClusterMixin,
base.BiclusterMixin,
base.TransformerMixin,
base.MetaEstimatorMixin,
base.ClassifierMixin,
base.RegressorMixin]
for mixin in mixins:
self.assertEqual(
isinstance(pd_est, mixin),
isinstance(est, mixin),
mixin)
return test
示例14: test_get_params_without_init
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_get_params_without_init(self, teardown):
"""Test edge case where the base class does not define
an __init__ method. get_params should resolve to object.__init__
which results in an empty dict.
"""
class TransformerWithoutInit(TransformerMixin, BaseEstimator):
pass
class TransformerWithoutInitStep(Step, TransformerWithoutInit):
pass
step = TransformerWithoutInitStep()
assert step.get_params() == {}
示例15: test_basic
# 需要導入模塊: from sklearn import base [as 別名]
# 或者: from sklearn.base import TransformerMixin [as 別名]
def test_basic():
bags = [np.random.normal(5, 3, size=(np.random.randint(10, 100), 20))
for _ in xrange(50)]
feats = Features(bags, stack=True)
stder = BagStandardizer()
stdized = stder.fit_transform(bags)
stdized.make_stacked()
assert np.allclose(np.mean(stdized.stacked_features), 0)
assert np.allclose(np.std(stdized.stacked_features), 1)
first_five = stder.transform(bags[:5])
assert first_five == stdized[:5]
minmaxer = BagMinMaxScaler([3, 7])
minmaxed = minmaxer.fit_transform(feats)
minmaxed.make_stacked()
assert np.allclose(np.min(minmaxed.stacked_features, 0), 3)
assert np.allclose(np.max(minmaxed.stacked_features, 0), 7)
normer = BagNormalizer('l1')
normed = normer.fit_transform(Features(bags))
normed.make_stacked()
assert np.allclose(np.sum(np.abs(normed.stacked_features), 1), 1)
class GetMean(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
return X.mean(axis=1)[None, :]
m = BagPreprocesser(GetMean())
assert_raises(ValueError, lambda: m.transform(bags))