text_html_output_1.png
from sklearn import metrics

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train_filename = '/kaggle/input/spaceship-titanic/train.csv'
test_filename = '/kaggle/input/spaceship-titanic/test.csv'

df_train = pd.read_csv(train_filename)
df_test = pd.read_csv(test_filename)
df_test_original = df_test.copy()
df_train

def print_unique(df):
    for col in df:
        unique = df[col].unique()
        max_len = 6
        if len(unique) <= max_len:
            unique = [f'{u}({(df[col] == u).sum()})' for u in unique]
        if len(unique) > max_len:
            unique = list(unique[:max_len]) + [f'...({len(unique) - max_len})']
df_train_Y = df_train[['Transported']]
good_columns = ['CryoSleep', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']
df_train_X_good_cols = df_train[good_columns]
df_test_X_good_cols = df_test[good_columns]

df_Y = df_train_Y.copy()
df = df_train_X_good_cols.copy()
df_test = df_test_X_good_cols.copy()
df[['CryoSleep']] = df[['CryoSleep']].fillna(value=df[['CryoSleep']].mode().iloc[0, 0])
df_test[['CryoSleep']] = df_test[['CryoSleep']].fillna(value=df_test[['CryoSleep']].mode().iloc[0, 0])
num_cols = ['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']
df[num_cols] = df[num_cols].fillna(value=df[num_cols].median())
df_test[num_cols] = df_test[num_cols].fillna(value=df_test[num_cols].median())
df['CryoSleep'] = df['CryoSleep'].astype(int)
df_test['CryoSleep'] = df_test['CryoSleep'].astype(int)
df_Y['Transported'] = df_Y['Transported'].astype(int)

X_train, X_test, y_train, y_test = train_test_split(df.to_numpy(), df_Y.to_numpy(), test_size=0.1)
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=1000)
classifier.fit(X_train, y_train.ravel())
y_pred = classifier.predict(X_test)

importances = classifier.feature_importances_
std = np.std([tree.feature_importances_ for tree in classifier.estimators_], axis=0)

forest_importances = pd.Series(importances, index=good_columns)

fig, ax = plt.subplots()
forest_importances.plot.bar(yerr=std, ax=ax)
ax.set_title("Feature importances using MDI")
ax.set_ylabel("Mean decrease in impurity")
fig.tight_layout()

test_pred = classifier.predict(df_test.to_numpy())

df_test_df = pd.DataFrame(test_pred)

a = df_test_original[['PassengerId']]

b = df_test_df.astype(bool)

pd.concat([a, b])

final_result = pd.concat([a, b], ignore_index=True, axis=1)
final_result.columns = ['PassengerId', 'Transported']
final_result