Python使用 sklearn pipeline进行数据清洗

news/2024/7/3 10:57:30
  1. setup pipeline

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
(‘imputer’, Imputer(strategy=“median”)), #中值写入
(‘attribs_adder’, CombinedAttributesAdder()),#增加比例列
(‘std_scaler’, StandardScaler()),#标准化
])
housing_num_tr = num_pipeline.fit_transform(housing_num)

And a transformer to just select a subset of the Pandas DataFrame columns:
from sklearn.base import BaseEstimator, TransformerMixin

Create a class to select numerical or categorical columns

since Scikit-Learn doesn’t handle DataFrames yet

class DataFrameSelector(BaseEstimator, TransformerMixin):
def init(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names].values

2. 建立总的pipeline*

num_attribs = list(housing_num)
cat_attribs = [“ocean_proximity”]

num_pipeline = Pipeline([
(‘selector’, DataFrameSelector(num_attribs)),
(‘imputer’, Imputer(strategy=“median”)),
(‘attribs_adder’, CombinedAttributesAdder()),
(‘std_scaler’, StandardScaler()),
])

cat_pipeline = Pipeline([
(‘selector’, DataFrameSelector(cat_attribs)),
(‘cat_encoder’, CategoricalEncoder(encoding=“onehot-dense”)),
])

from sklearn.pipeline import FeatureUnion

full_pipeline = FeatureUnion(transformer_list=[
(“num_pipeline”, num_pipeline),
(“cat_pipeline”, cat_pipeline),
])
housing_prepared = full_pipeline.fit_transform(housing)
housing_prepared


http://www.niftyadmin.cn/n/4613421.html

相关文章

Swift iOS macOS 多语言支持,国际化支持, Localization Localizable

Swift iOS macOS 多语言支持,国际化支持, Localization Localizable 搞开发一定要不断的提升自己的英文水平,因为很多文档都是英文的,像 iOS 的开发API文档等等。学好英文,你就可以在编程的世界里任意遨游。 完成一个项…

Swift iOS macOS 字符串 插入到已排序的数组中,字符串对比

Swift iOS macOS 字符串 插入到已排序的数组中,字符串对比 今天需要实现这样一个功能: 有一个字符串 ad,需要按顺序插入到这样一个字符串数组中。 注意:这个数组中有近8万条数据,所以不可能每添加一个数据都重新排序一…

python 回归和决策树 数据验证和参数调整

cross_val_score, cv10, 随机分成10个子集 ​from sklearn.model_selection import cross_val_score scores cross_val_score(tree_reg, housing_prepared, housing_labels, scoring“neg_mean_squared_error”, cv10) tree_rmse_scores np.sqrt(-scores) def display_scor…

Swift iOS macOS 如何 Localize StoryBoard,StoryBoard 本地化,添加多语言支持,基于 String

Swift iOS macOS 如何 Localize StoryBoard,StoryBoard 本地化,添加多语言支持,基于 String 本地化主要包含两个部分:代码中文本的本地化 和 StoryBoard 中文本的本地化 iOS 和 macOS 是一样的 这里只介绍 StoryBoard 的本地化&a…

Integer类型的数据比较大小

因为实体类用的是Integer包装类,所以是对象,不能直接比较大小, 一、一个Integer一个Int可以直接比较大小 二、两个Integer需要用.intValue()方法比较大小: 例如:cw.getCwId().intValue()list.get(i).getCwId().intValu…

使用NLP从文章中自动提取关键字

运用场景: call centre 数据的关键字提取,从而根据关键字归类为部件相关类(ADF,Engine, FB,boot up,duplex issue etc)和流程相关类(install issue, print issue, fax issue etc.。 在研究和新闻文章中,关键词构成了…

Power BI中的QA功能预览

微软在休斯敦的全球合作伙伴大会上发布了Power BI for Office 365,通过Excel和Office 365中的自服务式商业智能解决方案为信息工作者提供了数据分析以及可视化功能以帮助他们更好的洞悉企业内部和外部的数据。其中有一个很棒的功能就是Power BI的QA功能,…

用户关闭页面前提示,提醒保存内容 Window 的 unload onunload onbeforeunload 事件

用户关闭页面前提示,提醒保存内容 Window 的 unload onunload onbeforeunload 事件 在线测试例子: http://kylebing.cn/test/unload-event/ 在很多有编辑内容的页面都需要实现这个功能: 用户在点击刷新、关闭、离开、关闭当前标签页、关闭浏览…