我們今天一口氣跑最簡流程,從讀取鐵達尼號數據、到上傳預測結果到Kaggle,然後明天再來一一解說
最簡流程
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,取得預測正確率
最簡流程就有約76%的正確率!