Kaggle competition: Titanic - Machine Learning from Disaster
nulls = data_explore.isna().sum()
nulls[nulls>0]<data_type_conversions>
test=pd.read_csv(".. /input/test.csv")
 test.head()
na_cols = ["Alley", "BsmtQual", "BsmtCond", "BsmtExposure", "BsmtFinType1", "BsmtFinType2", "GarageType", "GarageFinish", "GarageCond", "GarageQual"]
data_explore[na_cols] = data_explore[na_cols].fillna("NA" )<count_values>
gender_submission=pd.read_csv(".. /input/gender_submission.csv")
 gender_submission.head()
data_explore["Alley"].value_counts()<choose_model_class>
df.isnull().sum()
num_imputer = SimpleImputer(strategy="mean")
 cat_imputer = SimpleImputer(strategy="most_frequent" )<categorify>
df[(df['Fare'].isnull())|(df['Embarked'].isnull())]
num_nans = ['LotFrontage', 'MasVnrArea', 'GarageYrBlt']
cat_nans = ['MasVnrType', 'Electrical', 'FireplaceQu']
data_explore[num_nans] = num_imputer.fit_transform(data[num_nans])
 data_explore[cat_nans] = cat_imputer.fit_transform(data[cat_nans] )<count_missing_values>
cabin_df.groupby('PassengerCount' ).count()
nulls = data_explore.isna().sum()
nan_cols = nulls[nulls>0].index
nan_cols<data_type_conversions>
cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==1,1,'')
 cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==2,2,cabin_df['CabinOccupancy'])
 cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==3,3,cabin_df['CabinOccupancy'])
 cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==4,4,cabin_df['CabinOccupancy'])
 cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==5,5,cabin_df['CabinOccupancy'])
 cabin_df['CabinOccupancy']=np.where(cabin_df['PassengerCount']==6,6,cabin_df['CabinOccupancy'])
 cabin_df['CabinOccupancy']=cabin_df['CabinOccupancy'].astype(str)
 cabin_df.tail()
data_explore['MSSubClass'] = data_explore['MSSubClass'].astype(str)
 cat_attrs = []
num_attrs = []
columns = list(data_explore.columns)
 for col in columns:
if data_explore[col].dtype=='O':
cat_attrs.append(col)
 else:
num_attrs.append(col )<define_variables>
df=pd.merge(df,cabin_df[['Cabin','CabinOccupancy']],how='left',on='Cabin')
 df.head()
Q1 = data_explore.quantile(0.25)
 Q3 = data_explore.quantile(0.75)
 IQR = Q3 - Q1
outliers =(( data_explore <(Q1 - 1.5 * IQR)) |(data_explore >(Q3 + 1.5 * IQR)) ).sum()
outliers[outliers>0]<sort_values>
block=df['Cabin'].str.split('([A-Za-z]+ )(\d+)', expand=True)
 block['block']=np.where(block[1].isnull() ,block[3],block[1])
 block['block']=np.where(block['block'].isnull() ,block[5],block['block'])
 block['block']=np.where(block['block'].isnull() ,block[7],block['block'])
 block['block']=np.where(block['block'].isnull() ,block[9],block['block'])
 block['block']=np.where(block['block'].isnull() ,block[11],block['block'])
 block.head()
corr_matrix['SalePrice'].sort_values(ascending=False )<count_values>
df=pd.merge(df,block,how='outer',left_index=True,right_index=True)
 df=df.drop([0,1,2,3,4,5,6,7,8,9,10,11,12], axis=1)
 df.head()
data_explore['GarageCars'].value_counts()<prepare_x_and_y>
titles=df['Name'].str.split(',',expand=True)[1].str.split('.',expand=True)
 titles.rename(columns={0: 'Titles'}, inplace=True)
 titles=titles.drop(columns=[1,2])
 titles.head()
X = data.drop(columns=['SalePrice'], axis=1)
 y = data['SalePrice'].copy()<split>
df=pd.merge(df,titles,how='outer',left_index=True,right_index=True)
 df.head()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 y_log_train = np.log(y_train)
 y_log_test = np.log(y_test )<drop_column>
nonull_df=df[(df['Age'].notnull())&(df['Age'].notnull())&(df['Embarked'].notnull())&(df['block'].notnull())&(df['CabinOccupancy'].notnull())]
nonull_df.head()
na_cols = ["Alley", "BsmtQual", "BsmtCond", "BsmtExposure", "BsmtFinType1", "BsmtFinType2", "GarageType", "GarageFinish", "GarageCond", "GarageQual", "PoolQC", "Fence", "MiscFeature"]
cat_attrs = [cat for cat in cat_attrs if not cat in na_cols]
num_attrs.remove('SalePrice' )<import_modules>
nonull_df.count() ['PassengerId']
from sklearn.impute import SimpleImputer, KNNImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PowerTransformer, OneHotEncoder<categorify>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train[['Age','Fare','Parch','Pclass_1','Pclass_2','Pclass_3','SibSp','Embarked_S','Sex_male','Titles_ Mr']]
nonull_df_train.head()
num_pipeline = Pipeline([('imputer', SimpleImputer(strategy="mean")) , 
('transformer', PowerTransformer(method='yeo-johnson', standardize=True)) ])
 cat_pipeline_1 = Pipeline([('cat_na_fill', SimpleImputer(strategy="constant", fill_value='NA')) , 
('encoder', OneHotEncoder(handle_unknown='ignore')) ])
 cat_pipeline_2 = Pipeline([('cat_nan_fill', SimpleImputer(strategy="most_frequent")) , 
('encoder', OneHotEncoder(handle_unknown='ignore')) ] )<feature_engineering>
X_nonull_df_train = nonull_df_train.drop('Fare',axis=1)
 y_nonull_df_train = nonull_df_train['Fare']
