Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
%matplotlib inline
sns.set()
warnings.filterwarnings("ignore" )
cols = np.array(d_train.columns[d_train.dtypes != object])
 d = defaultdict(LabelEncoder)
 train = train.apply(lambda x: d[x.name].fit_transform(x))
test = test.apply(lambda x: d[x.name].transform(x))
train[cols] = d_train[cols]
test[np.delete(cols,len(cols)-1)]=d_test[np.delete(cols,len(cols)-1)]<feature_engineering>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )
train["O_Style"] = train["Condition1"] + train["Condition2"] + train["BldgType"] + train["HouseStyle"]+ train["RoofStyle"] + train["MasVnrType"]
train["O_Neighbor"] = train["Street"] + train["LotShape"] + train["Neighborhood"] * train["Condition1"]
train["O_Street"] = train["Street"] * train["LotShape"] + train["LandContour"] * train["LotConfig"]
train["SqFtPerRoom"] = train["GrLivArea"] /(train["TotRmsAbvGrd"] + train["FullBath"] +train["HalfBath"] + train["KitchenAbvGr"])
 train["HighQualSF"] = train["1stFlrSF"] + train["2ndFlrSF"] + train["TotalBsmtSF"]
train["Years_Years"] = train["YrSold"] - train["YearRemodAdd"]
train['Total_Home_Quality'] = train['OverallQual'] + train['OverallCond']
train['Total_Bathrooms'] =(train['FullBath'] +(0.5 * train['HalfBath'])+ train['BsmtFullBath'] +(0.5 * train['BsmtHalfBath']))
test["O_Style"] = test["Condition1"] + test["Condition2"] + test["BldgType"] + test["HouseStyle"]+ test["RoofStyle"] + test["MasVnrType"]
test["O_Neighbor"] = test["Street"] + test["LotShape"] + test["Neighborhood"] * test["Condition1"]
test["O_Street"] = test["Street"] * test["LotShape"] + test["LandContour"] * test["LotConfig"]
test["SqFtPerRoom"] = test["GrLivArea"] /(test["TotRmsAbvGrd"] + test["FullBath"] +test["HalfBath"] + test["KitchenAbvGr"])
 test["HighQualSF"] = test["1stFlrSF"] + test["2ndFlrSF"] + test["TotalBsmtSF"]
test["Years_Years"] = test["YrSold"] - test["YearRemodAdd"]
test['Total_Home_Quality'] = test['OverallQual'] + test['OverallCond']
test['Total_Bathrooms'] =(test['FullBath'] +(0.5 * test['HalfBath'])+ test['BsmtFullBath'] +(0.5 * test['BsmtHalfBath']))<drop_column>
train.isnull().sum()
train = train.drop(['Id'], axis=1)
 test = test.drop(['Id'], axis=1 )<import_modules>
test.isnull().sum()
from sklearn import metrics
from sklearn import model_selection
from sklearn import preprocessing
from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.metrics import plot_confusion_matrix
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost.sklearn import XGBRegressor
from catboost import CatBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor,AdaBoostRegressor,BaggingRegressor, RandomForestRegressor
import xgboost as xgb
import lightgbm as lgb<split>
test.isnull().sum()
X = train.drop(columns=['SalePrice'] ).values
y = np.log1p(train["SalePrice"])
 Z = test.values
X_train, X_test1, y_train, y_test1 = train_test_split(X, y, test_size = 0.0001, random_state = 42)
 X_train1, X_test, y_train1, y_test = train_test_split(X, y, test_size = 0.5, random_state = 42 )<train_model>
survived = train[train['Survived'] == 1]
not_survived = train[train['Survived'] == 0]
print("Survived: %i(%.1f%%)"%(len(survived), float(len(survived)) /len(train)*100.0))
print("Not Survived: %i(%.1f%%)"%(len(not_survived), float(len(not_survived)) /len(train)*100.0))
print("Total: %i"%len(train))
Acc = pd.DataFrame(index=None, columns=['model','Root Mean Squared Error','Accuracy on Traing set','Accuracy on Testing set'])
 regressors = [['DecisionTreeRegressor',DecisionTreeRegressor() ],
['XGBRegressor', XGBRegressor() ],
['CatBoostRegressor', CatBoostRegressor(verbose= False)],
['LGBMRegressor',lgb.LGBMRegressor() ],
['GradientBoostingRegressor',GradientBoostingRegressor() ],
['ExtraTreesRegressor',ExtraTreesRegressor() ]]
for mod in regressors:
name = mod[0]
model = mod[1]
model.fit(X_train1,y_train1)
 ATrS = model.score(X_train1,y_train1)
 ATeS = model.score(X_test,y_test)
 RMSE = mean_squared_error(y_test, model.predict(X_test))
