Wenn Sie dies verschieben, sollte dies eine angemessene Vorhersagegenauigkeit sein. Ich werde versuchen, es ein bisschen mehr aufzufrischen.
from sklearn.ensemble import RandomForestClassifier
#Daten importieren und Inhalte überprüfen
train_data = pd.read_csv("../input/titanic/train.csv")
test_data = pd.read_csv("../input/titanic/test.csv")
#Umgang mit fehlenden Werten
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)
#Datenaufbereitung
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])
#Feature Quantity Engineering von Trainingsdaten
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())
#Feature Engineering von Testdaten
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())
#Modellierung und Anpassung
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
#Speichern von Daten zur Übermittlung
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")
Recommended Posts