これを動かしたらそれなりの予測精度にはなるはず。
もうちょっとブラッシュアップしてみようと思う。
from sklearn.ensemble import RandomForestClassifier
# データの取り込みと中身の確認
train_data = pd.read_csv("../input/titanic/train.csv")
test_data = pd.read_csv("../input/titanic/test.csv")
# 欠損値の処理
train_data['Age'].fillna(train_data['Age'].median(), inplace=True)
train_data['Embarked'].fillna(train_data['Embarked'].mode(), inplace=True)
test_data['Age'].fillna(test_data['Age'].median(), inplace=True)
test_data['Fare'].fillna(test_data.groupby('Pclass')['Fare'].median()[3], inplace=True)
# データの準備
y_train = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", 'Embarked']
X_train = pd.get_dummies(train_data[features])
X_test = pd.get_dummies(test_data[features])
# 訓練用データの特徴量エンジニアリング
X_train['Young'] = np.where(train_data['Age'] < 15, 1, 0)
X_train['Old'] = np.where(train_data['Age'] >= 65, 1, 0)
X_train['Family'] = train_data['SibSp'] + train_data['Parch']
X_train['Alone'] = np.where(X_train['Family'] == 0, 1, 0)
X_train['Fare'] = (train_data['Fare'] - train_data['Fare'].min()) / (train_data['Fare'].max() - train_data['Fare'].min())
# テスト用データの特徴量エンジニアリング
X_test['Young'] = np.where(test_data['Age'] < 15, 1, 0)
X_test['Old'] = np.where(test_data['Age'] >= 65, 1, 0)
X_test['Family'] = test_data['SibSp'] + test_data['Parch']
X_test['Alone'] = np.where(X_test['Family'] == 0, 1, 0)
X_test['Fare'] = (test_data['Fare'] - test_data['Fare'].min()) / (test_data['Fare'].max() - test_data['Fare'].min())
# モデルの作成と当てはめ
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# 提出用データの保存
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")