Kaggle competition: Titanic - Machine Learning from Disaster
data["ScreenPorch"] = np.where(data["ScreenPorch"] == 0, 0, 1)
 analyse_categorical("ScreenPorch" )<feature_engineering>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
data["PoolArea"] = np.where(data["PoolArea"] == 0, 0, 1)
 analyse_categorical("PoolArea" )<drop_column>
Sib = train_data.groupby(['SibSp','Survived'])
 Sibf = pd.DataFrame(Sib.count() ['PassengerId'])
 print(Sibf)
 Par = train_data.groupby(['Parch','Survived'])
 Parf = pd.DataFrame(Par.count() ['PassengerId'])
 print(Parf )
data.drop("PoolQC", axis=1, inplace=True )<drop_column>
train_data.Cabin.value_counts()
data.drop("MiscVal", axis=1, inplace=True )<drop_column>
train_data['Embarked'] = train_data['Embarked'].fillna('S' )
data.drop("MoSold", axis=1, inplace=True )<drop_column>
train_data['Title'] = train_data['Name'].map(lambda x:x.split(',')[1].split('.')[0].strip())
 train_data['Title'].value_counts()
data.drop("YrSold", axis=1, inplace=True )<categorify>
TitleDict={}
TitleDict['Mr']='Mr'
TitleDict['Mlle']='Miss'
TitleDict['Miss']='Miss'
TitleDict['Master']='Master'
TitleDict['Jonkheer']='Master'
TitleDict['Mme']='Mrs'
TitleDict['Ms']='Mrs'
TitleDict['Mrs']='Mrs'
TitleDict['Don']='Royalty'
TitleDict['Sir']='Royalty'
TitleDict['the Countess']='Royalty'
TitleDict['Dona']='Royalty'
TitleDict['Lady']='Royalty'
TitleDict['Capt']='Officer'
TitleDict['Col']='Officer'
TitleDict['Major']='Officer'
TitleDict['Dr']='Officer'
TitleDict['Rev']='Officer'
train_data['Title']=train_data['Title'].map(TitleDict)
 train_data['Title'].value_counts()
def generate_dummies(data):
features = data.columns[data.dtypes == "object"].to_list()
data = pd.concat([data, pd.get_dummies(data[features])], axis=1)
 data.drop(features, axis=1, inplace=True)
 return data<categorify>
def fare_category(fare):
if fare <= 4:
return 0
elif fare <= 10:
return 1
elif fare <= 30:
return 2
elif fare <= 45:
return 3
else:
return 4
train_data['Fare_Category'] = train_data['Fare'].map(fare_category )
data = generate_dummies(data)
 data.head(1 )<prepare_x_and_y>
Ticket_Count = train_data.groupby('Ticket', as_index = False)['PassengerId'].count()
Ticket_Count_0 = Ticket_Count[Ticket_Count.PassengerId == 1]['Ticket']
train_data['GroupTicket'] = np.where(train_data.Ticket.isin(Ticket_Count_0), 0, 1)
X_train = data.loc[data_train.index].drop("SalePrice", axis=1)
 y_train = data.loc[data_train.index, "SalePrice"]
X_test = data.loc[data_test.index].drop("SalePrice", axis=1)
 cv=KFold(10, shuffle=True, random_state=random_state )<train_on_grid>
def file_missing(document):
age_features = ['Age','Fare','Parch','SibSp','Pclass', 'Title']
age_features_dummies = pd.get_dummies(document[age_features])
 known_age = age_features_dummies[age_features_dummies.Age.notnull() ].values
unknown_age = age_features_dummies[age_features_dummies.Age.isnull() ].values
X = known_age[:, 1:]
y = known_age[:, 0]
model = RandomForestRegressor(random_state = 0, n_estimators = 2000, n_jobs = -1)
 model.fit(X,y)
 prediction_age = model.predict(unknown_age[:, 1::])
 document.loc[(document.Age.isnull()), 'Age' ] = prediction_age
return document, model
def Cabin_type(document):
document.loc[(document.Cabin.notnull()), 'Cabin' ] = "Yes"
document.loc[(document.Cabin.isnull()), 'Cabin' ] = "No"
return document
train_data, model = file_missing(train_data)
 train_data = Cabin_type(train_data)
 train_data.head(10)
model = KNeighborsRegressor()
pipe = Pipeline([("scaler", RobustScaler()),("knn", model)])
 params = {
'knn__n_neighbors': [3, 5, 7, 8, 9, 10, 11, 15, 20,],
'knn__weights': ['uniform', 'distance'],
}
knn_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, n_jobs=-1, scoring='neg_root_mean_squared_error')
 knn_grid.fit(X_train, y_train)
 train_score = knn_grid.score(X_train, y_train )<find_best_params>
get_features = ["Pclass", "Sex","Age", "Cabin", "Fare", "Embarked","Title", "Familysize", 'GroupTicket','Fare_Category' ]
get_features_dummies = pd.get_dummies(train_data[get_features])
 print(get_features_dummies)
MODEL_0 = knn_grid.best_estimator_
print(f"Model train / CV score is {train_score:.5f} / {knn_grid.best_score_:.5f}")
<save_to_csv>
scaler = preprocessing.StandardScaler()
age_scale = scaler.fit(get_features_dummies['Age'].values.reshape(-1,1))
get_features_dummies['Age_scaled'] = scaler.fit_transform(get_features_dummies['Age'].values.reshape(-1,1), age_scale)
 fare_scale = scaler.fit(get_features_dummies['Fare'].values.reshape(-1,1))
get_features_dummies['Fare_scaled'] = scaler.fit_transform(get_features_dummies['Fare'].values.reshape(-1,1), fare_scale)
 get_features_dummies.head()
