kaggle Pipelines

本文介绍了使用sklearn进行数据预处理与模型预测的基本流程。通过构建包含Imputer(缺失值填充)和RandomForestRegressor(随机森林回归)的管道,实现了从数据读取到训练模型并进行预测的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# Most scikit-learn objects are either transformers or models.

  # Transformers are for pre-processing before modeling. The Imputer class (for filling in missing values) is an example of a transformer. # Over time, you will learn many more transformers, and you will frequently use multiple transformers sequentially.

  # Models are used to make predictions. You will usually preprocess your data (with transformers) before putting it in a model.

  # You can tell if an object is a transformer or a model by how you apply it. After fitting a transformer, you apply it with the transform # command. After fitting a model, you apply it with the predict command. Your pipeline must start with transformer steps and end with a # model. This is what you'd want anyway.

  # Eventually you will want to apply more transformers and combine them more flexibly. We will cover this later in an Advanced Pipelines # tutorial.

 

import pandas as pd
from sklearn.model_selection import train_test_split

# Read Data
data = pd.read_csv('../input/melb_data.csv')
cols_to_use = ['Rooms', 'Distance', 'Landsize', 'BuildingArea', 'YearBuilt']
X = data[cols_to_use]
y = data.Price
train_X, test_X, train_y, test_y = train_test_split(X, y)

from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Imputer

my_pipeline = make_pipeline(Imputer(), RandomForestRegressor())
my_pipeline.fit(train_X, train_y)
predictions = my_pipeline.predict(test_X)

 

转载于:https://www.cnblogs.com/cbattle/p/8830864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值