Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<count_values>
pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 500)
 df_train = pd.read_csv('.. /input/titanic/train.csv')
 df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_sub = pd.read_csv('.. /input/titanic/gender_submission.csv' )
combine.SaleType.value_counts()<count_values>
df = pd.concat([df_train, df_test],sort=False)
 df.reset_index(drop=True,inplace=True )
combine['SaleType']=combine['SaleType'].fillna('WD')
 combine.SaleType.value_counts()<feature_engineering>
df.isnull().sum()
combine['SalePrice']=combine['SalePrice'].fillna(0)
 combine.SalePrice.isnull().sum()<count_missing_values>
df[df['Fare'].isnull() ]
combine.isnull().sum().sum()<count_values>
df_dropna = df.dropna(subset=['Fare'] )
combine.YrSold.value_counts()<count_values>
df['Pclass'].value_counts()
test_raw.YrSold.value_counts()<feature_engineering>
grouped = df.groupby('Pclass' )
combine['Age']=combine['YrSold']-combine['YearBuilt']
combine['PriceFlux']=2011-combine['YrSold']
combine['Renew']=combine['YrSold']-combine['YearRemodAdd']
combine['MSSubClass']=combine['MSSubClass'].apply(str )<categorify>
age60=df[(df['Age']>60)&(df['Age']<70)]
Label_cols=['MSSubClass','LotShape','LandContour','LandSlope','BldgType','HouseStyle','ExterQual','ExterCond','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','HeatingQC','CentralAir','Electrical','KitchenQual','Functional','FireplaceQu','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature','SaleType','SaleCondition']
for col in Label_cols:
lbe=LabelEncoder()
lbe.fit(list(combine[col].values))
combine[col]=lbe.transform(list(combine[col].values))<drop_column>
age60.groupby(['Pclass','Embarked'] ).mean()
combine=combine.drop(['YearBuilt','YearRemodAdd','MoSold','YrSold','GarageYrBlt','GarageArea','TotRmsAbvGrd'],axis=1)
 combine.shape<categorify>
age60.groupby(['Pclass','Embarked','SibSp','Parch'] ).mean()
combine=pd.get_dummies(combine)
 combine.shape<set_options>
df.loc[df['PassengerId'] == 1044, 'Fare'] = 7.9
pd.options.display.max_columns=None
combine.columns.values<count_values>
df[df['Fare'].isnull() ]
test_raw.GarageType.value_counts()<drop_column>
df[df['PassengerId'] == 1044]
combine=combine.drop(['MSZoning_None','Street_Pave','Alley_None','LotConfig_Inside','Neighborhood_NAmes','Condition1_Norm','Condition2_Norm','RoofStyle_Gable','RoofMatl_CompShg','Exterior1st_VinylSd','Exterior2nd_VinylSd','MasVnrType_None','Foundation_PConc','Heating_GasA','GarageType_None'],axis=1)
 combine.shape<drop_column>
[df.Fare]=np.round([df.Fare],1 )
train=train.drop(['Id'],axis=1 )<drop_column>
print(df['Ticket'].str.split(expand=True))
test=combine[combine.SalePrice==0]
test=test.drop(['Id','SalePrice'],axis=1)
 test.shape<compute_test_metric>
Ticket = df['Ticket'].str.extract(' (.*)\s (.*)' )
def root_mean_squared_log_error(y_valid, y_preds):

 if len(y_preds)!=len(y_valid): return 'error_mismatch'
y_preds_new = [math.log(x)for x in y_preds]
y_valid_new = [math.log(x)for x in y_valid]
return mean_squared_error(y_valid_new, y_preds_new, squared=False )<split>
df['Ticket_head']=Ticket[0]
df['Ticket_num']=Ticket[1]
y=train['SalePrice']
x=train.drop(['SalePrice'],axis=1)
 X_train, X_valid, y_train, y_valid=train_test_split(x,y,random_state=73 )<compute_train_metric>
Ticket_head = df['Ticket_head'].apply(lambda x: x.replace(".", "")if type(x)is str else float(x))
Ticket_head=Ticket_head.fillna('0' )
RF_f=RandomForestRegressor(bootstrap=False, max_depth=60, max_features='sqrt',
min_samples_split=4, n_estimators=1700)
 RF_f.fit(X_train,y_train)
 y_pred_RF_f=RF_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_RF_f))<compute_train_metric>
df['Ticket_head'] = Ticket_head
XT=ExtraTreesRegressor(n_estimators=1000,random_state=73)
 XT.fit(X_train,y_train)
 y_pred_XT=XT.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_XT))<compute_train_metric>
df['Ticket_num']=df['Ticket_num'].fillna('0')
 df['Ticket_num'].value_counts()
Ada_f=AdaBoostRegressor(learning_rate=0.03, loss='exponential', n_estimators=2300,
random_state=73)
 Ada_f.fit(X_train,y_train)
 y_pred_Ada_f=Ada_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_Ada_f))<compute_train_metric>
df[df['Age'].isnull() ]
GB_f=GradientBoostingRegressor(max_depth=10, max_features='sqrt',
min_samples_split=12, n_estimators=1500)
 GB_f.fit(X_train,y_train)
 y_pred_GB_f=GB_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_GB_f))<compute_train_metric>
gc = df.drop(['PassengerId'], axis=1 )
HGB_f=HistGradientBoostingRegressor(learning_rate=0.02,
loss='least_absolute_deviation', max_depth=40,
max_iter=750, min_samples_leaf=2,
random_state=73)
 HGB_f.fit(X_train,y_train)
 y_pred_HGB_f=HGB_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_HGB_f))<compute_train_metric>
gc['Fare']=pd.cut(gc['Fare'], 5, labels=False)
 column = 'Cabin'
gc[column] = gc[column].fillna(0)
 gc[column] = gc[column].str.extract('([A-Za-z]+)', expand = False)
 column = 'mrms'
gc[column] = gc['Name'].str.extract('([A-Za-z]+)\.', expand = False)
 gc[column] = gc[column].replace(['Col', 'Mlle', 'Major','Countess','Lady', 'Sir', 'Dona', 'Capt', 'Jonkheer', 'Don', 'Mme', 'Ms', 'Dr', 'Rev'], 'Other')
 le = LabelEncoder()
gc['Ticket_head']=le.fit_transform(gc['Ticket_head'])
 gc['Ticket_num']=le.fit_transform(gc['Ticket_num'] )
XGB=XGBRegressor(n_estimators=1200,learning_rate=0.05,random_state=73)
 XGB.fit(X_train,y_train)
 y_pred_XGB=XGB.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_XGB))<train_on_grid>
list_ce = ['Cabin','Embarked','Sex','mrms','Survived','Fare']
ce_ohe = ce.OneHotEncoder(cols=list_ce,handle_unknown='impute')
 gc = ce_ohe.fit_transform(gc )
<choose_model_class>
gc = gc.drop(['Name','Ticket'], axis=1 )
XGB_f=XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0.5, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.05, max_delta_step=0, max_depth=4,
min_child_weight=1, monotone_constraints='() ',
n_estimators=1200, n_jobs=0, num_parallel_tree=1, random_state=73,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1.0,
tree_method='exact', validate_parameters=1, verbosity=None)
 XGB_f.fit(X_train,y_train)
 y_pred_XGB_f=XGB_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_XGB_f))<choose_model_class>
gccollist.remove('Age' )
XGB_model_f=XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.5, gamma=0.2, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.05, max_delta_step=0, max_depth=4,
min_child_weight=2, monotone_constraints='() ',
n_estimators=1000, n_jobs=0, num_parallel_tree=1, random_state=73,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1.0,
tree_method='exact', validate_parameters=1, verbosity=None)
 XGB_model_f.fit(X_train,y_train)
 y_pred_XGB_model_f=XGB_model_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_XGB_model_f))<choose_model_class>
gc.isnull().sum()
LGBg_f=LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
 LGBg_f.fit(X_train,y_train)
 y_pred_LGBg_f=LGBg_f.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_LGBg_f))<compute_train_metric>
gc['Age']=gc['Age'].fillna(gc.groupby(gccollist)['Age'].transform('mean'))
Vote=VotingRegressor([('gb',GB_f),('hgb',HGB_f),('xgb',XGB_f),('lgb',LGBg_f)])
 Vote.fit(X_train,y_train)
 y_pred_Vote=Vote.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_Vote))<compute_train_metric>
gc.Age.isnull().sum()
estimators=[('gb',GB_f),('hgb',HGB_f),('xgb',XGB_f),('lgb',LGBg_f)]
Stack=StackingRegressor(estimators=estimators,final_estimator=RandomForestRegressor(n_estimators=800,random_state=42))
Stack.fit(X_train,y_train)
 y_pred_Stack=Stack.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, y_pred_Stack))<train_model>
gc[gc['Age'].isnull() ]
Voting=VotingRegressor([('gb',GB_f),('hgb',HGB_f),('xgb',XGB_f),('lgb',LGBg_f)])
 Voting.fit(x,y )<save_to_csv>
gc_corr = gc.corr()
corr_y = pd.DataFrame({"features":gc.columns,"corr_y":gc_corr["Age"]},index=None)
 corr_y = corr_y.reset_index(drop=True )
preds = Voting.predict(test)
 output = pd.DataFrame({'Id': test_raw.Id,
'SalePrice': preds})
 output.to_csv('submission.csv', index=False )<compute_test_metric>
corr_y.sort_values('corr_y' )
def root_mean_squared_log_error(y_valid, y_preds):

 if len(y_preds)!=len(y_valid): return 'error_mismatch'
y_preds_new = [math.log(x)for x in y_preds]
y_valid_new = [math.log(x)for x in y_valid]
return mean_squared_error(y_valid_new, y_preds_new, squared=False )<load_from_csv>
gc['Age'] = np.round(gc['Age'].fillna(gc['Age'].mean()))
train_data = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 pd.set_option('display.max_columns', None)
 train_data.head()<count_unique_values>
gc.Age.isnull().sum()
print(train_data.columns[train_data.isna().any() ].unique())
 len(train_data.columns[train_data.isna().any() ].unique() )<prepare_x_and_y>
df['Age']=gc['Age']
features = [x for x in train_data.columns if x not in ['SalePrice']]
X = train_data.drop(['SalePrice'], axis=1)
 Y = train_data['SalePrice']<split>
df['Age']=pd.cut(df['Age'], 5, labels=False )
X_train, X_valid, y_train, y_valid = train_test_split(X, Y, random_state=42)
 numerical_cols = [cname for cname in X_train.columns if
X_train[cname].dtype in ['int64', 'float64']]
categorical_cols = [cname for cname in X_train.columns if
X_train[cname].nunique() < 13 and
X_train[cname].dtype == "object"]
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ] )<train_model>
df.isnull().sum()
random_model = RandomForestRegressor(random_state=42, n_estimators=1000)
 random_clf = Pipeline(steps=[('preprocessor', preprocessor), 
('random_model', random_model)
 ])
 random_clf.fit(X_train, y_train)
 random_clf.fit(X_train, y_train)
 random_preds = random_clf.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, random_preds))<train_model>
pip install --upgrade optuna
xgb_model = XGBRegressor(n_estimators=1000, learning_rate=0.01, random_state=42)
 xgb_clf = Pipeline(steps=[('preprocessor', preprocessor), 
('xgb_model', xgb_model)
 ])
 xgb_clf.fit(X_train, y_train, xgb_model__verbose=False)
 xgb_clf.fit(X_train, y_train)
 xgb_preds = xgb_clf.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, xgb_preds))<train_model>
gc=df
ada_model = AdaBoostRegressor(random_state=42, learning_rate=0.01, n_estimators=1000)
 ada_clf = Pipeline(steps=[('preprocessor', preprocessor), 
('xgb_model', ada_model)
 ])
 ada_clf.fit(X_train, y_train)
 ada_clf.fit(X_train, y_train)
 ada_preds = ada_clf.predict(X_valid)
 print('RMSLE:', root_mean_squared_log_error(y_valid, ada_preds))<count_missing_values>