pre_process = ColumnTransformer([('drop_id', 'drop', ['Id']), 
('cat_pipeline_1', cat_pipeline_1, na_cols), 
('cat_pipeline_2', cat_pipeline_2, cat_attrs), 
('num_pipeline', num_pipeline, num_attrs)], remainder='passthrough' )<categorify>
nullfare=df[df['Fare'].isnull() ]
nullfare
X_train_transformed = pre_process.fit_transform(X_train)
 X_test_transformed = pre_process.transform(X_test )<categorify>
nullfare=df[df['Fare'].isnull() ]
cat_feats=['Embarked','Sex','block','Titles','Pclass']
nullfare_test = pd.get_dummies(nullfare,columns=cat_feats,drop_first=False)
 nullfare_test=nullfare_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived','CabinOccupancy'])
 nullfare_test.head()
oh_na_cols = list(pre_process.transformers_[1][1]['encoder'].get_feature_names(na_cols))
oh_nan_cols = list(pre_process.transformers_[2][1]['encoder'].get_feature_names(cat_attrs))
feature_columns = oh_na_cols+oh_nan_cols + num_attrs<import_modules>
X_nullfare_test = nullfare_test
from sklearn.model_selection import GridSearchCV, KFold<choose_model_class>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Pclass_1','Pclass_2'])
 X_nullfare_test=X_nullfare_test.drop(columns=['Fare'])
kf = KFold(n_splits=5, shuffle=True, random_state=42 )<import_modules>
dtreeReg = DecisionTreeRegressor()
dtreeReg.fit(X_nonull_df_train,y_nonull_df_train )
from sklearn.linear_model import ElasticNet<train_on_grid>
predictions_nullfare = dtreeReg.predict(X_nullfare_test)
 predictions_nullfare
elastic_net_grid_param = [{'l1_ratio': list(np.linspace(0, 1, 10)) , 'alpha': [0.0001, 0.005, 0.001, 0.005, 0.01, 0.05, 0.1]}]
elastic_net_grid_search = GridSearchCV(ElasticNet(random_state=42), elastic_net_grid_param, cv=kf, scoring='neg_root_mean_squared_error', return_train_score=True, n_jobs=-1)
 elastic_net_grid_search.fit(X_train_transformed, y_log_train )<find_best_params>
nullfare['Fare']=np.where(nullfare['Fare'].isnull() ,93.5,nullfare['Fare'])
 nullfare.head()
train_results=[]
train_results.append(['Elastic Net', elastic_net_grid_search.best_params_, -elastic_net_grid_search.best_score_])
 elastic_net_grid_search.best_params_, -elastic_net_grid_search.best_score_<find_best_params>
df['Fare']=np.where(df['Fare'].isnull() ,93.5,df['Fare'])
 df[df['PassengerId']==1044].head()
best_elastic_net_reg = elastic_net_grid_search.best_estimator_
best_elastic_net_reg<import_modules>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train[['Age','Fare','Parch','SibSp',
'Embarked_S','Sex_male','Titles_ Mr',
'block_A','block_B','block_C','block_D',
'block_E','block_F','block_G','Pclass_1','Pclass_2','Pclass_3']]
nonull_df_train.head()
from sklearn.svm import SVR<train_on_grid>
X_nonull_df_train = nonull_df_train.drop(['block_A','block_B','block_C','block_D','block_E','block_F','block_G'],axis=1)
 y_nonull_df_train = nonull_df_train[['block_A','block_B','block_C','block_D','block_E','block_F','block_G']]
svr_grid_param = [{'C':list(np.linspace(0.1, 1, 10)) , 'epsilon':[0.01, 0.05, 0.1, 0.5, 1]}]
svr_grid_search = GridSearchCV(SVR(kernel="poly", degree=2), svr_grid_param, cv=kf, scoring="neg_root_mean_squared_error", return_train_score=True, n_jobs=-1)
 svr_grid_search.fit(X_train_transformed, y_log_train )<find_best_params>
nullblock=nullfare
cat_feats=['Embarked','Sex','block','Titles']
nullblock_test = pd.get_dummies(nullblock,columns=cat_feats,drop_first=False)
 nullblock_test=nullblock_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullblock_test.head()
train_results.append(['SVR', svr_grid_search.best_params_, -svr_grid_search.best_score_])
 svr_grid_search.best_params_, -svr_grid_search.best_score_<find_best_params>
X_nullblock_test = nullblock_test
best_svr_reg = svr_grid_search.best_estimator_
best_svr_reg<import_modules>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Pclass_1','Pclass_2'])
 X_nullblock_test=X_nullblock_test.drop(columns=['CabinOccupancy'])
from sklearn.ensemble import RandomForestRegressor<import_modules>
dtreeClass = DecisionTreeClassifier()
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
from sklearn.ensemble import RandomForestRegressor<train_on_grid>
predictions_nullblock = dtreeClass.predict(X_nullblock_test)
 predictions_nullblock
rf_grid_param = [{'max_features':[0.2, 0.4, 0.6, 'auto'], 'max_depth':[8, 12, 16, 20]}]
rf_grid_search = GridSearchCV(RandomForestRegressor(n_estimators=300, random_state=42, n_jobs=-1), rf_grid_param, cv=kf, scoring='neg_root_mean_squared_error', return_train_score=True, n_jobs=-1)
 rf_grid_search.fit(X_train_transformed, y_log_train )<find_best_params>
nullblock['block']=np.where(nullfare['block'].isnull() ,'C',nullblock['block'])
 nullblock.head()
train_results.append(['Random Forest', rf_grid_search.best_params_, -rf_grid_search.best_score_])
 rf_grid_search.best_params_, -rf_grid_search.best_score_<find_best_params>
df['block']=np.where(( df['block'].isnull())&(df['PassengerId']==1044),'C',df['block'])
 df[df['PassengerId']==1044].head()
best_rf_reg = rf_grid_search.best_estimator_
best_rf_reg<import_modules>
cat_feats=['Embarked','Sex','Titles','block','Pclass','CabinOccupancy']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train[['Age','Fare','Parch','Pclass_1','Pclass_2','Pclass_3','SibSp',
'Embarked_S','Sex_male','block_C','Titles_ Mr','CabinOccupancy_1',
'CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6']]
nonull_df_train.head()
from xgboost import XGBRegressor<train_on_grid>
X_nonull_df_train = nonull_df_train.drop(columns=['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6'],axis=1)
 y_nonull_df_train = nonull_df_train[['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6']]
xgb_grid_parm=[{'max_depth':[4, 6, 8, 12], 'subsample':[0.5, 0.75, 1.0]}]
xgb_grid_search = GridSearchCV(XGBRegressor(objective='reg:squarederror', n_estimators=300, learning_rate=0.1, random_state=42, n_jobs=-1), xgb_grid_parm, cv=kf, scoring="neg_root_mean_squared_error", return_train_score=True, n_jobs=-1)
 xgb_grid_search.fit(X_train_transformed, y_log_train )<find_best_params>
nullocc=nullfare
cat_feats=['Embarked','Sex','block','Titles','Pclass']
nullocc_test = pd.get_dummies(nullocc,columns=cat_feats,drop_first=False)
 nullocc_test=nullocc_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullocc_test.head()
train_results.append(['XGBoost', xgb_grid_search.best_params_, -xgb_grid_search.best_score_])
 xgb_grid_search.best_params_, -xgb_grid_search.best_score_<compute_train_metric>
X_nullocc_test = nullocc_test
cvres = xgb_grid_search.cv_results_
for train_mean_score, test_mean_score, params in zip(cvres["mean_train_score"], cvres["mean_test_score"], cvres["params"]):
print(-train_mean_score, -test_mean_score, params )<find_best_params>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Pclass_1','Pclass_2'])
 X_nullocc_test=X_nullocc_test.drop(columns=['CabinOccupancy'] )
best_xgb_reg = xgb_grid_search.best_estimator_
best_xgb_reg<import_modules>
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
from sklearn.ensemble import StackingRegressor
from sklearn.linear_model import ElasticNetCV<train_on_grid>
predictions_nullocc = dtreeClass.predict(X_nullocc_test)
 predictions_nullocc
base_estimators = [('elastic_net', best_elastic_net_reg),('svr', best_svr_reg),('rf', best_rf_reg),('xgb', best_xgb_reg)]
stack_reg = StackingRegressor(estimators=base_estimators, final_estimator=ElasticNetCV(l1_ratio=[.1,.5,.7,.9,.95,.99, 1], random_state=42), cv=kf, passthrough=False, n_jobs=-1)
 stack_reg.fit(X_train_transformed, y_log_train )<compute_train_metric>
nullocc['CabinOccupancy']=np.where(nullocc['CabinOccupancy'].isnull() ,'2',nullocc['CabinOccupancy'])
 nullocc.head()
stack_rmse_scores = cross_val_score(stack_reg, X_train_transformed, y_log_train, scoring='neg_root_mean_squared_error', cv=kf, n_jobs=-1)
 stack_rmse = np.round(np.mean(-stack_rmse_scores), 4)
 train_results.append(['Stacking', '', stack_rmse] )<create_dataframe>
df['CabinOccupancy']=np.where(( df['CabinOccupancy'].isnull())&(df['PassengerId']==1044),'2',df['CabinOccupancy'])
 df[df['PassengerId']==1044].head()
pd.set_option('display.max_colwidth', -1)
 train_models_df = pd.DataFrame(train_results, columns=['Model', 'Best Paramas', 'RMSLE'])
 train_models_df<find_best_params>
nonull_df=df[(df['Fare'].notnull())&(df['Age'].notnull())&(df['Embarked'].notnull())&(df['block'].notnull())&(df['CabinOccupancy'].notnull())]
nonull_df.count() ['PassengerId']
results = dict()
best_models = [best_elastic_net_reg, best_svr_reg, best_rf_reg, best_xgb_reg, stack_reg]
model_names = []
model_rmse = []
for model in best_models:
test_rmse_scores = cross_val_score(model, X_test_transformed, y_log_test, scoring='neg_root_mean_squared_error', cv=kf, n_jobs=-1)
 test_rmse_scores = np.round(-test_rmse_scores,4)
 test_rmse = np.round(np.mean(test_rmse_scores),4)
 model_names.append(model.__class__.__name__)
 model_rmse.append(test_rmse )<find_best_params>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train = nonull_df_train[['Age','Fare','Parch','Pclass_1','Pclass_2','Pclass_3','SibSp','CabinOccupancy',
'Embarked_S','Embarked_Q','Embarked_C','Sex_female','Titles_ Miss','Titles_ Mrs','block_B']]
nonull_df_train.head()
best_model = best_models[np.argmin(model_rmse)]
best_model<predict_on_test>
X_nonull_df_train = nonull_df_train.drop(['Embarked_S','Embarked_Q','Embarked_C'],axis=1)
 y_nonull_df_train = nonull_df_train[['Embarked_S','Embarked_Q','Embarked_C']]
y_train_pred = best_model.predict(X_train_transformed)
 y_test_pred = best_model.predict(X_test_transformed)
 y_train_pred = np.exp(y_train_pred)
 y_test_pred = np.exp(y_test_pred)
 predicted = np.concatenate([y_train_pred, y_test_pred], axis=0)
 obsereved = np.concatenate([y_train, y_test], axis=0 )<concatenate>
cat_feats=['Embarked','Sex','block','Titles','Pclass']
nullembark_test = pd.get_dummies(nullembark,columns=cat_feats,drop_first=False)
 nullembark_test=nullembark_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullembark_test.head()
combine_data = pd.concat([X_train, X_test], axis=0)
 combine_data['SalePrice'] = obsereved
combine_data['Predicted_SalePrice'] = predicted
combine_data.shape<train_model>
X_nullembark_test = nullembark_test
final_model = Pipeline([('pre_process', pre_process), 
('best_model', best_model)])
 final_model.fit(X_train, y_log_train )<load_from_csv>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Pclass_1','Pclass_2'])
test_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )<predict_on_test>
dtreeClass = DecisionTreeClassifier()
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
log_predictions = final_model.predict(test_data)
 predictions = np.exp(log_predictions )<create_dataframe>
predictions_nullembark = dtreeClass.predict(X_nullembark_test)
 predictions_nullembark
test_predictions = pd.DataFrame(test_data['Id'])
 test_predictions['SalePrice'] = predictions.copy()<save_to_csv>
df['Embarked']=np.where(( df['PassengerId']==62)|(df['PassengerId']==830)&(df['Embarked'].isnull()),'S',df['Embarked'])
 df[(df['PassengerId']==830)|(df['PassengerId']==62)].head()
test_predictions.to_csv("./submission.csv", index=False )<import_modules>
nonull_df=df[(df['Fare'].notnull())&(df['Age'].notnull())&(df['Embarked'].notnull())&(df['block'].notnull())]
nonull_df.count() ['PassengerId']
print('Python: {}'.format(sys.version))
print('scipy: {}'.format(scipy.__version__))
print('numpy: {}'.format(np.__version__))
print('matplotlib: {}'.format(matplotlib.__version__))
print('pandas: {}'.format(pd.__version__))
print('sklearn: {}'.format(sklearn.__version__))
print('seaborn: {}'.format(sns.__version__))<load_from_csv>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
train=pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test=pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv" )<sort_values>
X_nonull_df_train = nonull_df_train.drop(['Age'],axis=1)
 y_nonull_df_train = nonull_df_train[['Age']]
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<sort_values>
nullage=df[df['Age'].isnull() & df['block'].notnull() ]
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullage_test = pd.get_dummies(nullage,columns=cat_feats,drop_first=False)
 nullage_test=nullage_test.drop(columns=['Age','Name','Ticket','PassengerId','Cabin','Survived'])
 nullage_test.head()
total = test.isnull().sum().sort_values(ascending=False)
 percent =(test.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<train_model>
X_nullage_test = nullage_test
train.fillna(-999,inplace=True)
 test.fillna(-999,inplace=True )<count_missing_values>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Col','Titles_ Dona','Titles_ Dr','Titles_ Lady','Titles_ Major','Titles_ Master',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess','block_G'])
train.isnull().sum().sum() , test.isnull().sum().sum()<prepare_x_and_y>
dtreeReg.fit(X_nonull_df_train,y_nonull_df_train )
y = train.SalePrice.values
X = train.drop(['SalePrice', 'Id'], axis=1)
 X_test = test.drop(['Id'], axis = 1 )<define_variables>
predictions_nullage = dtreeReg.predict(X_nullage_test)
 predictions_nullage
categorical_features_indices = np.where(X.dtypes != np.float)[0]
categorical_features_indices = np.where(X_test.dtypes != np.float)[0]<split>
nullage.drop(columns=['Age'])
 nullage['Age']=predictions_nullage
nullage.head()
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=1, shuffle=True)
 X_train.shape, X_val.shape, y_train.shape,y_val.shape, X_test.shape<train_model>
df['Age']=np.where(((df['Age'].isnull())&(df['block'].notnull())) ,(df['Age'].fillna(nullage['Age'])) ,(df['Age']))
model = CatBoostRegressor(random_state=1, iterations=1500, depth=5, learning_rate=.1, loss_function='RMSE')
 model.fit(X_train, y_train,cat_features=categorical_features_indices,eval_set=(X_val, y_val),plot=True )<compute_test_metric>
nonull_df=df[(df['Fare'].notnull())&(df['Age'].notnull())&(df['Embarked'].notnull())&(df['block'].notnull())]
nonull_df.count() ['PassengerId']
print(model.score(X_train, y_train))<predict_on_test>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
y_pred = model.predict(X_val)
 y_pred = y_pred.astype(int)
 print(model.score(X_val, y_val)) , print(r2_score(y_pred, model.predict(X_val)) )<create_dataframe>
X_nonull_df_train = nonull_df_train.drop(['Age'],axis=1)
 y_nonull_df_train = nonull_df_train[['Age']]
df=pd.DataFrame({'Actual': y_val, 'Predicted':y_pred})
 df<predict_on_test>
nullageblk=df[df['block'].isnull() & df['Age'].isnull() ]
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullageblk_test = pd.get_dummies(nullageblk,columns=cat_feats,drop_first=False)
 nullageblk_test=nullageblk_test.drop(columns=['Age','Name','Ticket','PassengerId','Cabin','Survived'])
 nullageblk_test.head()
final_labels = model.predict(X_test)
 final_labels = final_labels.astype(int)
 final_labels<create_dataframe>
X_nullageblk_test = nullageblk_test
final_result = pd.DataFrame({'Id': test['Id'], 'SalePrice': final_labels} )<save_to_csv>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Col','Titles_ Dona','Titles_ Lady','Titles_ Major',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess','block_G',
'block_A','block_B','block_C','block_D','block_E','block_F','CabinOccupancy'])
 X_nullageblk_test=X_nullageblk_test.drop(columns=['CabinOccupancy','Titles_ Ms'] )
final_result.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
dtreeReg.fit(X_nonull_df_train,y_nonull_df_train )
submission = pd.read_csv("submission.csv")
 submission<import_modules>
