Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
%matplotlib inline
warnings.filterwarnings("ignore")
 print('Versions:')
 print(' python', platform.python_version())
 n =('numpy', 'pandas', 'sklearn', 'matplotlib', 'seaborn')
 nn =(np, pd, sklearn, mpl, sns)
 for a, b in zip(n, nn):
print(' --', str(a), b.__version__ )
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y<init_hyperparams>
pd.set_option('colheader_justify', 'left')
 pd.set_option('precision', 0)
 pd.options.display.float_format = '{:,.2f}'.format
pd.set_option('display.max_colwidth', -1 )
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 13,
"learning_rate" : 0.01,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 80,
"min_sum_heassian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : random_state,
"verbosity" : 1,
"seed": random_state
}<create_dataframe>
sns.set_style('whitegrid', { 'axes.axisbelow': True, 'axes.edgecolor': 'black', 'axes.facecolor': 'white',
'axes.grid': True, 'axes.labelcolor': 'black', 'axes.spines.bottom': True, 'axes.spines.left': True,
'axes.spines.right': False, 'axes.spines.top': False, 'figure.facecolor': 'white',
'grid.color': 'grey', 'grid.linestyle': ':', 'image.cmap': 'rocket', 'lines.solid_capstyle': 'round',
'patch.edgecolor': 'w', 'patch.force_edgecolor': True, 'text.color': 'black',
'xtick.top': False, 'xtick.bottom': True, 'xtick.color': 'navy', 'xtick.direction': 'out',
'ytick.right': False, 'ytick.left': True, 'ytick.color': 'navy', 'ytick.direction': 'out'} )
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = df_train[['ID_code', 'target']]
oof['predict'] = 0
predictions = df_test[['ID_code']]
val_aucs = []
feature_importance_df = pd.DataFrame()<prepare_x_and_y>
from sklearn import svm, tree, linear_model, neighbors, naive_bayes, ensemble, discriminant_analysis, gaussian_process
from sklearn import feature_selection, model_selection, metrics
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from xgboost import XGBClassifier
features = [col for col in df_train.columns if col not in ['target', 'ID_code']]
X_test = df_test[features].values<split>
train_raw = pd.read_csv('.. /input/titanic/train.csv')
 test_raw = pd.read_csv('.. /input/titanic/test.csv')
 len(train_raw)+ len(test_raw )
for fold,(trn_idx, val_idx)in enumerate(skf.split(df_train, df_train['target'])) :
X_train, y_train = df_train.iloc[trn_idx][features], df_train.iloc[trn_idx]['target']
X_valid, y_valid = df_train.iloc[val_idx][features], df_train.iloc[val_idx]['target']
N = 5
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values)
 X_t = pd.DataFrame(X_t)
 X_t = X_t.add_prefix('var_')
 trn_data = lgb.Dataset(X_t, label=y_t)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=3000,
verbose_eval = 1000,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_clf.feature_importance()
fold_importance_df["fold"] = fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N<compute_test_metric>
df = pd.concat(objs=[train_raw, test_raw], axis=0)
 df.shape
mean_auc = np.mean(val_aucs)
 std_auc = np.std(val_aucs)
 all_auc = roc_auc_score(oof['target'], oof['predict'])
 print("Mean auc: %.9f, std: %.9f.All auc: %.9f." %(mean_auc, std_auc, all_auc))<save_to_csv>
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('lgb_all_predictions.csv', index=None)
 sub_df = pd.DataFrame({"ID_code":df_test["ID_code"].values})
 sub_df["target"] = predictions['target']
sub_df.to_csv("lgb_submission.csv", index=False)
 oof.to_csv('lgb_oof.csv', index=False )<import_modules>
col1 = test_raw['PassengerId']
df.drop(['PassengerId', 'Cabin'], axis=1, inplace=True )
import pandas as pd
from tqdm import tqdm<load_from_csv>
df['Age'] = df['Age'].fillna(df['Age'].median())
 df['Fare'] = df['Fare'].fillna(df['Fare'].mean())
 df['Embarked'] = df['Embarked'].fillna('S')
 df.isnull().sum().to_frame().T
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
df['Fsize'] = df['Parch'] + df['SibSp'] + 1
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )<feature_engineering>
df['Surname'], df['Name'] = zip(*df['Name'].apply(lambda x: x.split(',')))
 df['Title'], df['Name'] = zip(*df['Name'].apply(lambda x: x.split('.')))
 titles =(df['Title'].value_counts() < 10)
 df['Title'] = df['Title'].apply(lambda x: ' Misc' if titles.loc[x] == True else x)
 df['Title'].value_counts().to_frame().T
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
df['Tname'] = df['Ticket']
df['Tset']=0
for t in df['Tname'].unique() :
if df['Surname'].loc[(df['Tname']==t)].nunique() != 1:
df['Tset'].loc[(df['Tname']==t)] = 'mixed'
else:
df['Tset'].loc[(df['Tname']==t)] = 'monotonic'
for t in df['Tname'].unique() :
if df['Surname'].loc[(df['Tname']==t)].nunique() != 1:
df['Tset'].loc[(df['Tname']==t)] = 'mixed'
else:
df['Tset'].loc[(df['Tname']==t)] = 'monotonic'
submission.to_csv('submission.csv', index=False )<save_to_csv>
for t in df['Ticket'].unique() :
df['Ticket'].loc[(df['Ticket']==t)] = len(df.loc[(df['Ticket']==t)])
 df['Price'] = df['Fare'] / df['Ticket']