prediction_0 = MODEL_0.predict(X_test)
 prediction_0 = np.e ** prediction_0
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_0})
 submit.to_csv("MODEL_0.csv",index=False )<train_on_grid>
train_df = get_features_dummies.filter(regex='Age_.*|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*|Title_.*|Familysize|GroupTicket|Fare_Category')
 X = train_df.values
y = train_data.Survived
train_X, val_X, train_y, val_y = train_test_split(X, y,test_size=0.2, random_state = 1)
 rf_est = ensemble.RandomForestClassifier(n_estimators = 30, criterion = 'gini', max_features = 'sqrt',
max_depth = 8, min_samples_split = 10, min_samples_leaf = 1,
n_jobs = 50, random_state = 42, verbose = 1)
 gbm_est = ensemble.GradientBoostingClassifier(n_estimators=900, learning_rate=0.0008, loss='exponential',
min_samples_split=3, min_samples_leaf=2, max_features='sqrt',
max_depth=3, random_state=42, verbose=1)
 et_est = ensemble.ExtraTreesClassifier(n_estimators=750, max_features='sqrt', max_depth=35, n_jobs=50,
criterion='entropy', random_state=42, verbose=1)
 voting_est = ensemble.VotingClassifier(estimators = [('rf', rf_est),('gbm', gbm_est),('et', et_est)],
voting = 'soft', weights = [3,5,2],
n_jobs = 50)
 voting_est.fit(train_X, train_y)
 voting_est_predictions = voting_est.predict(val_X)
 voting_est_mae = mean_absolute_error(voting_est_predictions, val_y)
 print(voting_est_mae )
model = Ridge()
pipe = Pipeline([("scaler", RobustScaler()),("ridge", model)])
 params = {
'ridge__alpha': [0.01, 0.1, 1, 10, 100, 1000]
}
ridge_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, n_jobs=-1, scoring='neg_root_mean_squared_error')
 ridge_grid.fit(X_train, y_train)
 train_score = ridge_grid.score(X_train, y_train )<find_best_params>
forest = RandomForestClassifier()
cross_validation = StratifiedKFold(n_splits=10)
 parameter_grid = {
'max_depth' : [8],
'n_estimators': [30],
'max_features': ['sqrt'],
'min_samples_split': [10],
'min_samples_leaf':[1],
'bootstrap': [True],
}
grid_search = GridSearchCV(forest,
scoring='accuracy',
param_grid=parameter_grid,
cv=cross_validation,
verbose=1 
)
 grid_search.fit(train_X, train_y)
 grid_predictions = grid_search.predict(val_X)
 grid_mae = mean_absolute_error(grid_predictions, val_y)
 print(grid_mae )
MODEL_1 = ridge_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {ridge_grid.best_score_:.5f}")
<save_to_csv>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
prediction_1 = MODEL_1.predict(X_test)
 prediction_1 = np.e ** prediction_1
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_1})
 submit.to_csv("MODEL_1.csv",index=False )<train_on_grid>
test_data['Title'] = test_data['Name'].map(lambda x:x.split(',')[1].split('.')[0].strip())
 test_data['Title']=test_data['Title'].map(TitleDict)
 test_data['Title'].value_counts()
model = Lasso(max_iter=100000)
 pipe = Pipeline([("scaler", MinMaxScaler()),("lasso", model)])
 params = {
'lasso__alpha': [0.00001, 0.0001, 0.001, 0.01, 0.1]
}
lasso_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, n_jobs=-1, scoring='neg_root_mean_squared_error')
 lasso_grid.fit(X_train, y_train)
 train_score = lasso_grid.score(X_train, y_train )<find_best_params>
test_data, model = file_missing(test_data )
MODEL_2 = lasso_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {lasso_grid.best_score_:.5f}")
<save_to_csv>
test_data['Familynum']=test_data['Parch']+test_data['SibSp']+1
test_data['Familysize'] = test_data['Familynum'].map(Family_size )
prediction_2 = MODEL_2.predict(X_test)
 prediction_2 = np.e ** prediction_2
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_2})
 submit.to_csv("MODEL_2.csv",index=False )<train_on_grid>
Ticket_Count = test_data.groupby('Ticket', as_index = False)['PassengerId'].count()
Ticket_Count_0 = Ticket_Count[Ticket_Count.PassengerId == 1]['Ticket']
test_data['GroupTicket'] = np.where(test_data.Ticket.isin(Ticket_Count_0), 0, 1 )
model = ElasticNet(max_iter=100000)
 pipe = Pipeline([("scaler", MinMaxScaler()),("elastic", model)])
 params = {
'elastic__alpha': [0.0001, 0.001, 0.01, 0.1],
'elastic__l1_ratio': [0.001, 0.01, 0.1, 0.5, 1, 10]
}
elastic_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, n_jobs=-1, scoring='neg_root_mean_squared_error')
 elastic_grid.fit(X_train, y_train)
 train_score = elastic_grid.score(X_train, y_train )<find_best_params>
test_data['Fare_Category'] = test_data['Fare'].map(fare_category )
MODEL_3 = elastic_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {elastic_grid.best_score_:.5f}")
<save_to_csv>
test_features_dummies = pd.get_dummies(test_data[get_features])
 test_features_dummies['Age_scaled'] = scaler.fit_transform(test_features_dummies['Age'].values.reshape(-1,1), age_scale)
 test_features_dummies['Fare_scaled'] = scaler.fit_transform(test_features_dummies['Fare'].values.reshape(-1,1), fare_scale)
 test_features_dummies.head()
<train_on_grid><EOS> 
test = test_features_dummies.filter(regex='Age_.*|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*|Title_.*|Familysize|GroupTicket|Fare_Category')
 predictions = grid_search.predict(test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions},)
 output = output.round(0 ).astype(int)
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!")
 print(output )