predictions_nullageblk = dtreeReg.predict(X_nullageblk_test)
 predictions_nullageblk
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
from scipy.stats import skew
from scipy.stats.stats import pearsonr
from sklearn import metrics
from sklearn.metrics import mean_squared_error<load_from_csv>
nullageblk.drop(columns=['Age'])
 nullageblk['Age']=predictions_nullageblk
nullageblk
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )<concatenate>
cat_feats=['Embarked','Sex','Titles','block','Pclass','CabinOccupancy']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],test.loc[:,'MSSubClass':'SaleCondition']))<feature_engineering>
X_nonull_df_train = nonull_df_train.drop(columns=['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6'],axis=1)
 y_nonull_df_train = nonull_df_train[['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6']]
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index<feature_engineering>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullageblk_test = pd.get_dummies(nullageblk,columns=cat_feats,drop_first=False)
 nullageblk_test=nullageblk_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullageblk_test.head()
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
skewed_feats
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )<categorify>
X_nullageblk_test = nullageblk_test
all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean() )<prepare_x_and_y>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Col','Titles_ Dona','Titles_ Lady','Titles_ Major',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess','block_G',
'block_A','block_B','block_C','block_D','block_E','block_F'])
 X_nullageblk_test=X_nullageblk_test.drop(columns=['Titles_ Ms','CabinOccupancy'] )
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice<compute_train_metric>
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse )<choose_model_class>
predictions_nullageblk = dtreeClass.predict(X_nullageblk_test)
 predictions_nullageblk
model_ridge = Ridge()<define_search_space>
predictions_nullageblk=pd.DataFrame(predictions_nullageblk, columns=['1','2','3','4','5','6'])
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['1']==1,'1','')
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['2']==1,'2',predictions_nullageblk['CabinOccupancy'])
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['3']==1,'3',predictions_nullageblk['CabinOccupancy'])
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['4']==1,'4',predictions_nullageblk['CabinOccupancy'])
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['5']==1,'5',predictions_nullageblk['CabinOccupancy'])
 predictions_nullageblk['CabinOccupancy']=np.where(predictions_nullageblk['6']==1,'6',predictions_nullageblk['CabinOccupancy'])
 predictions_nullageblk.head()
alphas = [0.05, 0.1, 0.3, 1, 3, 5, 10, 15, 30, 50, 75]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean()
for alpha in alphas]<train_on_grid>
predictions_nullageblk=predictions_nullageblk['CabinOccupancy'].values
model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y)
 rmse_cv(model_lasso ).mean()<concatenate>
nullageblk.drop(columns=['CabinOccupancy'])
 nullageblk['CabinOccupancy']=predictions_nullageblk
nullageblk
imp_coef = pd.concat([coef.sort_values().head(10),
coef.sort_values().tail(10)] )<import_modules>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
import xgboost as xgb<train_on_grid>
X_nonull_df_train = nonull_df_train.drop(['block_A','block_B','block_C','block_D','block_E','block_F','block_G'],axis=1)
 y_nonull_df_train = nonull_df_train[['block_A','block_B','block_C','block_D','block_E','block_F','block_G']]
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=100 )<train_model>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullageblk_test = pd.get_dummies(nullageblk,columns=cat_feats,drop_first=False)
 nullageblk_test=nullageblk_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullageblk_test.head()
model_xgb = xgb.XGBRegressor(n_estimators=360, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )<predict_on_test>
X_nullageblk_test=nullageblk_test
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))<create_dataframe>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Col','Titles_ Dona','Titles_ Lady','Titles_ Major',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess'])
 X_nullageblk_test=X_nullageblk_test.drop(columns=['Titles_ Ms'] )
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )<import_modules>
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
from catboost import Pool
from catboost import CatBoostRegressor
from sklearn.model_selection import train_test_split
import numpy as np<data_type_conversions>
predictions_nullageblk = dtreeClass.predict(X_nullageblk_test)
 predictions_nullageblk
float_cols = X.dtypes[X.dtypes == "float"].index
X.loc[:,float_cols] = X[float_cols].astype(str)
 X_train.shape, X.shape<split>
predictions_nullageblk=pd.DataFrame(predictions_nullageblk, columns=['block_A','block_B','block_C','block_D','block_E','block_F','block_G'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_A']==1,'A','')
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_B']==1,'B',predictions_nullageblk['block'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_C']==1,'C',predictions_nullageblk['block'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_D']==1,'D',predictions_nullageblk['block'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_E']==1,'E',predictions_nullageblk['block'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_F']==1,'F',predictions_nullageblk['block'])
 predictions_nullageblk['block']=np.where(predictions_nullageblk['block_G']==1,'G',predictions_nullageblk['block'])
 predictions_nullageblk.head()
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=SEED)
 cat_features = list(range(X.shape[1]))<create_dataframe>
predictions_nullageblk=predictions_nullageblk['block'].values
train_data = Pool(data=X_train,
label=y_train,
cat_features=cat_features 
)
 valid_data = Pool(data=X_valid,
label=y_valid,
cat_features=cat_features
)<train_on_grid>
nullageblk.drop(columns=['block'])
 nullageblk['block']=predictions_nullageblk
nullageblk
%%time
params = { 'early_stopping_rounds': 100,
'verbose': False,
'random_seed': SEED,
'eval_metric':'RMSE'
}
cbc_7 = CatBoostRegressor(**params)
 cbc_7.fit(train_data,
eval_set=valid_data,
use_best_model=True,
plot=True 
);<compute_test_metric>
df['Age'] = df['Age'].mask(df['Age'].eq(0)).fillna(df['PassengerId'].map(nullageblk.set_index('PassengerId')['Age']))
df['CabinOccupancy'] = df['CabinOccupancy'].mask(df['CabinOccupancy'].eq(0)).fillna(df['PassengerId'].map(nullageblk.set_index('PassengerId')['CabinOccupancy']))
df['block'] = df['block'].mask(df['block'].eq(0)).fillna(df['PassengerId'].map(nullageblk.set_index('PassengerId')['block']))
cbc_7.get_feature_importance(prettified=True )<data_type_conversions>
df[df['PassengerId']==6]
X_test = test_df
float_cols_test = X_test.dtypes[X_test.dtypes == "float"].index
X_test.loc[:,float_cols_test] = X_test[float_cols_test].astype(str)
 X_train.shape, X.shape , X_test.shape<define_variables>
nonull_df=df[(df['Fare'].notnull())&(df['Age'].notnull())&(df['Embarked'].notnull())&(df['block'].notnull())]
nonull_df.count() ['PassengerId']
%%time
n_fold = 3
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=SEED)
 params = {
'early_stopping_rounds': 100,
'verbose': False,
'random_seed': SEED,
'eval_metric':'RMSE'
}
test_data = Pool(data=X_test,
cat_features=cat_features)
 bins = np.linspace(0, 1, 100)
 y_binned = np.digitize(y, bins)
 scores = []
