本文整理匯總了Python中sklearn.preprocessing.FunctionTransformer方法的典型用法代碼示例。如果您正苦於以下問題:Python preprocessing.FunctionTransformer方法的具體用法?Python preprocessing.FunctionTransformer怎麽用?Python preprocessing.FunctionTransformer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.preprocessing
的用法示例。
在下文中一共展示了preprocessing.FunctionTransformer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def __init__(self, pos_features, pipeline_obj_path):
"""
Args:
pos_features: list of positional features to use
pipeline_obj_path: path to the serialized pipeline obj_path
"""
self.pos_features = pos_features
self.pipeline_obj_path = pipeline_obj_path
# deserialize the pickle file
with open(self.pipeline_obj_path, "rb") as f:
pipeline_obj = pickle.load(f)
self.POS_FEATURES = pipeline_obj[0]
self.minmax_scaler = pipeline_obj[1]
self.imp = pipeline_obj[2]
self.funct_transform = FunctionTransformer(func=sign_log_func,
inverse_func=sign_log_func_inverse)
# for simplicity, assume all current pos_features are the
# same as from before
assert self.POS_FEATURES == self.pos_features
示例2: __init__
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def __init__(
self,
alpha=1.0,
threshold=0.1,
degree=3,
operators=None,
dt=1.0,
n_jobs=1,
derivative=None,
feature_names=None,
kw={},
):
self.alpha = alpha
self.threshold = threshold
self.degree = degree
self.operators = operators
self.n_jobs = n_jobs
self.derivative = derivative or FunctionTransformer(func=_derivative, kw_args={"dt": dt})
self.feature_names = feature_names
self.kw = kw
示例3: get_estimator
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def get_estimator():
merge_transformer = FunctionTransformer(_merge_external_data,
validate=False)
categorical_cols = ['Arrival', 'Departure']
drop_col = ['DateOfDeparture']
preoprocessor = make_column_transformer(
(OneHotEncoder(handle_unknown='ignore'), categorical_cols),
('drop', drop_col),
remainder='passthrough'
)
pipeline = Pipeline(steps=[
('merge', merge_transformer),
('transfomer', preoprocessor),
('regressor', RandomForestRegressor(n_estimators=10, max_depth=10,
max_features=10)),
])
return pipeline
示例4: test_different_implementations
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_different_implementations():
random_state = 1233
X_train, y_train = make_classification_problem()
# Compare with chained transformations.
tran1 = RandomIntervalSegmenter(n_intervals='sqrt',
random_state=random_state)
tran2 = RowTransformer(FunctionTransformer(func=np.mean, validate=False))
A = tran2.fit_transform(tran1.fit_transform(X_train))
tran = RandomIntervalFeatureExtractor(n_intervals='sqrt',
features=[np.mean],
random_state=random_state)
B = tran.fit_transform(X_train)
np.testing.assert_array_equal(A, B)
# Compare with transformer pipeline using TSFeatureUnion.
示例5: test_different_pipelines
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_different_pipelines():
random_state = 1233
X_train, y_train = make_classification_problem()
steps = [
('segment', RandomIntervalSegmenter(n_intervals='sqrt',
random_state=random_state)),
('transform', FeatureUnion([
('mean', RowTransformer(
FunctionTransformer(func=np.mean, validate=False))),
('std',
RowTransformer(FunctionTransformer(func=np.std, validate=False))),
('slope', RowTransformer(
FunctionTransformer(func=time_series_slope, validate=False))),
])),
]
pipe = Pipeline(steps)
a = pipe.fit_transform(X_train)
tran = RandomIntervalFeatureExtractor(n_intervals='sqrt',
features=[np.mean, np.std,
time_series_slope],
random_state=random_state)
b = tran.fit_transform(X_train)
np.testing.assert_array_equal(a, b)
np.testing.assert_array_equal(pipe.steps[0][1].intervals_, tran.intervals_)
示例6: test_ColumnTransformer_pipeline
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_ColumnTransformer_pipeline():
X_train, y_train = load_basic_motions(split="train", return_X_y=True)
X_test, y_test = load_basic_motions(split="test", return_X_y=True)
# using Identity function transformers (transform series to series)
def id_func(X):
return X
column_transformer = ColumnTransformer([
('id0', FunctionTransformer(func=id_func, validate=False), ['dim_0']),
('id1', FunctionTransformer(func=id_func, validate=False), ['dim_1'])
])
steps = [
('extract', column_transformer),
('tabularise', Tabularizer()),
('classify', RandomForestClassifier(n_estimators=2, random_state=1))]
model = Pipeline(steps=steps)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
assert y_pred.shape[0] == y_test.shape[0]
np.testing.assert_array_equal(np.unique(y_pred), np.unique(y_test))
示例7: test_FeatureUnion_pipeline
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_FeatureUnion_pipeline():
# pipeline with segmentation plus multiple feature extraction
steps = [
('segment', RandomIntervalSegmenter(n_intervals=3)),
('transform', FeatureUnion([
('mean', RowTransformer(
FunctionTransformer(func=np.mean, validate=False))),
('std',
RowTransformer(FunctionTransformer(func=np.std, validate=False)))
])),
('clf', DecisionTreeClassifier())
]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
assert y_pred.shape[0] == y_test.shape[0]
np.testing.assert_array_equal(np.unique(y_pred), np.unique(y_test))
示例8: test_objectmapper
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.preprocessing.Binarizer, pp.Binarizer)
self.assertIs(df.preprocessing.FunctionTransformer,
pp.FunctionTransformer)
self.assertIs(df.preprocessing.Imputer, pp.Imputer)
self.assertIs(df.preprocessing.KernelCenterer, pp.KernelCenterer)
self.assertIs(df.preprocessing.LabelBinarizer, pp.LabelBinarizer)
self.assertIs(df.preprocessing.LabelEncoder, pp.LabelEncoder)
self.assertIs(df.preprocessing.MultiLabelBinarizer, pp.MultiLabelBinarizer)
self.assertIs(df.preprocessing.MaxAbsScaler, pp.MaxAbsScaler)
self.assertIs(df.preprocessing.MinMaxScaler, pp.MinMaxScaler)
self.assertIs(df.preprocessing.Normalizer, pp.Normalizer)
self.assertIs(df.preprocessing.OneHotEncoder, pp.OneHotEncoder)
self.assertIs(df.preprocessing.PolynomialFeatures, pp.PolynomialFeatures)
self.assertIs(df.preprocessing.RobustScaler, pp.RobustScaler)
self.assertIs(df.preprocessing.StandardScaler, pp.StandardScaler)
示例9: _get_transformations_one_to_many_greater
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def _get_transformations_one_to_many_greater(self, feature_names):
# results in number of features greater than original features
# copy all features except last one. For last one, replicate columns to create 3 more features
transformations = []
feature_names = list(feature_names)
index = 0
for f in feature_names[:-1]:
transformations.append(("{}".format(index), "passthrough", [f]))
index += 1
def copy_func(x):
return np.tile(x, (1, 3))
copy_transformer = FunctionTransformer(copy_func)
transformations.append(("copy_transformer", copy_transformer, [feature_names[-1]]))
return ColumnTransformer(transformations)
示例10: test_multiply_by_function_transformer
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_multiply_by_function_transformer(self):
from gordo.machine.model.transformer_funcs.general import multiply_by
# Provide a require argument
tf = FunctionTransformer(func=multiply_by, kw_args={"factor": 2})
self._validate_transformer(tf)
# Ignore the required argument
tf = FunctionTransformer(func=multiply_by)
with self.assertRaises(TypeError):
self._validate_transformer(tf)
示例11: test_transform_target_regressor_1d_transformer
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_transform_target_regressor_1d_transformer(X, y):
# All transformer in scikit-learn expect 2D data. FunctionTransformer with
# validate=False lift this constraint without checking that the input is a
# 2D vector. We check the consistency of the data shape using a 1D and 2D y
# array.
transformer = FunctionTransformer(func=lambda x: x + 1,
inverse_func=lambda x: x - 1,
validate=False)
regr = TransformedTargetRegressor(regressor=LinearRegression(),
transformer=transformer)
y_pred = regr.fit(X, y).predict(X)
assert y.shape == y_pred.shape
# consistency forward transform
y_tran = regr.transformer_.transform(y)
_check_shifted_by_one(y, y_tran)
assert y.shape == y_pred.shape
# consistency inverse transform
assert_allclose(y, regr.transformer_.inverse_transform(
y_tran).squeeze())
# consistency of the regressor
lr = LinearRegression()
transformer2 = clone(transformer)
lr.fit(X, transformer2.fit_transform(y))
y_lr_pred = lr.predict(X)
assert_allclose(y_pred, transformer2.inverse_transform(y_lr_pred))
assert_allclose(regr.regressor_.coef_, lr.coef_)
示例12: test_np_log
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_np_log():
X = np.arange(10).reshape((5, 2))
# Test that the numpy.log example still works.
assert_array_equal(
FunctionTransformer(np.log1p).transform(X),
np.log1p(X),
)
示例13: test_kw_arg
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_kw_arg():
X = np.linspace(0, 1, num=10).reshape((5, 2))
F = FunctionTransformer(np.around, kw_args=dict(decimals=3))
# Test that rounding is correct
assert_array_equal(F.transform(X),
np.around(X, decimals=3))
示例14: test_kw_arg_update
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_kw_arg_update():
X = np.linspace(0, 1, num=10).reshape((5, 2))
F = FunctionTransformer(np.around, kw_args=dict(decimals=3))
F.kw_args['decimals'] = 1
# Test that rounding is correct
assert_array_equal(F.transform(X), np.around(X, decimals=1))
示例15: test_inverse_transform
# 需要導入模塊: from sklearn import preprocessing [as 別名]
# 或者: from sklearn.preprocessing import FunctionTransformer [as 別名]
def test_inverse_transform():
X = np.array([1, 4, 9, 16]).reshape((2, 2))
# Test that inverse_transform works correctly
F = FunctionTransformer(
func=np.sqrt,
inverse_func=np.around, inv_kw_args=dict(decimals=3),
)
assert_array_equal(
F.inverse_transform(F.transform(X)),
np.around(np.sqrt(X), decimals=3),
)