sklearn的pipeline的简单使用

基础模型:tfidf+Ridge
1.在不使用pipeline的情况下,模型是这样的:

# 模型定义与训练
tf = TfidfVectorizer(min_df= 3, max_df=0.5, analyzer = 'char_wb', ngram_range = (3,5))
tv_fit = tf.fit_transform(df['text'])
rf = Ridge()
rf.fit(tv_fit,df['y'])
# 模型测试
x1 = tf.transform(df_val['less_toxic'])
x2 = tf.transform(df_val['more_toxic'])
p1 = rf.predict(x1)
p2 = rf.predict(x2)
print(f'Validation Accuracy is { np.round((p1 < p2).mean() * 100,2)}')

输出:
Validation Accuracy is 68.45
2.在使用pipeline的情况下:

pipeline = Pipeline(
    [
        ("vect", TfidfVectorizer(min_df= 3, max_df=0.5, analyzer = 'char_wb', ngram_range = (3,5))),
        #("clf", RandomForestRegressor(n_estimators = 5, min_sample_leaf=3)),
        ("clf", Ridge()),
        #("clf",LinearRegression())
    ]
)
# Train the pipeline
pipeline.fit(df['text'], df['y'])
df_val = pd.read_csv("../input/jigsaw-toxic-severity-rating/validation_data.csv")
p1 = pipeline.predict(df_val['less_toxic'])
p2 = pipeline.predict(df_val['more_toxic'])
print(f'Validation Accuracy is { np.round((p1 < p2).mean() * 100,2)}')

输出:
Validation Accuracy is 68.45

推荐内容:https://blog.csdn.net/lanchunhui/article/details/50521648

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值