Acc = Acc.append(pd.Series({'model':name, 'Root Mean Squared Error': RMSE,'Accuracy on Traing set':ATrS,'Accuracy on Testing set':ATeS}),ignore_index=True)
 Acc.sort_values(by='Root Mean Squared Error' )<train_model>
train.Pclass.value_counts()
def modelFitter(maxDepth, num_leaves, learning_rate, n_estimators):
model = lgb.LGBMRegressor(learning_rate=learning_rate,
num_leaves=num_leaves.astype("int32"),
max_depth=maxDepth.astype("int32"),
bagging_freq=bagging_freq.astype("int32"))
evalSet = [(X_test, y_test)]
model.fit(X_train, y_train, eval_metric="rmse", eval_set=evalSet, early_stopping_rounds=50, verbose=False)
 bestScore = model.best_score_[list(model.best_score_.keys())[0]]['rmse']
return -bestScore
pbounds = {'maxDepth':(1,5),'num_leaves':(10, 35),'learning_rate':(0.005, 0.01), 'bagging_freq':(4, 6)} 
<compute_train_metric>
pclass_survived = train.groupby('Pclass' ).Survived.value_counts()
pclass_survived
LGBMR = lgb.LGBMRegressor(objective='regression', num_leaves=6,learning_rate=0.02, n_estimators=2000,max_bin=100,
bagging_fraction=0.8,bagging_freq=4, bagging_seed=8,feature_fraction=0.2,feature_fraction_seed=8,
min_sum_hessian_in_leaf = 11,verbose=-1,random_state=42)
 LGBMR.fit(X_train,y_train)
 ATrS = LGBMR.score(X_train,y_train)
 ATeS = LGBMR.score(X_test,y_test)
 RMSE = mean_squared_error(y_test, LGBMR.predict(X_test))
print("Root Mean Squared: {}, Accuracy Train set: {},Accuracy Test set: {}".format(RMSE, ATrS, ATeS))<choose_model_class>
pclass_survived_average = train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()
pclass_survived_average
gbr = GradientBoostingRegressor()
params = {'loss': ['huber'],
'learning_rate': [0.012, 0.015, 0.02],
'max_depth': [3, 4, 5],
'min_samples_leaf' : [10, 12, 15],
'min_samples_split' : [3, 5, 7]} 
<train_model>
train.Sex.value_counts()
GBR = GradientBoostingRegressor(n_estimators=1000,learning_rate=0.012,max_depth=6,max_features='sqrt',min_samples_leaf=10,
min_samples_split=4,loss='huber',random_state=12)
 GBR.fit(X_train,y_train)
 ATrS = GBR.score(X_train,y_train)
 ATeS = GBR.score(X_test,y_test)
 RMSE = mean_squared_error(y_test, GBR.predict(X_test))
print("Root Mean Squared: {}, Accuracy Train set: {},Accuracy Test set: {}".format(RMSE, ATrS, ATeS))<train_on_grid>
sex_survival = train.groupby('Sex' ).Survived.value_counts()
sex_survival
train_data = X_train
train_labels = y_train
model = CatBoostRegressor()
grid = {'iterations': [4000, 7500],
'learning_rate': [0.003, 0.005, 0.007],
'depth': [3, 7, 10],
'l2_leaf_reg': [1, 2],
'random_seed': [12]} 
<compute_train_metric>
sex_survived_average = train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()
sex_survived_average
params = {'iterations': 5500,
'learning_rate': 0.005,
'loss_function':'RMSE',
'depth': 8,
'l2_leaf_reg': 1,
'eval_metric':'RMSE',
'verbose': False,
'random_seed': 12}
CBR = CatBoostRegressor(**params)
 CBR.fit(X_train,y_train)
 ATrS = CBR.score(X_train,y_train)
 ATeS = CBR.score(X_test,y_test)
 RMSE = mean_squared_error(y_test, CBR.predict(X_test))
