text_html_output_1.png
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import StandardScaler,MinMaxScaler

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

df = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')

X = df.drop('Transported', 1)
y = df['Transported'].apply(int)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

def trans(df):
    df['Age'].fillna(df.Age.mean(), inplace=True)
    df['RoomService'].fillna(df.RoomService.mean(), inplace=True)
    df['FoodCourt'].fillna(df.FoodCourt.mean(), inplace=True)
    df['ShoppingMall'].fillna(df.ShoppingMall.mean(), inplace=True)
    df['Spa'].fillna(df.Spa.mean(), inplace=True)
    df['VRDeck'].fillna(df.VRDeck.mean(), inplace=True)
    df['HomePlanet'].fillna(df['HomePlanet'].mode()[0], inplace=True)
    df['Destination'].fillna(df['Destination'].mode()[0], inplace=True)
    df['CryoSleep'].fillna(df['CryoSleep'].mode()[0], inplace=True)
    df['VIP'].fillna(df['VIP'].mode()[0], inplace=True)
    df['Cabin'].fillna(df['Cabin'].mode()[0], inplace=True)
    df['CryoSleep'] = df['CryoSleep'].apply(int)
    df[['Deck', 'Num', 'Side']] = df['Cabin'].str.split('/', expand=True)
    df['Num'] = df['Num'].astype(int)
    return df

dfz = trans(X_train)

cat = ['HomePlanet', 'Deck', 'Side', 'Destination']
ohe = OneHotEncoder(drop='first')
enc_df = pd.DataFrame(ohe.fit_transform(dfz[cat]).toarray(), columns=ohe.get_feature_names_out())
dfz = dfz.reset_index(drop=True).join(enc_df)
dfz.drop(['PassengerId', 'Name', 'VIP', 'Cabin', 'HomePlanet', 'Deck', 'Side', 'Destination'], axis=1, inplace=True)
num = ['Age', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']
mmc = MinMaxScaler()
dfz[num] = mmc.fit_transform(dfz[num])

dfz = trans(X_test)

cat = ['HomePlanet', 'Deck', 'Side', 'Destination']
enc_df = pd.DataFrame(ohe.transform(dfz[cat]).toarray(), columns=ohe.get_feature_names_out())
dfz = dfz.reset_index(drop=True).join(enc_df)
dfz.drop(['PassengerId', 'Name', 'VIP', 'Cabin', 'HomePlanet', 'Deck', 'Side', 'Destination'], axis=1, inplace=True)
num = ['Age', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']
dfz[num] = mmc.transform(dfz[num])

df = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')
df.head()