df.rename(columns={'Ticket':'Tgroup'}, inplace=True )
submission.to_csv('submission.csv', index=False )<load_from_csv>
df.drop(['Parch', 'SibSp', 'Name', 'Surname', 'Tname', 'Fare'], axis=1, inplace=True )
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")
 data = pd.concat([train, test], ignore_index=True )<sort_values>
label = LabelEncoder()
cols = df.dtypes[df.dtypes == 'object'].index.tolist()
for col in cols:
df[col] = label.fit_transform(df[col] )
percent_null = data.isnull().sum() /len(data)*100
percent_null = percent_null[percent_null>0]
print(percent_null.sort_values() )<count_missing_values>
df['Price'] = pd.qcut(df['Price'], 4)
 df['Age'] = pd.cut(df['Age'].astype(int), 5 )
def null_cols(dataframe):
for col in dataframe.columns:
null_count = dataframe[col].isnull().sum()
if null_count > 0:
percent_null = null_count/len(dataframe[col])*100
print(f"{col} percent null: {round(percent_null,3)}" )<count_missing_values>
df['Age'] = label.fit_transform(df['Age'])
 df['Price'] = label.fit_transform(df['Price'] )
null_cols(num_data )<feature_engineering>
a = len(train_raw)
 train = df[:a]
test = df[a:]
data.LotFrontage.fillna(np.mean(data.LotFrontage), inplace=True)
 data.MasVnrArea.fillna(np.mean(data.MasVnrArea), inplace=True)
 data.BsmtFinSF1.fillna(np.mean(data.BsmtFinSF1), inplace=True)
 data.BsmtFinSF2.fillna(np.mean(data.BsmtFinSF2), inplace=True)
 data.BsmtUnfSF.fillna(np.mean(data.BsmtUnfSF), inplace=True)
 data.TotalBsmtSF.fillna(np.mean(data.TotalBsmtSF), inplace=True)
 data.BsmtFullBath.fillna(np.mean(data.BsmtFullBath), inplace=True)
 data.BsmtHalfBath.fillna(np.mean(data.BsmtHalfBath), inplace=True)
 data.GarageYrBlt.fillna(np.mean(data.GarageYrBlt), inplace=True)
 data.GarageCars.fillna(np.mean(data.GarageCars), inplace=True)
 data.GarageArea.fillna(np.mean(data.GarageArea), inplace=True )<categorify>
test.drop(['Survived'], axis=1, inplace=True)
 test_raw.shape[0] == test.shape[0]
encoder = OneHotEncoder()
temp = pd.DataFrame(encoder.fit_transform(data[['MSSubClass']] ).toarray() , columns=['MS20','MS30','MS40','MS45','MS50','MS60','MS70','MS75','MS80','MS85','MS90','MS120','MS150','MS160','MS180','MS190'])
 data = data.join(temp)
 data.drop('MSSubClass', 1, inplace=True)
 temp = pd.DataFrame(encoder.fit_transform(data[['MoSold']] ).toarray() , columns=['SoldJan','SoldFeb','SoldMar','SoldApr','SoldMay','SoldJun','SoldJul','SoldAug','SoldSep','SoldOct','SoldNov','SoldDec'])
 data = data.join(temp)
 data.drop('MoSold', 1, inplace=True)
 temp = pd.DataFrame(encoder.fit_transform(data[['YrSold']] ).toarray() , columns=['Sold2006', 'Sold2007', 'Sold2008', 'Sold2009', 'Sold2010'])
 data = data.join(temp)
 data.drop('YrSold', 1, inplace=True )<categorify>