print("Root Mean Squared: {}, Accuracy Train set: {},Accuracy Test set: {}".format(RMSE, ATrS, ATeS))<compute_train_metric>
train.Embarked.value_counts()
dtrain = xgb.DMatrix(X_train, label=y_train)
 dtest = xgb.DMatrix(X_test)
 def xgb_evaluate(learning_rate, max_depth, gamma, subsample, colsample_bytree, reg_alpha):
params = {'learning_rate':learning_rate,
'max_depth': int(max_depth),
'gamma': gamma,
'subsample':subsample,
'colsample_bytree': colsample_bytree,
'reg_alpha':reg_alpha}
cv_result = xgb.cv(params, dtrain, num_boost_round=100, nfold=3)
 return -1.0 * cv_result['test-rmse-mean'].iloc[-1] 
<compute_train_metric>
train.groupby('Embarked' ).Survived.value_counts()
XGBR = xgb.XGBRegressor(colsample_bytree=0.5149443835418306, gamma=0.0,learning_rate=0.03,max_depth=4,
reg_alpha=0.005412343201815549, subsample=0.7119381043613345, n_estimators=1200,
min_child_weight=0,nthread=-1,scale_pos_weight=1,seed=27,random_state=42)
 XGBR.fit(X_train,y_train)
 ATrS = XGBR.score(X_train,y_train)
 ATeS = XGBR.score(X_test,y_test)
 RMSE = mean_squared_error(y_test, XGBR.predict(X_test))
print("Root Mean Squared: {}, Accuracy Train set: {},Accuracy Test set: {}".format(RMSE, ATrS, ATeS))<save_to_csv>
train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean()
result =(np.expm1(GBR.predict(Z)) + np.expm1(LGBMR.predict(Z)) + np.expm1(CBR.predict(Z)) + np.expm1(XGBR.predict(Z)))/4
sub = pd.DataFrame()
sub = pd.DataFrame({'Id':d_test.Id,'SalePrice':result})
 sub.to_csv('submission.csv',index=False)
 sub.head(2 )<load_from_csv>
train.Parch.value_counts()
warnings.filterwarnings(action="ignore")
 target_name = 'SalePrice'
dataset_train_raw = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 dataset_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 dataset_train_raw<prepare_x_and_y>
train.groupby('Parch' ).Survived.value_counts()
ignore_feature = ['Id']
y_train = dataset_train_raw[target_name]
dataset_train = dataset_train_raw.drop([target_name] + ignore_feature, axis=1, inplace=False)
 dataset_test.drop(ignore_feature, axis=1, inplace=True)
 all_data = pd.concat([dataset_train, dataset_test], axis=0, sort=False)
 all_data<define_variables>
train[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean()
specially_missed = ['Alley',
'PoolQC',
'MiscFeature',
'Fence',
'FireplaceQu',
'GarageType',
'GarageFinish',
'GarageQual',
'GarageCond',
'BsmtQual',
'BsmtCond',
'BsmtExposure',
'BsmtFinType1',
'BsmtFinType2',
'MasVnrType']
for feature in specially_missed:
all_data[feature] = all_data[feature].fillna('None' )<define_variables>
train.SibSp.value_counts()
numeric_missed = ['BsmtFinSF1',
'BsmtFinSF2',
'BsmtUnfSF',
'TotalBsmtSF',
'BsmtFullBath',
'BsmtHalfBath',
'GarageYrBlt',
'GarageArea',
'GarageCars',
'MasVnrArea']
for feature in numeric_missed:
all_data[feature] = all_data[feature].fillna(0 )<data_type_conversions>
train.groupby('SibSp' ).Survived.value_counts()
all_data['MSSubClass'] = all_data['MSSubClass'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )<categorify>
train[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean()
all_data['Functional'] = all_data['Functional'].fillna('Typ')
 all_data['Utilities'] = all_data['Utilities'].fillna('AllPub')
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna('TA')
 all_data['Electrical'] = all_data['Electrical'].fillna('SBrkr')
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
 all_data['LotFrontage'] = all_data.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.mean()))
