本文整理汇总了Python中sklearn.compose.make_column_transformer方法的典型用法代码示例。如果您正苦于以下问题:Python compose.make_column_transformer方法的具体用法?Python compose.make_column_transformer怎么用?Python compose.make_column_transformer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.compose
的用法示例。
在下文中一共展示了compose.make_column_transformer方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_column_transformer_kwargs
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def test_make_column_transformer_kwargs():
scaler = StandardScaler()
norm = Normalizer()
ct = make_column_transformer((scaler, 'first'), (norm, ['second']),
n_jobs=3, remainder='drop',
sparse_threshold=0.5)
assert_equal(ct.transformers, make_column_transformer(
(scaler, 'first'), (norm, ['second'])).transformers)
assert_equal(ct.n_jobs, 3)
assert_equal(ct.remainder, 'drop')
assert_equal(ct.sparse_threshold, 0.5)
# invalid keyword parameters should raise an error message
assert_raise_message(
TypeError,
'Unknown keyword arguments: "transformer_weights"',
make_column_transformer, (scaler, 'first'), (norm, ['second']),
transformer_weights={'pca': 10, 'Transf': 1}
)
示例2: get_estimator
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def get_estimator():
categorical_cols = ['Sex', 'Pclass', 'Embarked']
numerical_cols = ['Age', 'SibSp', 'Parch', 'Fare']
preprocessor = make_column_transformer(
(OneHotEncoder(handle_unknown='ignore'), categorical_cols),
(SimpleImputer(strategy='constant', fill_value=-1), numerical_cols),
)
pipeline = Pipeline([
('transformer', preprocessor),
('classifier', LogisticRegression()),
])
return pipeline
示例3: get_estimator
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [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_column_transformer_mixed_cols_sparse
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def test_column_transformer_mixed_cols_sparse():
df = np.array([['a', 1, True],
['b', 2, False]],
dtype='O')
ct = make_column_transformer(
(OneHotEncoder(), [0]),
('passthrough', [1, 2]),
sparse_threshold=1.0
)
# this shouldn't fail, since boolean can be coerced into a numeric
# See: https://github.com/scikit-learn/scikit-learn/issues/11912
X_trans = ct.fit_transform(df)
assert X_trans.getformat() == 'csr'
assert_array_equal(X_trans.toarray(), np.array([[1, 0, 1, 1],
[0, 1, 2, 0]]))
ct = make_column_transformer(
(OneHotEncoder(), [0]),
('passthrough', [0]),
sparse_threshold=1.0
)
with pytest.raises(ValueError,
match="For a sparse output, all columns should"):
# this fails since strings `a` and `b` cannot be
# coerced into a numeric.
ct.fit_transform(df)
示例5: test_make_column_transformer
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def test_make_column_transformer():
scaler = StandardScaler()
norm = Normalizer()
ct = make_column_transformer((scaler, 'first'), (norm, ['second']))
names, transformers, columns = zip(*ct.transformers)
assert_equal(names, ("standardscaler", "normalizer"))
assert_equal(transformers, (scaler, norm))
assert_equal(columns, ('first', ['second']))
# XXX remove in v0.22
with pytest.warns(DeprecationWarning,
match='`make_column_transformer` now expects'):
ct1 = make_column_transformer(([0], norm))
ct2 = make_column_transformer((norm, [0]))
X_array = np.array([[0, 1, 2], [2, 4, 6]]).T
assert_almost_equal(ct1.fit_transform(X_array),
ct2.fit_transform(X_array))
with pytest.warns(DeprecationWarning,
match='`make_column_transformer` now expects'):
make_column_transformer(('first', 'drop'))
with pytest.warns(DeprecationWarning,
match='`make_column_transformer` now expects'):
make_column_transformer(('passthrough', 'passthrough'),
('first', 'drop'))
示例6: test_make_column_transformer_pandas
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def test_make_column_transformer_pandas():
pd = pytest.importorskip('pandas')
X_array = np.array([[0, 1, 2], [2, 4, 6]]).T
X_df = pd.DataFrame(X_array, columns=['first', 'second'])
norm = Normalizer()
# XXX remove in v0.22
with pytest.warns(DeprecationWarning,
match='`make_column_transformer` now expects'):
ct1 = make_column_transformer((X_df.columns, norm))
ct2 = make_column_transformer((norm, X_df.columns))
assert_almost_equal(ct1.fit_transform(X_df),
ct2.fit_transform(X_df))
示例7: test_make_column_transformer_remainder_transformer
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def test_make_column_transformer_remainder_transformer():
scaler = StandardScaler()
norm = Normalizer()
remainder = StandardScaler()
ct = make_column_transformer((scaler, 'first'), (norm, ['second']),
remainder=remainder)
assert ct.remainder == remainder
示例8: create_preprocessing_pipeline
# 需要导入模块: from sklearn import compose [as 别名]
# 或者: from sklearn.compose import make_column_transformer [as 别名]
def create_preprocessing_pipeline(num_columns):
preprocessor = make_column_transformer(
(np.arange(num_columns), StandardScaler()),
remainder='passthrough'
)
return preprocessor