# Возместим отсутствующие данные о тарифе с помощью медианных значений
test [ «Fare»] = test [ «Fare»].fillna (test [ «Fare»].median ())
# Возместим отсутствующие данные о возрасте с помощью медианных значений
test [ «Age»] = test [ «Age»].fillna (test [ «Age»].median ())
# Предварительная обработка
test_encoded_sex = preprocessing.LabelEncoder () test.Sex = test_encoded_sex.fit_transform (test.Sex)
# Извлечем необходимые признаки Pclass, Sex, Age, и Fare из тестового набора данных:
test_features = test [[ «Pclass,» «Sex,» «Age,» «Fare»]].values
print (‘These are the features: \ n’)
print (test_features)
# Составим прогноз, используя тестовый набор данных, и выведем результат
my_prediction = my_tree_one.predict (test_features)
print (‘This is the prediction: \ n’)
print (my_prediction)
# Выведем данные в две колонки: PassengerId и прогноз выживания
PassengerId =np.array (test [ «PassengerId»]). astype (int) my_solution = pd.DataFrame (my_prediction, PassengerId, columns = [ «Survived»])
print (‘This is the solution in toto: \ n’)
print (my_solution)
# Проверим, что у нас 418 строк данных
print (‘This is the solution shape: \ n’)
print (my_solution.shape)
# Записать результаты в. CSV-файл my_solution.csv
my_solution.to_csv (“my_solution_one.csv,” index_label = [“PassengerId”])
А вот и результат:
These are the features:
[[3. 1. 34.5 7.8292]
[3. 0. 47. 7.]
[2. 1. 62. 9.6875] …,
[3. 1. 38.5 7.25]
[3. 1. 27. 8.05]