all_data['MSZoning'] = all_data.groupby('MSSubClass')['MSZoning'].transform(lambda x: x.fillna(x.mode() [0]))<sort_values>
total_survived = train[train['Survived']==1]
total_not_survived = train[train['Survived']==0]
male_survived = train[(train['Survived']==1)&(train['Sex']=="male")]
female_survived = train[(train['Survived']==1)&(train['Sex']=="female")]
male_not_survived = train[(train['Survived']==0)&(train['Sex']=="male")]
female_not_survived = train[(train['Survived']==0)&(train['Sex']=="female")]
numeric_feats = all_data.dtypes[all_data.dtypes != 'object'].index
skewed_feats = all_data[numeric_feats].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skewed_feats[abs(skewed_feats)> 0.5]
high_skew<feature_engineering>
train_test_data = [train, test]
for dataset in train_test_data:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.' )
for feature in high_skew.index:
all_data[feature] = np.log1p(all_data[feature] )<feature_engineering>
for dataset in train_test_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col', \
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
all_data['SqFtPerRoom'] = all_data['GrLivArea'] /(all_data['TotRmsAbvGrd'] + all_data['FullBath'] +
all_data['HalfBath'] + all_data['KitchenAbvGr'])
 all_data['TotalHomeQuality'] = all_data['OverallQual'] + all_data['OverallCond']
all_data['TotalBathrooms'] =(all_data['FullBath'] +(0.5 * all_data['HalfBath'])+
all_data['BsmtFullBath'] +(0.5 * all_data['BsmtHalfBath']))<categorify>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Other": 5}
for dataset in train_test_data:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0 )
X_all = pd.get_dummies(all_data)
 X_train = X_all[:len(y_train)]
X_test = X_all[len(y_train):]
X_train<import_modules>
for dataset in train_test_data:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int )
kf = KFold(n_splits=8, random_state=42, shuffle=True)
 def cv_rmse(model):
return -cross_val_score(model, X_train, y_train, scoring='neg_root_mean_squared_error', cv=kf)
 models = ['Linear', 'SVR', 'Random_Forest', 'XGBR', 'Cat_Boost', 'Ridge', 'Elastic_Net', 'Lasso', 'Stack']
scores = []
lin = LinearRegression()
score_lin = cv_rmse(lin)
 scores.append(score_lin.mean())
 svr = SVR()
score_svr = cv_rmse(svr)
 scores.append(score_svr.mean())
 rfr = RandomForestRegressor()
score_rfr = cv_rmse(rfr)
 scores.append(score_rfr.mean())
 xgb = xg.XGBRegressor()
score_xgb = cv_rmse(xgb)
 scores.append(score_xgb.mean())
 catb = CatBoostRegressor(verbose=0, allow_writing_files=False)
 score_catb = cv_rmse(catb)
 scores.append(score_catb.mean())
 rid = Ridge()
score_rid = cv_rmse(rid)
 scores.append(score_rid.mean())
 el = ElasticNet()
score_el = cv_rmse(el)
 scores.append(score_el.mean())
 las = Lasso()
score_las = cv_rmse(las)
 scores.append(score_las.mean())
 stack_gen = StackingRegressor(regressors=(CatBoostRegressor(verbose=0, allow_writing_files=False),
Ridge() ,
xg.XGBRegressor() ,
RandomForestRegressor()),
meta_regressor=CatBoostRegressor(verbose=0, allow_writing_files=False),
use_features_in_secondary=True)
 score_stack_gen = cv_rmse(stack_gen)
 scores.append(score_stack_gen.mean())
 cv_score = pd.DataFrame(models, columns=['Regressors'])
 cv_score['RMSE_mean'] = scores
cv_score<train_on_grid>
train.Embarked.value_counts()
predictions = {}
def xgbr(X_train, y_train, X_test):
xgbrM = xg.XGBRegressor()
params = {'max_depth': [3, 4, 5, 6, 7, 8],
'min_child_weight': [0, 4, 5, 6, 7, 8],
'learning_rate': [0.01, 0.05, 0.1, 0.2, 0.25, 0.8, 1],
'n_estimators': [10, 30, 50, 100, 200, 400, 1000]}
grid_search_xg = RandomizedSearchCV(estimator=xgbrM, scoring='neg_root_mean_squared_error', param_distributions=params, n_iter=200, cv=4, verbose=2,
random_state=42, n_jobs=-1)
 grid_search_xg.fit(X_train, y_train)
 xgbrModel = grid_search_xg.best_estimator_
print('Best params(XGBR):',grid_search_xg.best_params_)
 print('RMSE(XGBR):', -grid_search_xg.best_score_)
 return xgbrModel
xgbrModel = xg.XGBRegressor(n_estimators=400, min_child_weight=5, max_depth=7, learning_rate=0.05)
 xgbrModel.fit(X_train, y_train)
 predictions['XGBR'] = xgbrModel.predict(X_test )<train_on_grid>
