标签:selector tran median nic false learn wrap cto enc
1 train_data = load_titanic_data("train.csv") 2 train_data["Pclass"].value_counts()
3 491
1 216
2 184
Name: Pclass, dtype: int64
1 from sklearn.pipeline import Pipeline 2 from sklearn.preprocessing import Imputer 3 4 num_pipeline = Pipeline([ 5 ("select_numeric", DataFrameSelector(["Age", "SibSp", "Parch", "Fare"])), 6 ("imputer", Imputer(strategy="median")), 7 ]) 8 9 class MostFrequentImputer(BaseEstimator, TransformerMixin): 10 def fit(self, X, y=None): 11 self.most_frequent_ = pd.Series([X[c].value_counts().index[0] for c in X], 12 index=X.columns) 13 return self 14 def transform(self, X, y=None): 15 return X.fillna(self.most_frequent_) 16 17 from future_encoders import OneHotEncoder 18 cat_pipeline = Pipeline([ 19 ("select_cat", DataFrameSelector(["Pclass", "Sex", "Embarked"])), 20 ("imputer", MostFrequentImputer()), 21 ("cat_encoder", OneHotEncoder(sparse=False)), 22 ]) 23 24 cat_pipeline.fit_transform(train_data)
标签:selector tran median nic false learn wrap cto enc
原文地址:https://www.cnblogs.com/xiashiwendao/p/9465344.html