catboost_prediction = np.zeros(X_test.shape[0])
 for fold_n,(train_index, valid_index)in enumerate(folds.split(X, y_binned)) :
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = Pool(data=X_train,
label=y_train,
cat_features=cat_features)
 valid_data = Pool(data=X_valid,
label=y_valid,
cat_features=cat_features)
 model = CatBoostRegressor(**params)
 model.fit(train_data,
eval_set=valid_data,
use_best_model=True 
)
 score = model.get_best_score() ['validation']['RMSE']
scores.append(score)
 y_pred = model.predict(test_data)
 catboost_prediction += y_pred
catboost_prediction /= n_fold
print('CV mean: {:.4f}, CV std: {:.4f}'.format(np.mean(scores), np.std(scores)))
 print('RMSE:', np.sqrt(metrics.mean_squared_error(y_valid, model.predict(X_valid))))<create_dataframe>
cat_feats=['Embarked','Sex','Titles','block','Pclass','CabinOccupancy']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
h20_preds = pd.read_csv('.. /input/house-price-predictions-h20-automl-without-tuning/house_sales_price_pred_full.csv')
 predictions = pd.DataFrame({"h20":h20_preds['SalePrice'], "lasso":lasso_preds})
 predictions.plot(x = "h20", y = "lasso", kind = "scatter")
 predictions = pd.DataFrame({"h20":h20_preds['SalePrice'], "cb":cb_preds})
 predictions.plot(x = "h20", y = "cb", kind = "scatter")
 predictions = pd.DataFrame({"h20":h20_preds['SalePrice'], "xgb":xgb_preds})
 predictions.plot(x = "h20", y = "xgb", kind = "scatter" )<save_to_csv>
X_nonull_df_train = nonull_df_train.drop(columns=['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6'],axis=1)
 y_nonull_df_train = nonull_df_train[['CabinOccupancy_1','CabinOccupancy_2','CabinOccupancy_3','CabinOccupancy_4','CabinOccupancy_5','CabinOccupancy_6']]
preds = 0.57*lasso_preds + 0.0*xgb_preds + 0.0*cb_preds + 0.43*h20_preds['SalePrice']
cb_solution = pd.DataFrame({"id":test.Id, "SalePrice":preds})
 cb_solution.to_csv("house_price_preds.csv", index = False )<import_modules>
nullblk=df[df['block'].isnull() & df['Age'].notnull() ]
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullblk_test = pd.get_dummies(nullblk,columns=cat_feats,drop_first=False)
 nullblk_test=nullblk_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullblk_test.head()
from fastai_structured import *
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from catboost import CatBoostRegressor
from sklearn.model_selection import RandomizedSearchCV
import matplotlib.pyplot as plt
import seaborn as sns
import altair as alt<load_from_csv>
X_nullblk_test=nullblk_test
df = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 df.shape<count_values>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Dona','Titles_ Lady','Titles_ Major',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess',
'block_A','block_B','block_C','block_D','block_E','block_F','block_G'])
 X_nullblk_test=X_nullblk_test.drop(columns=['CabinOccupancy','Titles_ Jonkheer','Titles_ Don','Titles_ Rev'] )
df.dtypes.value_counts()<count_missing_values>
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
df.select_dtypes(['object'] ).isna().sum()<count_missing_values>
predictions_nullblk = dtreeClass.predict(X_nullblk_test)
 predictions_nullblk
df.select_dtypes(['float64'] ).isna().sum()<drop_column>
predictions_nullblk=pd.DataFrame(predictions_nullblk, columns=['1','2','3','4','5','6'])
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['1']==1,'1','')
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['2']==1,'2',predictions_nullblk['CabinOccupancy'])
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['3']==1,'3',predictions_nullblk['CabinOccupancy'])
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['4']==1,'4',predictions_nullblk['CabinOccupancy'])
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['5']==1,'5',predictions_nullblk['CabinOccupancy'])
 predictions_nullblk['CabinOccupancy']=np.where(predictions_nullblk['6']==1,'6',predictions_nullblk['CabinOccupancy'])
 predictions_nullblk.head()
df['TotalSqFeet'] = df.GrLivArea + df.TotalBsmtSF
df['TotBathrooms'] = df.FullBath +(df.HalfBath)*0.5 + df.BsmtFullBath +(df.BsmtHalfBath)*0.5
df.set_index('Id',inplace=True)
<sort_values>
predictions_nullblk=predictions_nullblk['CabinOccupancy'].values
df.groupby("Neighborhood")["SalePrice"].mean().sort_values(ascending=False ).index<sort_values>
nullblk.drop(columns=['CabinOccupancy'])
 nullblk['CabinOccupancy']=predictions_nullblk
