iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
AI & Data

使用python學習Machine Learning系列 第 8

Day 8 [Python ML、特徵工程] 基準模型(Baseline Model)

  • 分享至 

  • xImage
  •  

前言

今天開始是新的章節,因此也有新的資料集
Kickstarter Projects

在開始之前要先將資料集下載好丟到Dataset資料夾中

讀取資料

import pandas as pd
ks = pd.read_csv('./ks-projects-201801.csv',
                 parse_dates=['deadline', 'launched'])
ks.head(6)

取得某一行資料的狀態

# 取得某一個資料的全部狀態
print('Unique values in `state` column:', list(ks.state.unique()))
Unique values in `state` column: ['failed', 'canceled', 'successful', 'live', 'undefined', 'suspended']

利用query指令去掉狀態中的live

ks = ks.query('state!="live"')
print(list(ks.state.unique()))
['failed', 'canceled', 'successful', 'undefined', 'suspended']

在state中,將successful標註為1,其餘的都標註為0

feature = ['state', 'outcome']
# Add outcome column, "successful" == 1, others are 0
ks = ks.assign(outcome=(ks['state'] == 'successful').astype(int))
ks[feature].head(6)

將時間資料轉換成hour day month year並加入原始資料中

ks = ks.assign(hour=ks.launched.dt.hour,
               day=ks.launched.dt.day,
               month=ks.launched.dt.month,
               year=ks.launched.dt.year)

利用LabelEncoder處理類別屬性資料

from sklearn.preprocessing import LabelEncoder

cat_features = ['category', 'currency', 'country']
encoder = LabelEncoder()

# 將labelencoder應用到每一個column中
encoded = ks[cat_features].apply(encoder.fit_transform)

將處理好的類別資料加入以下欄位中

data = ks[['goal', 'hour', 'day', 'month', 'year', 'outcome']].join(encoded)
data.head()

將資料分割為訓練(train)、驗證(valid)、測試(test)

valid_fraction = 0.1
valid_size = int(len(data) * valid_fraction)

train = data[:-2 * valid_size]
valid = data[-2 * valid_size:-valid_size]
test = data[-valid_size:]

lightGBM model

import lightgbm as lgb

feature_cols = train.columns.drop('outcome')

dtrain = lgb.Dataset(train[feature_cols], label=train['outcome'])
dvalid = lgb.Dataset(valid[feature_cols], label=valid['outcome'])

param = {'num_leaves': 64, 'objective': 'binary'}
param['metric'] = 'auc'
num_round = 1000
bst = lgb.train(param, dtrain, num_round, valid_sets=[dvalid], early_stopping_rounds=10, verbose_eval=False)
[LightGBM] [Info] Number of positive: 107340, number of negative: 193350
[LightGBM] [Warning] Auto-choosing row-wise multi-threading, the overhead of testing was 0.008400 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 528
[LightGBM] [Info] Number of data points in the train set: 300690, number of used features: 8
[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.356979 -> initscore=-0.588501
[LightGBM] [Info] Start training from score -0.588501

預測並且評估模型

from sklearn import metrics
ypred = bst.predict(test[feature_cols])
score = metrics.roc_auc_score(test['outcome'], ypred)

print(f"Test AUC score: {score}")
Test AUC score: 0.7472160532987071

上一篇
Day 7 [Python ML] Machine Learning的處理流程
下一篇
Day 9 [Python ML、特徵工程] 分類工程
系列文
使用python學習Machine Learning29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言