Kaggle|Courses|Pipelines

管道機(jī)制。
管道捆綁了 預(yù)處理 和 建模 的步驟疲眷,可以使代碼更簡單和井井有條灾挨。雖然有一些數(shù)據(jù)科學(xué)家不使用管道,但是使用管道有一些重要的好處:
-更整潔的代碼:在預(yù)處理的每個(gè)步驟中都要考慮數(shù)據(jù)會(huì)很混亂饿序。使用管道則無需在每個(gè)步驟中手動(dòng)跟蹤
-易于產(chǎn)出:很難將模型從原型過渡到可大規(guī)模部署的模型。在這里我們不會(huì)涉及許多相關(guān)問題羹蚣,但是管道可以提供幫助原探。
-更多模型驗(yàn)證方法:交叉驗(yàn)證等
Step 1: Define Preprocessing Steps
Similar to how a pipeline bundles together preprocessing and modeling steps, we use the ColumnTransformer class to bundle together different preprocessing steps.
The code below:

-imputes missing values in numerical data, and

-imputes missing values and applies a one-hot encoding to categorical data.

from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder

# Preprocessing for numerical data
numerical_transformer = SimpleImputer(strategy='constant')

# Preprocessing for categorical data
categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent')),
    ('onehot', OneHotEncoder(handle_unknown='ignore'))
])

# Bundle preprocessing for numerical and categorical data
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numerical_transformer, numerical_cols),
        ('cat', categorical_transformer, categorical_cols)
    ])

Step 2: Define the Model

Next, we define a random forest model with the familiar RandomForestRegressor class.

from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor(n_estimators=100, random_state=0)

Step 3: Create and Evaluate the Pipeline

Finally, we use the Pipeline class to define a pipeline that bundles the preprocessing and modeling steps. There are a few important things to notice:

  • With the pipeline, we preprocess the training data and fit the model in a single line of code. (In contrast, without a pipeline, we have to do imputation, one-hot encoding, and model training in separate steps. This becomes especially messy if we have to deal with both numerical and categorical variables!)
  • With the pipeline, we supply the unprocessed features in X_valid to the predict() command, and the pipeline automatically preprocesses the features before generating predictions. (However, without a pipeline, we have to remember to preprocess the validation data before making predictions.)
from sklearn.metrics import mean_absolute_error

# Bundle preprocessing and modeling code in a pipeline
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                              ('model', model)
                             ])

# Preprocessing of training data, fit model 
my_pipeline.fit(X_train, y_train)

# Preprocessing of validation data, get predictions
preds = my_pipeline.predict(X_valid)

# Evaluate the model
score = mean_absolute_error(y_valid, preds)
print('MAE:', score)


MAE: 160679.18917034855

Conclusion

Pipelines are valuable for cleaning up machine learning code and avoiding errors, and are especially useful for workflows with sophisticated data preprocessing.

Your Turn

Use a pipeline in the next exercise to use advanced data preprocessing techniques and improve your predictions!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市顽素,隨后出現(xiàn)的幾起案子咽弦,更是在濱河造成了極大的恐慌,老刑警劉巖胁出,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件型型,死亡現(xiàn)場離奇詭異,居然都是意外死亡全蝶,警方通過查閱死者的電腦和手機(jī)闹蒜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抑淫,“玉大人绷落,你說我怎么就攤上這事∈嘉” “怎么了嘱函?”我有些...
    開封第一講書人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長埂蕊。 經(jīng)常有香客問我往弓,道長,這世上最難降的妖魔是什么蓄氧? 我笑而不...
    開封第一講書人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任函似,我火速辦了婚禮,結(jié)果婚禮上喉童,老公的妹妹穿的比我還像新娘撇寞。我一直安慰自己,他們只是感情好堂氯,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開白布蔑担。 她就那樣靜靜地躺著,像睡著了一般咽白。 火紅的嫁衣襯著肌膚如雪啤握。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評(píng)論 1 291
  • 那天晶框,我揣著相機(jī)與錄音排抬,去河邊找鬼。 笑死授段,一個(gè)胖子當(dāng)著我的面吹牛蹲蒲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侵贵,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼届搁,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了窍育?” 一聲冷哼從身側(cè)響起卡睦,我...
    開封第一講書人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蔫骂,沒想到半個(gè)月后么翰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辽旋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年浩嫌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片补胚。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡码耐,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出溶其,到底是詐尸還是另有隱情骚腥,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布瓶逃,位于F島的核電站束铭,受9級(jí)特大地震影響廓块,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜契沫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一带猴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧懈万,春花似錦拴清、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涕侈,卻和暖如春沪停,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背驾凶。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來泰國打工牙甫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人调违。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓窟哺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親技肩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子且轨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容