但是我們需要提交一個(gè)答案到比賽中。我們需要在測(cè)試???據(jù)上采取一個(gè)和訓(xùn)練數(shù)據(jù)上一樣的校準(zhǔn)的步驟來完成這個(gè)事情。如果我們沒有校準(zhǔn)操作,我們就不能夠利用它做出有效的預(yù)測(cè)。
這些操作要在我們對(duì)列所有的改變之前進(jìn)行。
處理 titanic_test 和處理 titanic 用同樣的方法。
這涉及到:
使用訓(xùn)練數(shù)據(jù)集中的年齡中位數(shù)填充“Age”列的的所有缺失值。訓(xùn)練數(shù)據(jù)集的年齡已經(jīng)使用完全相同的值替換了缺失年齡(因?yàn)橹狄呀?jīng)不同了,所以這不能作為測(cè)試集的中位數(shù))。你應(yīng)該用 titanic['Age'].median() 去找到中位數(shù)。
將 Sex 列中的所有 male 換成 0 ,所有 female 換成 1 。
用 S 填充 Embarked 列中的所有缺失值。
將 Embarked 列中的 S 換成 0 , C 換成 1 , Q 換成 2 。
我們也需要將 Fare 列的缺失值替換掉。用 .fillna 將測(cè)試集中的缺失值用中位數(shù)替換掉。訓(xùn)練集中的 Fare 列沒有缺失值了,但是測(cè)試集有時(shí)會(huì)不同。
titanic_test = pandas.read_csv("titanic_test.csv")
titanic_test['Age'] = titanic_test['Age'].fillna(titanic_test['Age'].median())
titanic_test['Fare'] = titanic_test['Fare'].fillna(titanic_test['Fare'].median())
titanic_test.loc[titanic_test['Sex'] == 'male','Sex'] = 0
titanic_test.loc[titanic_test['Sex'] == 'female','Sex'] = 1
titanic_test['Embarked'] = titanic_test['Embarked'].fillna('S')
titanic_test.loc[titanic_test['Embarked'] == 'S', 'Embarked'] = 0
titanic_test.loc[titanic_test['Embarked'] == 'C', 'Embarked'] = 1
titanic_test.loc[titanic_test['Embarked'] == 'Q', 'Embarked'] = 2
13:生成一個(gè)提交文件
現(xiàn)在我們有了我們需要生成一個(gè)比賽提交答案的所有東西了。
首先,我們必須先在訓(xùn)練數(shù)據(jù)上訓(xùn)練一個(gè)算法。然后我們?cè)跍y(cè)試數(shù)據(jù)集上做一個(gè)預(yù)測(cè)。最后,我們生成一個(gè)包含預(yù)測(cè)和乘客id的csv文件。
一旦你完成了下面步驟的代碼,你就能用 submission.to_csv('kaggle.csv, index=False') 輸出一個(gè)csv結(jié)果。這將會(huì)給你的第一個(gè)條件所有需要的東西——雖然這沒有給你很好的準(zhǔn)確率。(~0.75)
# Initialize the algorithm class
alg = LogisticRegression(random_state=1)
# Train the algorithm using all the training data
alg.fit(titanic[predictors], titanic["Survived"])
# Make predictions using the test set.
predictions = alg.predict(titanic_test[predictors])
# Create a new dataframe with only the columns Kaggle wants from the dataset.
submission = pandas.DataFrame({
"PassengerId": titanic_test["PassengerId"],
"Survived": predictions
})
14:下一步
我們只是生成了一個(gè)調(diào)教文件,但是準(zhǔn)確率不是很好(0.75)。由于我們的預(yù)測(cè)在不同的數(shù)據(jù)集上導(dǎo)致在測(cè)試集上的分?jǐn)?shù)低于我們?cè)诮徊骝?yàn)證上的分?jǐn)?shù)。在下一個(gè)任務(wù)中我們將學(xué)習(xí)如何生成更好的特征和使用更好的模型來提高我們的分?jǐn)?shù)。
恭喜你完成了這個(gè)任務(wù)!
結(jié)束!