iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 26
0
AI & Data

AI+Line系列 第 26

Day26 大數據(5):最簡流程

我們今天一口氣跑最簡流程,從讀取鐵達尼號數據、到上傳預測結果到Kaggle,然後明天再來一一解說

https://imgur.com/N2pv6Fn.png

最簡流程

df_train = pd.read_csv('./data/' + 'titanic_train.csv')
df_test = pd.read_csv('./data/' + 'titanic_test.csv')

Y_train = df_train['Survived']
df_train = df_train.drop(['Survived'] , axis=1) # 移除欄位

ids = df_test['PassengerId']
df_train = df_train.drop(['PassengerId'] , axis=1) # 移除欄位
df_test = df_test.drop(['PassengerId'] , axis=1) # 移除欄位

df = pd.concat([df_train,df_test])
df.head()

LEncoder = LabelEncoder()
MMEncoder = MinMaxScaler()
for c in df.columns:
    df[c] = df[c].fillna(-1)
    if df[c].dtype == 'object':
        print(c)
        df[c] = LEncoder.fit_transform(list(df[c].values))
    df[c] = MMEncoder.fit_transform(df[c].values.reshape(-1, 1))
df.head()

train_num = Y_train.shape[0]
X_train = df[:train_num]
X_test = df[train_num:]

model = GradientBoostingClassifier()
model.fit(X_train, Y_train)

importance = pd.Series(data=model.feature_importances_, index=X_train.columns)
importance = importance.sort_values(ascending=False)
print(importance)

pred = model.predict(X_test)
sub = pd.DataFrame({'PassengerId': ids, 'Survived': pred})
sub.head()

sub.to_csv('titanic_baseline.csv', index=False) 

將預測結果titanic_baseline.csv檔案傳到Kaggle,取得預測正確率
https://imgur.com/v670art.png

最簡流程就有約76%的正確率!
https://imgur.com/CxJ4mCA.png


上一篇
Day25 大數據(4):觀察數據
下一篇
Day27 大數據(6):最簡流程說明
系列文
AI+Line30

尚未有邦友留言

立即登入留言