gc['Fare']=pd.cut(gc['Fare'], 5, labels=False)
 column = 'Cabin'
gc[column] = gc[column].fillna(0)
 gc[column] = gc[column].str.extract('([A-Za-z]+)', expand = False)
 column = 'mrms'
gc[column] = gc['Name'].str.extract('([A-Za-z]+)\.', expand = False)
 gc[column] = gc[column].replace(['Col', 'Mlle', 'Major','Countess','Lady', 'Sir', 'Dona', 'Capt', 'Jonkheer', 'Don', 'Mme', 'Ms', 'Dr', 'Rev'], 'Other')
 le = LabelEncoder()
gc['Ticket_head']=le.fit_transform(gc['Ticket_head'])
 gc['Ticket_num']=le.fit_transform(gc['Ticket_num'])
 list_ce = ['Cabin','Embarked','Sex','mrms','Fare']
ce_ohe = ce.OneHotEncoder(cols=list_ce,handle_unknown='impute')
 gc = ce_ohe.fit_transform(gc)
 gc = gc.drop(['PassengerId','Name','Ticket'], axis=1 )
train_data['OverallQual'].isnull().sum()<count_missing_values>
train=gc
train_data['OverallCond'].isnull().sum()<count_missing_values>
test_x = train[train['Survived'].isnull() ]
test_x = test_x.drop(['Survived'], axis=1)
 df_result_dropna = train.dropna(subset=['Survived'])
 feature_names = df_result_dropna.drop(['Survived'], axis=1)
 feature_names = list(feature_names.columns)
 train_y = df_result_dropna['Survived'].astype(int)
 train_x = df_result_dropna.drop(['Survived'], axis=1 )
train_data['YearBuilt'].isnull().sum()<count_missing_values>
tr_x, va_x, tr_y, va_y = train_test_split(train_x, train_y,random_state=42,test_size=0.2 )
train_data['YearRemodAdd'].isnull().sum()<count_missing_values>
import optuna.integration.lightgbm as lgb
from sklearn.metrics import accuracy_score,f1_score
from sklearn.metrics import confusion_matrix
train_data['TotalBsmtSF'].isnull().sum()<filter>
best_params = {}
tuning_history = []
params = {'objective': 'binary','metric': 'binary_logloss'}
trn_data= lgb.Dataset(tr_x, label=tr_y)
 val_data= lgb.Dataset(va_x, label=va_y)
 model = lgb.train(params, trn_data,
valid_sets=[trn_data, val_data],
verbose_eval=0,
best_params=best_params,
tuning_history=tuning_history
)
Q1 = train_data['TotalBsmtSF'].quantile(0.25)
 Q3 = train_data['TotalBsmtSF'].quantile(0.75)
 IQR = Q3 - Q1
outliers = train_data.loc[(train_data['TotalBsmtSF'] >(Q3 + 1.75 * IQR)) |(train_data['TotalBsmtSF'] <(Q1 - 1.75 * IQR)) , 'TotalBsmtSF']
print("Percent of Outliers: ", outliers.count() / train_data['TotalBsmtSF'].count() * 100 )<drop_column>
prediction = np.rint(model.predict(va_x, num_iteration=model.best_iteration))
train_data.drop(train_data.loc[(train_data['TotalBsmtSF'] >(Q3 + 1.75 * IQR)) |(train_data['TotalBsmtSF'] <(Q1 - 1.75 * IQR)) ].index, inplace=True)
 train_data.shape<count_missing_values>
accuracy = accuracy_score(va_y, prediction)
 best_params = model.params
print("Best params:", best_params)
 print("Accuracy = {}".format(accuracy))
print("Params: ")
 for key, value in best_params.items() :
print(" {}: {}".format(key, value))
train_data['1stFlrSF'].isnull().sum()<filter>
pred_x = np.rint(model.predict(test_x, num_iteration=model.best_iteration))
<drop_column><EOS> 
df_sub['Survived'] = pred_x.astype(int)
 df_sub.to_csv('df_sub_pred_x.csv', index=False )