Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
data["YearRemodAdd"] = make_quantile_bins("YearRemodAdd", 8)
 analyse_categorical("YearRemodAdd" )<categorify>
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_df.info()
data["Exterior1st"].replace(["WdShing"], "Wd Shng", inplace=True)
 data["Exterior2nd"].replace(["CmentBd"], "CemntBd", inplace=True)
 data["Exterior2nd"].replace(["Brk Cmn"], "BrkComm", inplace=True)
 data = expand_categorical(data, ["Exterior1st", "Exterior2nd"] )<data_type_conversions>
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_df.info()
analyse_numeric("MasVnrArea" )<drop_column>
train_df["Age"].isnull().sum()
data.drop("MasVnrArea", axis=1, inplace=True )<concatenate>
test_df["Age"].isnull().sum()
data = expand_categorical(data, ["BsmtFinType1", "BsmtFinType2"] )<data_type_conversions>
data = [train_df, test_df]
for dataset in data:
mean = train_df["Age"].mean()
dataset['Age'] = dataset['Age'].fillna(mean)
 dataset["Age"] = train_df["Age"].astype(float)
 for dataset in data:
dataset.loc[ dataset['Age'] <= 15, 'Age'] = 0
dataset.loc[(dataset['Age'] > 15)&(dataset['Age'] <= 25), 'Age'] = 1
dataset.loc[(dataset['Age'] > 25)&(dataset['Age'] <= 35), 'Age'] = 2
dataset.loc[(dataset['Age'] > 35)&(dataset['Age'] <= 40), 'Age'] = 3
dataset.loc[(dataset['Age'] > 40)&(dataset['Age'] <= 45), 'Age'] = 4
dataset.loc[(dataset['Age'] > 45)&(dataset['Age'] <= 50), 'Age'] = 5
dataset.loc[ dataset['Age'] > 50, 'Age'] = 6
train_df['Age'].value_counts()
analyse_numeric("BsmtFinSF1")
 data["BsmtFinSF1"].fillna(0, inplace=True )<feature_engineering>
train_df.Cabin.value_counts()
data['BsmtFinSF1'] = make_quantile_bins('BsmtFinSF1', 3)
 analyse_categorical("BsmtFinSF1" )<feature_engineering>
deck = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "O": 8}
data = [train_df, test_df]
for dataset in data:
dataset['CabinDeck'] = dataset['Cabin'].fillna("O")
 dataset['CabinDeck'] = dataset['Cabin'].astype(str ).str[0]
dataset['CabinDeck'] = dataset['CabinDeck'].map(deck)
 dataset['CabinDeck'] = dataset['CabinDeck'].fillna(0)
 dataset['CabinDeck'] = dataset['CabinDeck'].astype(int)
 train_df.head(10 )
data["BsmtFinSF2"].fillna(0, inplace=True)
 data['BsmtFinSF2'] = np.where(data["BsmtFinSF2"] == 0, 0, 1)
 analyse_categorical("BsmtFinSF2" )<drop_column>
train_df = train_df.drop(['Cabin'], axis=1)
 test_df.drop(['Cabin'], axis=1, inplace = True )
data.drop('BsmtFinSF2', axis=1, inplace=True )<feature_engineering>
test_df['CabinDeck'].value_counts()
data["BsmtUnfSF"].fillna(0, inplace=True)
 data['BsmtUnfSF'] = np.where(data["BsmtUnfSF"] == 0, 0, 1)
 analyse_categorical("BsmtUnfSF" )<feature_engineering>
train_df.Embarked.value_counts()
data['TotalSF'] = data['TotalBsmtSF'] + data['1stFlrSF'] + data['2ndFlrSF']
analyse_numeric("TotalSF" )<data_type_conversions>
for dataset in data:
dataset['Fare'] = dataset['Fare'].fillna(0)
 for dataset in data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31)&(dataset['Fare'] <= 99), 'Fare'] = 3
dataset.loc[(dataset['Fare'] > 99)&(dataset['Fare'] <= 250), 'Fare'] = 4
dataset.loc[ dataset['Fare'] > 250, 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df['Fare'].value_counts()
analyse_numeric("1stFlrSF" )<data_type_conversions>
train_df.Embarked.value_counts()
analyse_numeric("2ndFlrSF" )<feature_engineering>
train_df.Sex.value_counts()
data['has2ndFlr'] = np.where(data["2ndFlrSF"] > 0, 1, 0)
 analyse_categorical("has2ndFlr" )<feature_engineering>
embarkedMap = {"S": 0, "C": 1, "Q": 2}
genderMap = {"male": 0, "female": 1}
for dataset in data:
dataset['Embarked'] = dataset['Embarked'].map(embarkedMap)
 dataset['Sex'] = dataset['Sex'].map(genderMap )
data["LowQualFinSF"].fillna(0, inplace=True)
 data['LowQualFinSF'] = np.where(data["LowQualFinSF"] == 0, 0, 1)
 analyse_categorical("LowQualFinSF" )<drop_column>
data = [train_df, test_df]
titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in data:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.')
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr',\
'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 dataset['Title'] = dataset['Title'].map(titles)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_df = train_df.drop(['Name'], axis=1)
 test_df = test_df.drop(['Name'], axis=1)
 train_df['Title'].value_counts()
data.drop("LowQualFinSF", axis=1, inplace=True )<data_type_conversions>
train_df['Ticket'].value_counts()
train_df = train_df.drop(['Ticket'], axis=1)
 test_df = test_df.drop(['Ticket'], axis=1 )
analyse_numeric("GrLivArea" )<drop_column>
train_df = train_df.drop(['PassengerId'], axis=1 )
data["BathroomsBasement"] = data["BsmtFullBath"] + 0.5 * data["BsmtHalfBath"]
analyse_categorical("BathroomsBasement")
 data.drop(["BsmtFullBath", "BsmtHalfBath"], axis=1, inplace=True )<drop_column>
X_train = train_df.drop('Survived', axis=1)
 Y_train = train_df['Survived']
data["Bathrooms"] = data["FullBath"] + 0.5 * data["HalfBath"]
analyse_categorical("Bathrooms")
 data.drop(["FullBath", "HalfBath"], axis=1, inplace=True )<drop_column>
X_test = test_df.drop("PassengerId", axis=1 ).copy()
X_test.head(10 )
data.drop("KitchenAbvGr", axis=1, inplace=True )<data_type_conversions>
clf_dt = DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='entropy',
max_depth=None, max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=10, min_samples_split=35,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=None, splitter='best')
 clf_dt.fit(X_train, Y_train)
 Y_pred = clf_dt.predict(X_test)
 scores = cross_val_score(clf_dt, X_train, Y_train, cv=10, scoring = "accuracy")
 print("Scores:", scores)
 print("Mean:", scores.mean())
 print("Standard Deviation:", scores.std() )
analyse_numeric("GarageYrBlt" )<feature_engineering>
output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': Y_pred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )