Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<find_best_params>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
MODEL_4 = svr_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {svr_grid.best_score_:.5f}")
<save_to_csv>
%matplotlib inline
sns.set()
prediction_4 = MODEL_4.predict(X_test)
 prediction_4 = np.e ** prediction_4
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_4})
 submit.to_csv("MODEL_4.csv",index=False )<train_on_grid>
train_test_dataset = [train, test]
for dataset in train_test_dataset:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False )
model = catboost.CatBoostRegressor(random_seed=random_state, verbose=False)
 pipe = Pipeline([("catboost", model)])
 params = {
'catboost__learning_rate':[0.01, 0.1, 0.2],
'catboost__depth': [5, 6]
}
catboost_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, scoring='neg_root_mean_squared_error')
 catboost_grid.fit(X_train, y_train)
 train_score = catboost_grid.score(X_train, y_train )<find_best_params>
title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2,
"Master": 3, "Dr": 3, "Rev": 3, "Col": 3, "Major": 3, "Mlle": 3,"Countess": 3,
"Ms": 3, "Lady": 3, "Jonkheer": 3, "Don": 3, "Dona" : 3, "Mme": 3,"Capt": 3,"Sir": 3 }
for dataset in train_test_dataset:
dataset['Title'] = dataset['Title'].map(title_mapping )
MODEL_5 = catboost_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {catboost_grid.best_score_:.5f}")
<save_to_csv>
train['Title'].value_counts()
prediction_5 = MODEL_5.predict(X_test)
 prediction_5 = np.e ** prediction_5
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_5})
 submit.to_csv("MODEL_5.csv",index=False )<train_on_grid>
train.drop('Name', axis=1, inplace=True)
 test.drop('Name', axis=1, inplace=True)
 train.drop('Embarked', axis=1, inplace=True)
 test.drop('Embarked', axis=1, inplace=True)
 train.drop('Cabin', axis=1, inplace=True)
 test.drop('Cabin', axis=1, inplace=True)
 train.drop('SibSp', axis=1, inplace=True)
 test.drop('SibSp', axis=1, inplace=True)
 train.drop('Parch', axis=1, inplace=True)
 test.drop('Parch', axis=1, inplace=True)
 train.drop('Ticket', axis=1, inplace=True)
 test.drop('Ticket', axis=1, inplace=True )
model = XGBRegressor(n_estimators=1000)
 pipe = Pipeline([("xgb", model)])
 params = {
'xgb__learning_rate': [.01,.02,.03,.05,.07],
'xgb__max_depth': [3, 4, 5]
}
xgb_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, scoring='neg_root_mean_squared_error')
 xgb_grid.fit(X_train, y_train)
 train_score = xgb_grid.score(X_train, y_train )<find_best_params>
sex_mapping = {"male": 0, "female": 1}
for dataset in train_test_dataset:
dataset['Sex'] = dataset['Sex'].map(sex_mapping )
MODEL_6 = xgb_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {xgb_grid.best_score_:.5f}")
<save_to_csv>
train["Age"].fillna(train.groupby("Title")["Age"].transform("median"), inplace=True)
 test["Age"].fillna(test.groupby("Title")["Age"].transform("median"), inplace=True )
prediction_6 = MODEL_6.predict(X_test)
 prediction_6 = np.e ** prediction_6
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_6})
 submit.to_csv("MODEL_6.csv",index=False )<train_on_grid>
for dataset in train_test_dataset:
dataset.loc[ dataset['Age'] <= 12, 'Age'] = 0,
dataset.loc[(dataset['Age'] > 12)&(dataset['Age'] <= 19), 'Age'] = 1,
dataset.loc[(dataset['Age'] > 19)&(dataset['Age'] <= 30), 'Age'] = 2,
dataset.loc[(dataset['Age'] > 30)&(dataset['Age'] <= 45), 'Age'] = 3,
dataset.loc[(dataset['Age'] > 45)&(dataset['Age'] <= 65), 'Age'] = 4,
dataset.loc[ dataset['Age'] > 65, 'Age'] = 5
model = lgb.LGBMRegressor(n_estimators=1000)
 pipe = Pipeline([("lgb", model)])
 params = {
'lgb__num_leaves': [2, 3, 4, 5],
'lgb__max_depth': [3, 4, 5]
}
lgb_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=1, scoring='neg_root_mean_squared_error')
 lgb_grid.fit(X_train, y_train)
 train_score = lgb_grid.score(X_train, y_train )<find_best_params>
train["Fare"].fillna(train.groupby("Pclass")["Fare"].transform("median"), inplace=True)
 test["Fare"].fillna(test.groupby("Pclass")["Fare"].transform("median"), inplace=True )
MODEL_7 = lgb_grid.best_estimator_
print(f"Model CV score is {train_score:.5f} / {lgb_grid.best_score_:.5f}")
<save_to_csv>
for dataset in train_test_dataset:
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, 'Fare'] = 3
prediction_7 = MODEL_7.predict(X_test)
 prediction_7 = np.e ** prediction_7
submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction_7})
 submit.to_csv("MODEL_7.csv",index=False )<save_to_csv>
train = train.drop(['PassengerId'], axis=1)
 train_data = train.drop('Survived', axis=1)
 target = train['Survived']
prediction = np.mean([prediction_1, prediction_2, prediction_5, prediction_6, prediction_7], axis=0)
 submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction})
 submit.to_csv("MODEL_8.csv",index=False )<save_to_csv>
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
import numpy as np
prediction = np.mean([prediction_2, prediction_3, prediction_5, prediction_6, prediction_7], axis=0)
 submit = pd.DataFrame({"Id": data_test.index,"SalePrice": prediction})
 submit.to_csv("MODEL_9.csv",index=False )<save_to_csv>
k_fold = KFold(n_splits=10, shuffle=True, random_state=0 )
def save_file(predictions):

 output = pd.DataFrame({'Id': sample_submission_file.Id,
'SalePrice': predictions})
 output.to_csv('submission.csv', index=False)
 print("Submission file is saved")
 def calculate_root_mean_squared_log_error(y_true, y_pred):

 if len(y_pred)!=len(y_true): return 'error_mismatch'
y_pred_new = [math.log(x+1)for x in y_pred]
y_true_new = [math.log(x+1)for x in y_true]
return mean_squared_error(y_true_new, y_pred_new, squared=False )<load_from_csv>
classifier = KNeighborsClassifier(n_neighbors = 15)
 scoring = 'accuracy'
score = cross_val_score(classifier, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score)
 round(np.mean(score)*100, 2 )
train_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv',
index_col='Id')
 X_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv',
index_col='Id')
 X = train_data.copy()
X.dropna(axis=0, subset=['SalePrice'], inplace=True)
 y = X.SalePrice
X.drop(['SalePrice', 'Utilities'], axis=1, inplace=True)
 X_test.drop(['Utilities'], axis=1, inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,
random_state=0)
 sample_submission_file = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv")
 print('Data is OK' )<count_missing_values>
classifier = DecisionTreeClassifier()
scoring = 'accuracy'
score = cross_val_score(classifier, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 round(np.mean(score)*100, 2 )
categorical_cols = [cname for cname in X_train.columns if X_train[cname].dtype == "object"]
numerical_cols = [cname for cname in X_train.columns if X_train[cname].dtype in ['int64',
'float64']]
missing_val_count_by_column_train =(X_train.isnull().sum())
 print("Number of missing values in each column:")
 print(missing_val_count_by_column_train[missing_val_count_by_column_train > 0] )<count_missing_values>
classifier = RandomForestClassifier(n_estimators=100)
 scoring = 'accuracy'
score = cross_val_score(classifier, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score)
 round(np.mean(score)*100, 2 )
missing_val_count_by_column_numeric =(X_train[numerical_cols].isnull().sum())
 print("Number of missing values in numerical columns:")
 print(missing_val_count_by_column_numeric[missing_val_count_by_column_numeric > 0] )<define_variables>
classifier = RandomForestClassifier()
classifier.fit(train_data, target)
 test_data = test.drop("PassengerId", axis=1 ).copy()
prediction = classifier.predict(test_data )
constant_num_cols = ['GarageYrBlt', 'MasVnrArea']
mean_num_cols = list(set(numerical_cols ).difference(set(constant_num_cols)))
 constant_categorical_cols = ['Alley', 'MasVnrType', 'BsmtQual', 'BsmtCond', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinType2', 'FireplaceQu','GarageType',
'GarageFinish', 'GarageQual', 'GarageCond', 'PoolQC', 'Fence',
'MiscFeature']
mf_categorical_cols = list(set(categorical_cols ).difference(set(constant_categorical_cols)))
 my_cols = constant_num_cols + mean_num_cols + constant_categorical_cols + mf_categorical_cols<categorify>
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": prediction
})
 submission.to_csv('submission.csv', index=False )