nullblk
df.groupby("MSSubClass")["SalePrice"].mean().sort_values(ascending=False )<concatenate>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nonull_df_train = pd.get_dummies(nonull_df,columns=cat_feats,drop_first=False)
 nonull_df_train=nonull_df_train.drop(columns=['Cabin','Name','PassengerId','Survived','Ticket'])
 nonull_df_train.head()
train_cats(df)
<load_from_csv>
X_nonull_df_train = nonull_df_train.drop(['block_A','block_B','block_C','block_D','block_E','block_F','block_G'],axis=1)
 y_nonull_df_train = nonull_df_train[['block_A','block_B','block_C','block_D','block_E','block_F','block_G']]
test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 test['TotalSqFeet'] = test.GrLivArea + test.TotalBsmtSF
test['TotBathrooms'] = test.FullBath +(test.HalfBath)*0.5 + test.BsmtFullBath +(test.BsmtHalfBath)*0.5
test.set_index('Id', inplace=True)
 apply_cats(test, df )<feature_engineering>
cat_feats=['Embarked','Sex','Titles','block','Pclass']
nullblk_test = pd.get_dummies(nullblk,columns=cat_feats,drop_first=False)
 nullblk_test=nullblk_test.drop(columns=['Name','Ticket','PassengerId','Cabin','Survived'])
 nullblk_test.head()
test.Neighborhood = test.Neighborhood.cat.codes
df.Neighborhood = df.Neighborhood.cat.codes<count_missing_values>
X_nullblk_test=nullblk_test
display_all(df.isna().sum() /len(df)*100 )<prepare_x_and_y>
X_nonull_df_train=X_nonull_df_train.drop(columns=['Titles_ Capt','Titles_ Dona','Titles_ Lady','Titles_ Major',
'Titles_ Mlle','Titles_ Mme','Titles_ Sir','Titles_ the Countess'])
 X_nullblk_test=X_nullblk_test.drop(columns=['Titles_ Jonkheer','Titles_ Don','Titles_ Rev'] )
X, y, nas = proc_df(df, 'SalePrice')
 X_test, _, _ = proc_df(test, na_dict=nas )<sort_values>
dtreeClass.fit(X_nonull_df_train,y_nonull_df_train )
X.skew(axis=0 ).sort_values(ascending=False )<feature_engineering>
predictions_nullblk = dtreeClass.predict(X_nullblk_test)
 predictions_nullblk
numeric_feats = df.select_dtypes(include=np.number)
 skewed_feats = [numeric_feats].apply(lambda x: skew(x.dropna())).sort_values(ascending=False)
 print("
Skew in numerical features:
")
 skewness = pd.DataFrame({'Skew' :skewed_feats})
 skewness.head(10)
 skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam )<prepare_x_and_y>