for dataset in train_test_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S' )
def ridge(X_train, y_train, X_test):
alpha_ridge = {'alpha': [-3, -2, -1, 1e-15, 1e-10, 1e-8, 1e-5, 1e-4, 1e-3, 1e-2, 0.5, 1, 1.5, 2, 3, 4, 5, 10, 20, 30, 40]}
rd = Ridge()
grid_search_rd = GridSearchCV(estimator=rd, scoring='neg_root_mean_squared_error', param_grid=alpha_ridge, cv=4, n_jobs=-1, verbose=3)
 grid_search_rd.fit(X_train, y_train)
 ridgeModel = grid_search_rd.best_estimator_
print('Best params(Ridge):', grid_search_rd.best_params_)
 print('RMSE(Ridge):', -grid_search_rd.best_score_)
 return ridgeModel
ridgeModel = Ridge(alpha=10)
 ridgeModel.fit(X_train, y_train)
 predictions['Ridge'] = ridgeModel.predict(X_test )<train_on_grid>
for dataset in train_test_data:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int )
def catBoost(X_train, y_train, X_test):
catM = CatBoostRegressor(verbose=0, allow_writing_files=False)
 params = {'learning_rate': [0.01, 0.05, 0.005, 0.0005],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 2, 3, 5, 9]}
grid_search_cat = RandomizedSearchCV(estimator=catM, scoring='neg_root_mean_squared_error', param_distributions=params, n_iter=10, cv=4, verbose=2,
random_state=42, n_jobs=-1)
 grid_search_cat.fit(X_train, y_train)
 catModel = grid_search_cat.best_estimator_
print('Best params(CatBoost):',grid_search_cat.best_params_)
 print('RMSE(CatBoost):', -grid_search_cat.best_score_)
 return catModel
catModel = CatBoostRegressor(verbose=0, allow_writing_files=False, learning_rate=0.05, l2_leaf_reg=2, depth=4)
 catModel.fit(X_train, y_train)
 predictions['CatBoost'] = catModel.predict(X_test )<define_variables>
for dataset in train_test_data:
age_avg = dataset['Age'].mean()
age_std = dataset['Age'].std()
age_null_count = dataset['Age'].isnull().sum()
age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 train['AgeBand'] = pd.cut(train['Age'], 5)
 print(train[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean() )
final_prediction = 0.25 * predictions['XGBR'] + 0.35 * predictions['CatBoost'] + 0.4 * predictions['Ridge']<save_to_csv>
for dataset in train_test_data:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
result = pd.DataFrame([len(y_train)+ 1 + i for i in range(len(X_test)) ], columns=['Id'])
 result[target_name] = np.expm1(final_prediction)
 result.to_csv('result.csv', index=False, header=True)
 result<set_options>
for dataset in train_test_data:
dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median() )
warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
for dataset in train_test_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, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int )
df_train=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 df_train.shape<load_from_csv>
for dataset in train_test_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
print(train[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean() )
df_test=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 df_test.shape<concatenate>
for dataset in train_test_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
print(train[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean() )
train_test=pd.concat([df_train,df_test],axis=0,sort=False)
 train_test.head()<define_variables>
features_drop = ['Name', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'FamilySize']
train = train.drop(features_drop, axis=1)
 test = test.drop(features_drop, axis=1)
 train = train.drop(['PassengerId', 'AgeBand', 'FareBand'], axis=1 )
sales= train_test['SalePrice']
Id= train_test['Id']<drop_column>
X_train = train.drop('Survived', axis=1)
 y_train = train['Survived']
X_test = test.drop("PassengerId", axis=1 ).copy()
X_train.shape, y_train.shape, X_test.shape
train_test= train_test.drop(columns=['SalePrice'])
 train_test= train_test.drop(columns=['Id'])
 train_test.head()<sort_values>
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
total = train_test.isnull().sum().sort_values(ascending=False)
 percent =(train_test.isnull().sum() /train_test.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<filter>
clf = LogisticRegression()
clf.fit(X_train, y_train)
 y_pred_log_reg = clf.predict(X_test)
 acc_log_reg = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_log_reg)+ '%' )
df_numeric=train_test.select_dtypes(include=np.number)
 df_numeric<sort_values>
clf = SVC()
clf.fit(X_train, y_train)
 y_pred_svc = clf.predict(X_test)
 acc_svc = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_svc)+ '%' )