X = train.drop(['Survived'], axis=1 ).columns.to_list()
y = ['Survived']
data['LotShape'].replace({"IR3": 1, 'IR2': 2, 'IR1': 3, 'Reg': 4}, inplace=True)
 data['Utilities'] = data['Utilities'].fillna('AllPub')
 data['Utilities'].replace({'NoSeWa': 1, 'AllPub': 2}, inplace=True)
 data['BldgType'].replace({"Twnhs": 1, 'TwnhsE': 2, 'Duplex': 3, '2fmCon': 4, '1Fam': 5}, inplace=True)
 data['ExterQual'].replace({"Fa": 1, 'TA': 2, 'Gd': 3, 'Ex': 4}, inplace=True)
 data['ExterCond'].replace({"Po": 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['BsmtQual'] = data['BsmtQual'].fillna(0)
 data['BsmtQual'].replace({'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['BsmtCond'] = data['BsmtCond'].fillna(0)
 data['BsmtCond'].replace({'Po': 1,'Fa': 2, 'TA': 3, 'Gd': 4}, inplace=True)
 data['BsmtExposure'] = data['BsmtExposure'].fillna(0)
 data['BsmtExposure'].replace({"No": 1, 'Mn': 2, 'Av': 3, 'Gd': 4}, inplace=True)
 data['BsmtFinType1'] = data['BsmtFinType1'].fillna(0)
 data['BsmtFinType1'].replace({"Unf": 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}, inplace=True)
 data['BsmtFinType2'] = data['BsmtFinType2'].fillna(0)
 data['BsmtFinType2'].replace({"Unf": 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}, inplace=True)
 data['HeatingQC'].replace({"Po": 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['CentralAir'].replace({"Y": 1, 'N': 0}, inplace=True)
 data['Electrical'] = data['Electrical'].fillna(0)
 data['Electrical'].replace({"Mix": 1, 'FuseP': 2, 'FuseF': 3, 'FuseA': 4, 'SBrkr': 5}, inplace=True)
 data['KitchenQual'] = data['KitchenQual'].fillna(0)
 data['KitchenQual'].replace({'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['Functional'] = data['Functional'].fillna(7)
 data['Functional'].replace({"Sev": 1, 'Maj2': 2, 'Maj1': 3, 'Mod': 4, 'Min2': 5, 'Min1': 6, 'Typ': 7}, inplace=True)
 data['FireplaceQu'] = data['FireplaceQu'].fillna(0)
 data['FireplaceQu'].replace({"Po": 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['GarageType'] = data['GarageType'].fillna(0)
 data['GarageType'].replace({"Detchd": 1, 'CarPort': 2, 'BuiltIn': 3, 'Basment': 4, 'Attchd': 5, '2Types': 6}, inplace=True)
 data['GarageFinish'] = data['GarageFinish'].fillna(0)
 data['GarageFinish'].replace({"Unf": 1, 'RFn': 2, 'Fin': 3}, inplace=True)
 data['GarageQual'] = data['GarageQual'].fillna('TA')
 data['GarageQual'].replace({"Po": 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['GarageCond'] = data['GarageCond'].fillna('TA')
 data['GarageCond'].replace({"Po": 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}, inplace=True)
 data['PavedDrive'].replace({"N": 1, 'P': 2, 'Y': 3}, inplace=True)
 data['PoolQC'] = data['PoolQC'].fillna(0)
 data['PoolQC'].replace({"Fa": 1, 'Gd': 2, 'Ex': 3}, inplace=True)
 data['Fence'] = data['Fence'].fillna(0)
 data['Fence'].replace({"MnWw": 1, 'GdWo': 2, 'MnPrv': 3, 'GdPrv': 4}, inplace=True )<categorify>
MLA = [
ensemble.AdaBoostClassifier() , ensemble.BaggingClassifier() , ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() , ensemble.RandomForestClassifier() ,
gaussian_process.GaussianProcessClassifier() ,
linear_model.LogisticRegressionCV() , linear_model.PassiveAggressiveClassifier() ,
linear_model.RidgeClassifierCV() , linear_model.SGDClassifier() , linear_model.Perceptron() ,
naive_bayes.BernoulliNB() , naive_bayes.GaussianNB() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True), svm.NuSVC(probability=True), svm.LinearSVC() ,
tree.DecisionTreeClassifier() , tree.ExtraTreeClassifier() ,
discriminant_analysis.LinearDiscriminantAnalysis() , discriminant_analysis.QuadraticDiscriminantAnalysis() ,
XGBClassifier() ]
data['MSZoning'] = data['MSZoning'].fillna('RL')
 temp = pd.DataFrame(encoder.fit_transform(data[['MSZoning']] ).toarray() , columns=['RLZone', 'RMZone', 'CZone', 'FVZone', 'RHZone'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Street']] ).toarray() , columns=['Pave','Grvl'])
 data = data.join(temp)
 data['Alley'] = data['Alley'].fillna('No alley access')
 temp = pd.DataFrame(encoder.fit_transform(data[['Alley']] ).toarray() , columns=['NoAlley','GrvlAlley','PvdAlley'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['LandContour']] ).toarray() , columns=['Lvl','Bnk','Low','HLS'])
 data = data.join(temp)
 data['NotLvl'] = data.Bnk + data.Low + data.HLS
data.drop(['Bnk', 'Low', 'HLS'], 1, inplace=True)
 temp = pd.DataFrame(encoder.fit_transform(data[['LotConfig']] ).toarray() , columns=['Inside','FR2','Corner','CulDSac','FR3'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['LandSlope']] ).toarray() , columns=['Gtl','Mod','Sev'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Neighborhood']] ).toarray() , columns=data.Neighborhood.unique())
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Condition1']] ).toarray() , columns=data.Condition1.unique())
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Condition2']] ).toarray() , columns=['Norm2', 'Artery2', 'RRNn2', 'Feedr2', 'PosN2', 'PosA2', 'RRAn2', 'RRAe2'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['HouseStyle']] ).toarray() , columns=['2Story','1Story','1.5Fin','1.5Unf','SFoyer','SLvl','2.5Unf','2.5Fin'])
 data = data.join(temp)
 data['Split'] = data.SLvl + data.SFoyer
data.drop(['SLvl', 'SFoyer'], 1, inplace=True)
 temp = pd.DataFrame(encoder.fit_transform(data[['RoofStyle']] ).toarray() , columns=['Gable','Hip','Gambrel','Mansard','Flat','Shed'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['RoofMatl']] ).toarray() , columns=['CompShg','WdShngl','Metal','WdShake','Membran','Tar&Grv','Roll','ClyTile'])
 data = data.join(temp)
 data['Exterior1st'] = data['Exterior1st'].fillna('Other')
 temp = pd.DataFrame(encoder.fit_transform(data[['Exterior1st']] ).toarray() , columns=['VinylSd1','MetalSd1','Wd Sdng1','HdBoard1','BrkFace1','WdShing1','CemntBd1','Plywood1','AsbShng1','Stucco1','BrkComm1','AsphShn1','Stone1','ImStucc1','CBlock1','Other1'])
 data = data.join(temp)
 data['Exterior2nd'] = data['Exterior2nd'].fillna('Other')
 temp = pd.DataFrame(encoder.fit_transform(data[['Exterior2nd']] ).toarray() , columns=['VinylSd2','MetalSd2','Wd Shng2','HdBoard2','Plywood2','Wd Sdng2','CmentBd2','BrkFace2','Stucco2','AsbShng2', 'Brk Cmn2','ImStucc2','AsphShn2','Stone2','Other2','CBlock2'])
 data['MasVnrType'] = data['MasVnrType'].fillna('None')
 temp = pd.DataFrame(encoder.fit_transform(data[['MasVnrType']] ).toarray() , columns=['BrkFace','NoMasVnr','StoneMasVnr','BrkCmn'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Foundation']] ).toarray() , columns=['PConc', 'CBlock', 'BrkTil', 'Wood', 'Slab', 'StoneFound'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['Heating']] ).toarray() , columns=['GasA', 'GasW', 'GravF', 'WallF', 'OthW', 'FloorF'])
 data = data.join(temp)
 data['SaleType'] = data['SaleType'].fillna('WD')
 temp = pd.DataFrame(encoder.fit_transform(data[['SaleType']] ).toarray() , columns=['WDSale', 'NewSale', 'CODSale', 'ConLDSale', 'ConLISale', 'CWDSale', 'ConLwSale', 'ConSale', 'OthSale'])
 data = data.join(temp)
 temp = pd.DataFrame(encoder.fit_transform(data[['SaleCondition']] ).toarray() , columns=['NormalCond', 'AbnormlCond', 'PartialCond', 'AdjLandCond', 'AllocaCond', 'FamilyCond'])
 data = data.join(temp)
 data.drop(['MiscFeature','MSZoning', 'Street', 'Alley', 'LandContour', 'LotConfig', 'LandSlope',
'Neighborhood', 'Condition1', 'Condition2', 'HouseStyle', 'RoofStyle',
'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Foundation',
'Heating', 'SaleType', 'SaleCondition'], 1, inplace=True)
<feature_engineering>
cv_split = model_selection.ShuffleSplit(n_splits = 10, test_size =.3, train_size =.6, random_state = 0)
 mla = pd.DataFrame(columns=['Name','TestScore','ScoreTime','FitTime','Parameters'])
 prediction = train[y]
i = 0
for alg in MLA:
name = alg.__class__.__name__
mla.loc[i, 'Name'] = name
mla.loc[i, 'Parameters'] = str(alg.get_params())
 cv_results = model_selection.cross_validate(alg, train[X], train[y], cv=cv_split)
 mla.loc[i, 'FitTime'] = cv_results['fit_time'].mean()
mla.loc[i, 'ScoreTime'] = cv_results['score_time'].mean()
mla.loc[i, 'TestScore'] = cv_results['test_score'].mean()
alg.fit(train[X], train[y])
 prediction[name] = alg.predict(train[X])
 i += 1
mla = mla.sort_values('TestScore', ascending=False ).reset_index(drop=True)
 mla
data['TotalBaths'] = data['FullBath'] + 0.5 * data['HalfBath'] + data['BsmtFullBath'] + 0.5 * data['BsmtHalfBath']
data['TotalSF'] = data['1stFlrSF'] + data['2ndFlrSF'] + data['TotalBsmtSF']
data['TotalPorchSF'] = data['OpenPorchSF'] + data['EnclosedPorch'] + data['3SsnPorch'] + data['ScreenPorch']
data['BsmtFinType'] = data['BsmtFinType1'] + data['BsmtFinType2']
data['BsmtFinSF'] = data['BsmtFinSF1'] + data['BsmtFinSF2']
data['GarageRating'] = data['GarageQual'] + data['GarageCond']
data['ExterRating'] = data['ExterQual'] + data['ExterCond']
data['OvrRating'] = data['OverallQual'] + data['OverallCond']
data.drop(['FullBath','HalfBath','BsmtFullBath','BsmtHalfBath','1stFlrSF','2ndFlrSF','TotalBsmtSF','OpenPorchSF','EnclosedPorch','3SsnPorch','ScreenPorch','BsmtFinType1','BsmtFinType2','BsmtFinSF1','BsmtFinSF2','GarageQual','GarageCond','ExterQual','ExterCond','OverallQual','OverallCond'], 1, inplace=True )<drop_column>
param_grid = {'criterion': ['gini', 'entropy'],
'max_depth': [2,4,6,8,10,None],
'random_state': [0]}
tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier() , param_grid=param_grid, scoring='roc_auc', cv=cv_split)
 tune_model.fit(train[X], train[y])
 print('Parameters: ', tune_model.best_params_ )
feature_cols = []
for col in data.columns:
feature_cols.append(col)
 feature_cols.remove('Id' )<data_type_conversions>
clf = tree.DecisionTreeClassifier()
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 clf.fit(train[X], train[y])
 results['test_score'].mean() *100
fs = feature_selection.RFECV(clf, step=1, scoring='accuracy', cv=cv_split)
 fs.fit(train[X], train[y])
 X = train[X].columns.values[fs.get_support() ]
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 print('Shape New: ', train[X].shape)
 print('Features to use: ', X )
test_index = data[data['SalePrice'].isnull() ].index.tolist()
test_index<prepare_x_and_y>
tuned = model_selection.GridSearchCV(tree.DecisionTreeClassifier() , param_grid=param_grid, scoring = 'roc_auc', cv=cv_split)
 tuned.fit(train[X], train[y])
 param_grid = tuned.best_params_
param_grid
train_data = data[:1460]
test_data = data[1460:].drop(['SalePrice'], 1)
 x = train_data.drop(['SalePrice'], 1)
 y = np.log1p(train_data['SalePrice'])
<import_modules>
clf = ensemble.GradientBoostingClassifier()
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 clf.fit(train[X], train[y])
 results['test_score'].mean() *100
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor, BaggingRegressor
from sklearn.linear_model import Ridge, RidgeCV, ElasticNet, ElasticNetCV
from sklearn.kernel_ridge import KernelRidge
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.svm import SVR
from mlxtend.regressor import StackingCVRegressor
from sklearn import model_selection
from sklearn.model_selection import cross_val_score, KFold, RandomizedSearchCV, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from scipy.stats import uniform as sp_randFloat
from scipy.stats import randint as sp_randInt<choose_model_class>
test['Survived'] = clf.predict(test[X] )
<train_on_grid><EOS> 
submit = pd.DataFrame({ 'PassengerId' : col1, 'Survived': test['Survived'] } ).set_index('PassengerId')
 submit['Survived'] = submit['Survived'].astype('int')
 submit.to_csv('submission.csv' )