predictions_nullblk=pd.DataFrame(predictions_nullblk, columns=['block_A','block_B','block_C','block_D','block_E','block_F','block_G'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_A']==1,'A','')
 predictions_nullblk['block']=np.where(predictions_nullblk['block_B']==1,'B',predictions_nullblk['block'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_C']==1,'C',predictions_nullblk['block'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_D']==1,'D',predictions_nullblk['block'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_E']==1,'E',predictions_nullblk['block'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_F']==1,'F',predictions_nullblk['block'])
 predictions_nullblk['block']=np.where(predictions_nullblk['block_G']==1,'G',predictions_nullblk['block'])
 predictions_nullblk.head()
y_first = np.log1p(df.SalePrice )<prepare_output>
predictions_nullblk=predictions_nullblk['block'].values
y = np.log1p(y_first )<count_missing_values>
nullblk.drop(columns=['block'])
 nullblk['block']=predictions_nullblk
nullblk
display_all(X.isna().sum() /len(X)*100 )<train_model>
df['CabinOccupancy'] = df['CabinOccupancy'].mask(df['CabinOccupancy'].eq(0)).fillna(df['PassengerId'].map(nullblk.set_index('PassengerId')['CabinOccupancy']))
df['block'] = df['block'].mask(df['block'].eq(0)).fillna(df['PassengerId'].map(nullblk.set_index('PassengerId')['block']))
xgb = XGBRegressor(n_estimators=10000)
 xgb.fit(X, y, verbose=False)
 xgb.score(X, y )<train_model>
df[df['PassengerId']==9]
m = CatBoostRegressor(random_state=0, verbose=1000, n_estimators=100000, learning_rate=0.007, early_stopping_rounds=5)
 m.fit(X, y)
 m.score(X, y )<train_model>
train['Age'] = train['Age'].mask(train['Age'].eq(0)).fillna(train['PassengerId'].map(df.set_index('PassengerId')['Age']))
train['Embarked'] = train['Embarked'].mask(train['Embarked'].eq(0)).fillna(train['PassengerId'].map(df.set_index('PassengerId')['Embarked']))
train=pd.merge(train,df[['PassengerId','CabinOccupancy','block','Titles']],how='left',on='PassengerId' )
m = CatBoostRegressor(random_state=0, verbose=1000, n_estimators=10000)
 m.fit(X, y)
 m.score(X, y )<drop_column>
import scipy
import scipy.stats as ss
from scipy.stats import pearsonr
from scipy import stats
m.save_model('hp_cb',
format="cbm",
export_parameters=None,
pool=None )<train_model>
def cramers_v(confusion_matrix):

 chi2 = ss.chi2_contingency(confusion_matrix)[0]
n = confusion_matrix.sum()
phi2 = chi2 / n
r, k = confusion_matrix.shape
phi2corr = max(0, phi2 -(( k-1)*(r-1)) /(n-1))
rcorr = r -(( r-1)**2)/(n-1)
 kcorr = k -(( k-1)**2)/(n-1)
 return np.sqrt(phi2corr / min(( kcorr-1),(rcorr-1)) )
m = RandomForestRegressor(n_jobs=-1, n_estimators=60, random_state=0)
 m.fit(X, y)
 m.score(X, y )<train_model>
confusion_matrix = pd.crosstab(train["Survived"], train["Sex"] ).as_matrix()
cramers_v(confusion_matrix )
et = ExtraTreesRegressor(n_jobs=-1, n_estimators=500, random_state=0, max_features=0.5)
 et.fit(X, y)
 et.score(X, y )<train_model>
scipy.stats.spearmanr(train["Survived"], train["Sex"] )
m = ExtraTreesRegressor(n_jobs=-1, n_estimators=500, random_state=0, max_features=0.5)
 m.fit(X, y)
 m.score(X, y )<import_modules>
confusion_matrix = pd.crosstab(train["Survived"], train["Pclass"] ).as_matrix()
cramers_v(confusion_matrix )
import shap
from catboost import Pool<set_options>
scipy.stats.spearmanr(train["Survived"], train["Pclass"] )
pd.set_option('display.max_rows', 100 )<compute_test_metric>
confusion_matrix = pd.crosstab(train["Survived"], train["block"] ).as_matrix()
cramers_v(confusion_matrix )
shap_values = m.get_feature_importance(Pool(X, y), type='ShapValues' )<compute_test_metric>
confusion_matrix = pd.crosstab(train["Survived"], train["Parch"] ).as_matrix()
cramers_v(confusion_matrix )
m.get_feature_importance(Pool(X, y), type='LossFunctionChange', prettified=True )<define_search_space>
scipy.stats.pointbiserialr(train["Survived"], train["Age"] )
max_features = ['sqrt', 'log2', 0.5, None]
min_samples_leaf = [1, 3, 5, 10, 25, 100]
random_grid = {'min_samples_leaf': min_samples_leaf,
'max_features': max_features}
print(random_grid )<choose_model_class>
scipy.stats.pointbiserialr(train["Survived"], train["Fare"] )
rf = RandomForestRegressor(n_estimators=100)
 rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid,
n_iter = 20, cv = 5, verbose=2, random_state=10, n_jobs=-1 )<train_model>
cat_feats=['Sex','block','Titles','Pclass']
search = rf_random.fit(X, y)
 print('Par:', search.best_params_)
 print('Score:', search.best_score_ )<predict_on_test>
train_ = pd.get_dummies(train_,columns=cat_feats,drop_first=False )
y_pred_xgb = xgb.predict(X_test )<predict_on_test>
test['Age'] = test['Age'].mask(test['Age'].eq(0)).fillna(test['PassengerId'].map(df.set_index('PassengerId')['Age']))
test['Fare'] = test['Fare'].mask(test['Fare'].eq(0)).fillna(test['PassengerId'].map(df.set_index('PassengerId')['Fare']))
test=pd.merge(test,df[['PassengerId','CabinOccupancy','block','Titles']],how='left',on='PassengerId')
 test.info()
y_pred_cb = m.predict(X_test )<prepare_output>
test_ = pd.get_dummies(test_,columns=cat_feats,drop_first=False )
y_pred = 9/10 * y_pred_cb + 1/10 * y_pred_et<prepare_output>
X_train = train_.drop('Survived',axis=1)
 y_train = train_['Survived']
y_pred = y_pred_cb<predict_on_test>
X_test=test_
y_pred = rf_random.predict(X_test )<prepare_output>
X_train=X_train.drop(columns=['Titles_ the Countess','Titles_ Sir','Titles_ Mme',
'Titles_ Mlle','Titles_ Major','Titles_ Lady','Titles_ Jonkheer',
'Titles_ Don','Titles_ Capt'])
 X_test=X_test.drop(columns=['Titles_ Dona'] )
y_first = np.expm1(y_pred )<prepare_output>
dtree = DecisionTreeClassifier()
dtree.fit(X_train,y_train )
y_pred = np.expm1(y_first )<create_dataframe>
predictions_dtree = dtree.predict(X_test )
my_submission = pd.DataFrame({'Id': test.index, 'SalePrice': y_pred} )<save_to_csv>
y_test=gender_submission['Survived'].values
my_submission.to_csv('submission.csv', index=False )<create_dataframe>
print(classification_report(y_test,predictions_dtree))
processed = X.copy()
processed['SalePrice'] = y<split>
print(confusion_matrix(y_test,predictions_dtree))
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = int(len(processed)*0.2)
 n_trn = len(df)-n_valid
raw_train, raw_valid = split_vals(processed, n_trn)
 X_train, X_valid = split_vals(X, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train.shape, y_train.shape, X_valid.shape<compute_test_metric>
rfc = RandomForestClassifier(n_estimators=300)
 rfc.fit(X_train,y_train )
def print_score(m):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<compute_test_metric>
predictions_rfc = rfc.predict(X_test )
print_score(m )<train_model>
print(classification_report(y_test,predictions_rfc))
m = ExtraTreesRegressor(n_jobs=-1, bootstrap=True, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )<train_model>
print(confusion_matrix(y_test,predictions_rfc))
m = RandomForestRegressor(n_jobs=-1, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )<split>
test['Survived']=predictions_rfc
test.head()
df_trn, y_trn, nas = proc_df(df, 'SalePrice')
 X_train, _ = split_vals(df_trn, n_trn)
 y_train, _ = split_vals(y_trn, n_trn)
 X_train.shape, y_train.shape, X_valid.shape<choose_model_class>
predictions=test[['PassengerId','Survived']]
<train_model><EOS> 
predictions.to_csv('output.csv', index=False )