total = df_numeric.isnull().sum().sort_values(ascending=False)
 percent =(df_numeric.isnull().sum() /df_numeric.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(5 )<sort_values>
clf = LinearSVC()
clf.fit(X_train, y_train)
 y_pred_linear_svc = clf.predict(X_test)
 acc_linear_svc = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_linear_svc)+ '%' )
total = df_obj.isnull().sum().sort_values(ascending=False)
 percent =(df_obj.isnull().sum() /df_obj.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(5 )<drop_column>
clf = KNeighborsClassifier(n_neighbors = 3)
 clf.fit(X_train, y_train)
 y_pred_knn = clf.predict(X_test)
 acc_knn = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_knn)+ '%' )
df_obj = df_obj.drop(columns=['PoolQC','MiscFeature','Alley','Fence','FireplaceQu'])
 df_obj<data_type_conversions>
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
 y_pred_decision_tree = clf.predict(X_test)
 acc_decision_tree = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_decision_tree)+ '%' )
for column in df_obj.columns:
df_obj[column] = df_obj[column].astype('category')
 df_obj[column] = df_obj[column].cat.codes<concatenate>
clf = RandomForestClassifier(n_estimators=100)
 clf.fit(X_train, y_train)
 y_pred_random_forest = clf.predict(X_test)
 acc_random_forest = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_random_forest)+ '%' )
df_new = pd.concat([df_numeric, df_obj], axis=1)
 df_new<concatenate>
clf = GaussianNB()
clf.fit(X_train, y_train)
 y_pred_gnb = clf.predict(X_test)
 acc_gnb = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_gnb)+ '%' )
df1 = pd.concat([df_new, sales], axis=1)
 df2 = pd.concat([Id , df1], axis=1)
 df2<prepare_x_and_y>
clf = Perceptron(max_iter=5, tol=None)
 clf.fit(X_train, y_train)
 y_pred_perceptron = clf.predict(X_test)
 acc_perceptron = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_perceptron)+ '%' )
X= train.drop('SalePrice', axis=1)
 y = train['SalePrice']<import_modules>
clf = SGDClassifier(max_iter=5, tol=None)
 clf.fit(X_train, y_train)
 y_pred_sgd = clf.predict(X_test)
 acc_sgd = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_sgd)+ '%' )
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import cross_val_score<choose_model_class>
clf = RandomForestClassifier(n_estimators=100)
 clf.fit(X_train, y_train)
 y_pred_random_forest_training_set = clf.predict(X_train)
 acc_random_forest = round(clf.score(X_train, y_train)* 100, 2)
 print("Accuracy: %i %%
"%acc_random_forest)
 class_names = ['Survived', 'Not Survived']
cnf_matrix = confusion_matrix(y_train, y_pred_random_forest_training_set)
 np.set_printoptions(precision=2)
 print('Confusion Matrix in Numbers')
 print(cnf_matrix)
 print('')
 cnf_matrix_percent = cnf_matrix.astype('float')/ cnf_matrix.sum(axis=1)[:, np.newaxis]
print('Confusion Matrix in Percentage')
 print(cnf_matrix_percent)
 print('')
 true_class_names = ['True Survived', 'True Not Survived']
predicted_class_names = ['Predicted Survived', 'Predicted Not Survived']
df_cnf_matrix = pd.DataFrame(cnf_matrix,
index = true_class_names,
columns = predicted_class_names)
 df_cnf_matrix_percent = pd.DataFrame(cnf_matrix_percent,
index = true_class_names,
columns = predicted_class_names)
 plt.figure(figsize =(15,5))
plt.subplot(121)
 sns.heatmap(df_cnf_matrix, annot=True, fmt='d', cmap = "Blues")
 plt.subplot(122)
 sns.heatmap(df_cnf_matrix_percent, annot=True, cmap = "Blues" )
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )<train_model>
models = pd.DataFrame({
'Model': ['LR', 'SVM', 'L-SVC',
'KNN', 'DTree', 'RF', 'NB',
'Perceptron', 'SGD'],
'Score': [acc_log_reg, acc_svc, acc_linear_svc,
acc_knn, acc_decision_tree, acc_random_forest, acc_gnb,
acc_perceptron, acc_sgd]
})
 models = models.sort_values(by='Score', ascending=False)
 models
<compute_train_metric><EOS> 
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": y_pred_random_forest
})
 submission.to_csv('gender_submission.csv', index=False )