Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<feature_engineering>
%matplotlib inline
warnings.filterwarnings('ignore' )
X = X / 255.0
test_x = test_x / 255.0<categorify>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 combine = [df_train, df_test]
df_train.name = 'Train'
df_test.name = 'Test'
y = to_categorical(y)
 print(f"Label size {y.shape}" )<split>
kfolds = KFold(n_splits=10, shuffle=True, random_state=14)
 target = np.log1p(df_train['SalePrice'])
 old_target = df_train['SalePrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0 )<normalization>
def get_dollars(estimator, kfolds, data, target, old_target):
scores = []
train = data.copy()
for i,(train_index, test_index)in enumerate(kfolds.split(target)) :
training = train.iloc[train_index,:]
valid = train.iloc[test_index,:]
tr_label = target.iloc[train_index]
val_label = target.iloc[test_index]
estimator.fit(training, tr_label)
 pred = estimator.predict(valid)
 or_result = old_target.iloc[test_index]
score = mean_absolute_error(y_pred=np.expm1(pred), y_true=or_result)
 scores.append(score)
 return round(np.mean(scores),3)
 def get_coef(clsf, ftrs):
imp = clsf.steps[1][1].coef_.tolist()
feats = ftrs
result = pd.DataFrame({'feat':feats,'score':imp})
 result = result.sort_values(by=['score'],ascending=False)
 return result
mean = np.mean(X_train)
 std = np.std(X_train)
 def standardize(x):
return(x-mean)/std<define_variables>
scores = []
or_scores = []
for i,(train_index, test_index)in enumerate(kfolds.split(target)) :
df_train['prediction'] = np.nan
print("Fold {} in progress".format(i))
base = target.iloc[train_index].median()
result = target.iloc[test_index]
or_result = old_target.iloc[test_index]
df_train['prediction'].iloc[test_index] = base
prediction = [p for p in df_train['prediction'].dropna() ]
print("Predicting with median {}".format(round(base,3)))
 score = mean_squared_error(y_pred= prediction, y_true=result)
 or_score = mean_absolute_error(y_pred=np.expm1(prediction), y_true=or_result)
 print("Scoring {}".format(score))
print("MAE {}$".format(or_score))
scores.append(score)
 or_scores.append(or_score)
 df_train.drop('prediction', axis=1, inplace=True)
 print("_"*40)
 print("Baseline: {} +- {}".format(round(np.mean(scores),3), round(np.std(scores),3)))
 print('MAE: {} +- {}'.format(round(np.mean(or_scores),3), round(np.std(or_scores),3)) )
epochs = 50
batch_size = 64<choose_model_class>
modelname = ['Baseline']
lassoscore = [0.16]
ridgescore = [0.16]
kaggle_lasso = [0.41899]
kaggle_ridge = [0.41899]
timeelapsed = [600]
maecv_lasso = [55646.558]
maecv_ridge = [55646.558]
model=Sequential()
model.add(Conv2D(filters=64, kernel_size =(3,3), activation="relu", input_shape=(28,28,1)))
 model.add(Conv2D(filters=64, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=256, kernel_size =(3,3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(BatchNormalization())
 model.add(Dense(512,activation="relu"))
model.add(Dense(10,activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] )<init_hyperparams>
for df in combine:
if df.name == 'Train':
mis_train = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_train.append(col)
 print("_"*40)
 print("_"*40)
 if df.name == 'Test':
mis_test = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_test.append(col)
 print("
")
 print(mis_train)
 print("_"*40)
 print(mis_test )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 train_gen = datagen.flow(X_train, y_train, batch_size=batch_size)
 test_gen = datagen.flow(X_test, y_test, batch_size=batch_size )<train_model>
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 butch_train = butch_train[[col for col in butch_test.columns]] 
(butch_train.columns != butch_test.columns ).sum()
history = model.fit_generator(train_gen,
epochs = epochs,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data = test_gen,
validation_steps = X_test.shape[0] // batch_size )<predict_on_test>
scl =('scl', RobustScaler() )
y_pred = model.predict(X_test)
 X_test__ = X_test.reshape(X_test.shape[0], 28, 28)
 fig, axis = plt.subplots(4, 4, figsize=(12, 14))
for i, ax in enumerate(axis.flat):
ax.imshow(X_test__[i], cmap='binary')
 ax.set(title = f"Real Number is {y_test[i].argmax() }
Predict Number is {y_pred[i].argmax() }");<predict_on_test>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print("_"*40)
 print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
pred = model.predict_classes(test_x, verbose=1 )<save_to_csv>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
sub['Label'] = pred
sub.to_csv("CNN_keras_sub.csv", index=False)
 sub.head()<import_modules>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.0075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels<import_modules>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop, Adam
from keras.preprocessing.image import ImageDataGenerator<load_from_csv>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
df = pd.read_csv('.. /input/train.csv')
<load_from_csv>
target = np.log1p(df_train[df_train.GrLivArea < 4500]['SalePrice'])
 old_target = df_train[df_train.GrLivArea < 4500]['SalePrice']
test_df = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
for df in combine:
df.loc[df.LotFrontage.isnull() , 'LotFrontage'] = 0
df.loc[df.Alley.isnull() , 'Alley'] = "NoAlley"
df['MSSubClass'] = df['MSSubClass'].astype(str)
 fil =(( df.BsmtQual.isnull())&(df.BsmtCond.isnull())&(df.BsmtExposure.isnull())& 
(df.BsmtFinType1.isnull())&(df.BsmtFinType2.isnull()))
fil1 =(( df.BsmtQual.notnull())|(df.BsmtCond.notnull())|(df.BsmtExposure.notnull())| 
(df.BsmtFinType1.notnull())|(df.BsmtFinType2.notnull()))
df.loc[fil1, 'MisBsm'] = 0
df.loc[fil, 'MisBsm'] = 1
df.loc[fil, 'BsmtQual'] = "NoBsmt"
df.loc[fil, 'BsmtCond'] = "NoBsmt"
df.loc[fil, 'BsmtExposure'] = "NoBsmt"
df.loc[fil, 'BsmtFinType1'] = "NoBsmt"
df.loc[fil, 'BsmtFinType2'] = "NoBsmt"
df.loc[(df.Fireplaces == 0)&(df.FireplaceQu.isnull()), 'FireplaceQu'] = "NoFire"
fil =(( df.GarageYrBlt.isnull())&(df.GarageType.isnull())&(df.GarageFinish.isnull())& 
(df.GarageQual.isnull())&(df.GarageCond.isnull()))
fil1 =(( df.GarageYrBlt.notnull())|(df.GarageType.notnull())|(df.GarageFinish.notnull())| 
(df.GarageQual.notnull())|(df.GarageCond.notnull()))
df.loc[fil1, 'MisGarage'] = 0
df.loc[fil, 'MisGarage'] = 1
df.loc[df.GarageYrBlt > 2200, 'GarageYrBlt'] = 2007
df.loc[fil, 'GarageYrBlt'] = 0
df.loc[fil, 'GarageType'] = "NoGrg"
df.loc[fil, 'GarageFinish'] = "NoGrg"
df.loc[fil, 'GarageQual'] = "NoGrg"
df.loc[fil, 'GarageCond'] = "NoGrg"
df.loc[df.Fence.isnull() , 'Fence'] = "NoFence"
df_test[['BsmtUnfSF',
'TotalBsmtSF',
'BsmtFinSF1',
'BsmtFinSF2']] = df_test[['BsmtUnfSF',
'TotalBsmtSF',
'BsmtFinSF1',
'BsmtFinSF2']].fillna(0 )
Y = df['label']
X = df.drop(['label'], axis = 1 )<categorify>
for df in combine:
if df.name == 'Train':
mis_train = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_train.append(col)
 print("_"*40)
 print("_"*40)
 if df.name == 'Test':
mis_test = []
cols = df.columns
for col in cols:
mis = df[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df.shape[0] * 100, 3)))
 mis_test.append(col)
 print("
")
 print(mis_train)
 print("_"*40)
 print(mis_test )
X = X / 255.0
X = X.values.reshape(-1,28,28,1)
 Y = np.array(Y)
 Y = to_categorical(Y, num_classes = 10 )<split>
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
df_train = df_train[df_train.GrLivArea < 4500] 
(butch_train.columns != butch_test.columns ).sum()
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, stratify = Y, random_state = 31, test_size = 0.2 )<choose_model_class>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
model = Sequential()<choose_model_class>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
model.add(Conv2D(32,(5,5),padding = 'Same',activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size =(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation = 'softmax')) 
<create_dataframe>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.0075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
datagen = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
brightness_range=None,
shear_range=0.1,
zoom_range=0.15,
channel_shift_range=0.0,
fill_mode='nearest',
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None, validation_split=0.0, dtype=None )<train_model>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
datagen.fit(X_train )<choose_model_class>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
model.compile(optimizer = "Nadam", loss = "categorical_crossentropy", metrics = ["accuracy"] )<define_variables>
print(df_train.shape, df_test.shape)
 df_train.drop("PoolQC", axis=1, inplace=True)
 df_train.drop("MiscFeature", axis=1, inplace=True)
 df_test.drop("PoolQC", axis=1, inplace=True)
 df_test.drop("MiscFeature", axis=1, inplace=True)
 print(df_train.shape, df_test.shape )
max_epochs = 30
batch_size = 256<train_model>
print("Size of train set before my butchering: {}".format(df_train.shape))
for f in df_train.columns:
df_train = df_train[pd.notnull(df_train[f])]
cols = df_train.columns
print("Start printing the missing values...")
 mis_train = []
for col in cols:
mis = df_train[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df_train.shape[0] * 100, 3)))
 mis_train.append(col)
 print("...done printing the missing values")
 print(mis_train)
 print("Size of train set after my butchering: {}".format(df_train.shape))
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size = batch_size),
epochs = max_epochs, verbose = 1, validation_data =(X_test, Y_test),
steps_per_epoch=X_train.shape[0] // batch_size )<prepare_x_and_y>
def find_segment(df, feat):
mis = df[feat].isnull().sum()
cols = df.columns
seg = []
for col in cols:
vc = df[df[feat].isnull() ][col].value_counts(dropna=False ).iloc[0]
if(vc == mis):
seg.append(col)
 return seg
def find_mode(df, feat):
md = df[df[feat].isnull() ][find_segment(df, feat)].dropna(axis=1 ).mode()
md = pd.merge(df, md, how='inner')[feat].mode().iloc[0]
return md
def find_median(df, feat):
md = df[df[feat].isnull() ][find_segment(df, feat)].dropna(axis=1 ).mode()
md = pd.merge(df, md, how='inner')[feat].median()
return md
def similar_mode(df, col, feats):
sm = df[df[col].isnull() ][feats]
md = pd.merge(df, sm, how='inner')[col].mode().iloc[0]
return md
def similar_median(df, col, feats):
sm = df[df[col].isnull() ][feats]
md = pd.merge(df, sm, how='inner')[col].median()
return md
test_df = test_df / 255.0
test_arr = test_df.values.reshape(-1,28,28,1 )<predict_on_test>
md = find_mode(df_test, 'MSZoning')
 print("MSZoning {}".format(md))
df_test[['MSZoning']] = df_test[['MSZoning']].fillna(md)
 md = 'AllPub'
df_test[['Utilities']] = df_test[['Utilities']].fillna(md)
 md = find_mode(df_test, 'MasVnrType')
 print("MasVnrType {}".format(md))
df_test[['MasVnrType']] = df_test[['MasVnrType']].fillna(md)
 md = find_mode(df_test, 'MasVnrArea')
 print("MasVnrArea {}".format(md))
df_test[['MasVnrArea']] = df_test[['MasVnrArea']].fillna(md)
 simi = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtQual', simi)
 print("BsmtQual {}".format(md))
df_test[['BsmtQual']] = df_test[['BsmtQual']].fillna(md)
 simi = ['BsmtQual', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtCond', simi)
 print("BsmtCond {}".format(md))
df_test[['BsmtCond']] = df_test[['BsmtCond']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_mode(df_test, 'BsmtExposure', simi)
 print("BsmtExposure {}".format(md))
df_test[['BsmtExposure']] = df_test[['BsmtExposure']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_median(df_test, 'BsmtFullBath', simi)
 print("BsmtFullBath {}".format(md))
df_test[['BsmtFullBath']] = df_test[['BsmtFullBath']].fillna(md)
 simi = ['BsmtQual', 'BsmtCond', 'BsmtFinType1', 'BsmtFinType2']
md = similar_median(df_test, 'BsmtHalfBath', simi)
 print("BsmtHalfBath {}".format(md))
df_test[['BsmtHalfBath']] = df_test[['BsmtHalfBath']].fillna(md)
 md = df_test.KitchenQual.mode().iloc[0]
print("KitchenQual {}".format(md))
df_test[['KitchenQual']] = df_test[['KitchenQual']].fillna(md)
 md = 'Typ'
df_test[['Functional']] = df_test[['Functional']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageYrBlt', simi)
 print("GarageYrBlt {}".format(md))
df_test[['GarageYrBlt']] = df_test[['GarageYrBlt']].fillna(md)
 md = 'Unf'
print("GarageFinish {}".format(md))
df_test[['GarageFinish']] = df_test[['GarageFinish']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageArea', simi)
 print("GarageArea {}".format(md))
df_test[['GarageArea']] = df_test[['GarageArea']].fillna(md)
 simi = ['GarageType', 'MisGarage', 'GarageFinish']
md = similar_mode(df_test, 'GarageQual', simi)
 print("GarageQual {}".format(md))
df_test[['GarageQual']] = df_test[['GarageQual']].fillna(md)
 simi = ['GarageType', 'MisGarage', 'GarageFinish']
md = similar_mode(df_test, 'GarageCond', simi)
 print("GarageCond {}".format(md))
df_test[['GarageCond']] = df_test[['GarageCond']].fillna(md)
 simi = ['GarageType', 'MisGarage']
md = similar_median(df_test, 'GarageCars', simi)
 print("GarageCars {}".format(md))
df_test[['GarageCars']] = df_test[['GarageCars']].fillna(md)
 cols = df_test.columns
mis_test = []
print("Start printing the missing values...")
 for col in cols:
mis = df_test[col].isnull().sum()
if mis > 0:
print("{}: {} missing, {}%".format(col, mis, round(mis/df_test.shape[0] * 100, 3)))
 mis_test.append(col)
 print("...done printing the missing values" )
Y_pred_arr = model.predict(test_arr )<prepare_output>
butch_train = df_train[[col for col in df_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = df_test[[col for col in df_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(list(set(butch_train.columns)- set(butch_test.columns)))
 butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train[df_train.GrLivArea < 4500]['SalePrice'])
 old_target = df_train[df_train.GrLivArea < 4500]['SalePrice'] 
(butch_train.columns != butch_test.columns ).sum()
Y_pred_arr = np.argmax(Y_pred_arr, axis = 1 )<save_to_csv>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
results = pd.Series(Y_pred_arr,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<load_from_csv>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose=0)
 styles=[':','-.','--','-',':','-.','--','-',':','-.','--','-']<choose_model_class>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
nets = 3
model = [0] *nets
for j in range(3):
model[j] = Sequential()
model[j].add(Conv2D(24,kernel_size=5,padding='same',activation='relu',
input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 if j>0:
model[j].add(Conv2D(48,kernel_size=5,padding='same',activation='relu'))
model[j].add(MaxPool2D())
 if j>1:
model[j].add(Conv2D(64,kernel_size=5,padding='same',activation='relu'))
model[j].add(MaxPool2D(padding='same'))
model[j].add(Flatten())
 model[j].add(Dense(256, activation='relu'))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.333)
 history = [0] * nets
names = ["(C-P)x1","(C-P)x2","(C-P)x3"]
epochs = 20
for j in range(nets):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=80, epochs = epochs,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])) )<choose_model_class>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
nets = 6
model = [0] *nets
for j in range(6):
model[j] = Sequential()
model[j].add(Conv2D(j*8+8,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 model[j].add(Conv2D(j*16+16,kernel_size=5,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Flatten())
 model[j].add(Dense(256, activation='relu'))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
prediction = np.expm1(best_ridge.predict(butch_test))
sub = pd.DataFrame()
sub['Id'] = df_test['Id']
sub['SalePrice'] = prediction
sub.to_csv('best_ridge_full_imputation.csv',index=False )
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.333)
 history = [0] * nets
names = ["8 maps","16 maps","24 maps","32 maps","48 maps","64 maps"]
epochs = 20
for j in range(nets):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=80, epochs = epochs,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])) )<choose_model_class>
df_train.drop("Exterior1st", axis=1, inplace=True)
 df_test.drop("Exterior1st", axis=1, inplace=True)
 df_train.drop("Exterior2nd", axis=1, inplace=True)
 df_test.drop("Exterior2nd", axis=1, inplace=True)
 df_train.drop("SaleType", axis=1, inplace=True)
 df_test.drop("SaleType", axis=1, inplace=True )
nets = 8
model = [0] *nets
for j in range(8):
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 model[j].add(Conv2D(64,kernel_size=5,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Flatten())
 if j>0:
model[j].add(Dense(2**(j+4), activation='relu'))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
def tr_ExtQual(df):
df.loc[df.ExterQual == 'Fa', 'ExterQual'] = 0
df.loc[df.ExterQual == 'TA', 'ExterQual'] = 1
df.loc[df.ExterQual == 'Gd', 'ExterQual'] = 2
df.loc[df.ExterQual == 'Ex', 'ExterQual'] = 3
df.ExterQual = pd.to_numeric(df.ExterQual)
 return df
def tr_ExtQual_plus(df):
df.loc[df.ExterQual == 'Fa', 'ExterQual'] = 1
df.loc[df.ExterQual == 'TA', 'ExterQual'] = 1
df.loc[df.ExterQual == 'Gd', 'ExterQual'] = 2
df.loc[df.ExterQual == 'Ex', 'ExterQual'] = 3
df.ExterQual = pd.to_numeric(df.ExterQual)
 return df
def tr_ExtCond(df):
df.loc[df.ExterCond == 'Po', 'ExterCond'] = 0
df.loc[df.ExterCond == 'Fa', 'ExterCond'] = 1
df.loc[df.ExterCond == 'TA', 'ExterCond'] = 2
df.loc[df.ExterCond == 'Gd', 'ExterCond'] = 3
df.loc[df.ExterCond == 'Ex', 'ExterCond'] = 4
df.ExterCond = pd.to_numeric(df.ExterCond)
 return df
def tr_ExtCond_plus(df):
df.loc[df.ExterCond == 'Po', 'ExterCond'] = 1
df.loc[df.ExterCond == 'Fa', 'ExterCond'] = 1
df.loc[df.ExterCond == 'TA', 'ExterCond'] = 1
df.loc[df.ExterCond == 'Gd', 'ExterCond'] = 2
df.loc[df.ExterCond == 'Ex', 'ExterCond'] = 2
df.ExterCond = pd.to_numeric(df.ExterCond)
 return df
def tr_BsmtQu(df):
df.loc[df.BsmtQual == 'NoBsmt', 'BsmtQual'] = 0
df.loc[df.BsmtQual == 'Fa', 'BsmtQual'] = 1
df.loc[df.BsmtQual == 'TA', 'BsmtQual'] = 2
df.loc[df.BsmtQual == 'Gd', 'BsmtQual'] = 3
df.loc[df.BsmtQual == 'Ex', 'BsmtQual'] = 4
df.BsmtQual = pd.to_numeric(df.BsmtQual)
 return df
def tr_BsmtQu_plus(df):
df.loc[df.BsmtQual == 'NoBsmt', 'BsmtQual'] = 0
df.loc[df.BsmtQual == 'Fa', 'BsmtQual'] = 1
df.loc[df.BsmtQual == 'TA', 'BsmtQual'] = 4
df.loc[df.BsmtQual == 'Gd', 'BsmtQual'] = 10
df.loc[df.BsmtQual == 'Ex', 'BsmtQual'] = 21
df.BsmtQual = pd.to_numeric(df.BsmtQual)
 return df
def tr_BsmtCo(df):
df.loc[df.BsmtCond == 'NoBsmt', 'BsmtCond'] = 0
df.loc[df.BsmtCond == 'Po', 'BsmtCond'] = 1
df.loc[df.BsmtCond == 'Fa', 'BsmtCond'] = 2
df.loc[df.BsmtCond == 'TA', 'BsmtCond'] = 3
df.loc[df.BsmtCond == 'Gd', 'BsmtCond'] = 4
df.BsmtCond = pd.to_numeric(df.BsmtCond)
 return df
def tr_BsmtExp(df):
df.loc[df.BsmtExposure == 'NoBsmt', 'BsmtExposure'] = 0
df.loc[df.BsmtExposure == 'No', 'BsmtExposure'] = 1
df.loc[df.BsmtExposure == 'Mn', 'BsmtExposure'] = 2
df.loc[df.BsmtExposure == 'Av', 'BsmtExposure'] = 3
df.loc[df.BsmtExposure == 'Gd', 'BsmtExposure'] = 4
df.BsmtExposure = pd.to_numeric(df.BsmtExposure)
 return df
def tr_BsmtExp_plus(df):
df.loc[df.BsmtExposure == 'NoBsmt', 'BsmtExposure'] = 0
df.loc[df.BsmtExposure == 'No', 'BsmtExposure'] = 6
df.loc[df.BsmtExposure == 'Mn', 'BsmtExposure'] = 7
df.loc[df.BsmtExposure == 'Av', 'BsmtExposure'] = 8
df.loc[df.BsmtExposure == 'Gd', 'BsmtExposure'] = 12
df.BsmtExposure = pd.to_numeric(df.BsmtExposure)
 return df
def tr_HeatQ(df):
df.loc[df.HeatingQC == 'Po', 'HeatingQC'] = 0
df.loc[df.HeatingQC == 'Fa', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'TA', 'HeatingQC'] = 2
df.loc[df.HeatingQC == 'Gd', 'HeatingQC'] = 3
df.loc[df.HeatingQC == 'Ex', 'HeatingQC'] = 4
df.HeatingQC = pd.to_numeric(df.HeatingQC)
 return df
def tr_HeatQ_plus(df):
df.loc[df.HeatingQC == 'Po', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'Fa', 'HeatingQC'] = 1
df.loc[df.HeatingQC == 'TA', 'HeatingQC'] = 3
df.loc[df.HeatingQC == 'Gd', 'HeatingQC'] = 4
df.loc[df.HeatingQC == 'Ex', 'HeatingQC'] = 7
df.HeatingQC = pd.to_numeric(df.HeatingQC)
 return df
def tr_KitcQu(df):
df.loc[df.KitchenQual == 'Fa', 'KitchenQual'] = 1
df.loc[df.KitchenQual == 'TA', 'KitchenQual'] = 2
df.loc[df.KitchenQual == 'Gd', 'KitchenQual'] = 3
df.loc[df.KitchenQual == 'Ex', 'KitchenQual'] = 4
df.KitchenQual = pd.to_numeric(df.KitchenQual)
 return df
def tr_KitcQu_plus(df):
df.loc[df.KitchenQual == 'Fa', 'KitchenQual'] = 1
df.loc[df.KitchenQual == 'TA', 'KitchenQual'] = 4
df.loc[df.KitchenQual == 'Gd', 'KitchenQual'] = 10
df.loc[df.KitchenQual == 'Ex', 'KitchenQual'] = 21
df.KitchenQual = pd.to_numeric(df.KitchenQual)
 return df
def tr_FireQu(df):
df.loc[df.FireplaceQu == 'NoFire', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Po', 'FireplaceQu'] = 1
df.loc[df.FireplaceQu == 'Fa', 'FireplaceQu'] = 2
df.loc[df.FireplaceQu == 'TA', 'FireplaceQu'] = 3
df.loc[df.FireplaceQu == 'Gd', 'FireplaceQu'] = 4
df.loc[df.FireplaceQu == 'Ex', 'FireplaceQu'] = 5
df.FireplaceQu = pd.to_numeric(df.FireplaceQu)
 return df
def tr_FireQu_plus(df):
df.loc[df.FireplaceQu == 'NoFire', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Po', 'FireplaceQu'] = 0
df.loc[df.FireplaceQu == 'Fa', 'FireplaceQu'] = 2
df.loc[df.FireplaceQu == 'TA', 'FireplaceQu'] = 3
df.loc[df.FireplaceQu == 'Gd', 'FireplaceQu'] = 4
df.loc[df.FireplaceQu == 'Ex', 'FireplaceQu'] = 8
df.FireplaceQu = pd.to_numeric(df.FireplaceQu)
 return df
def tr_GarQu(df):
df.loc[df.GarageQual == 'NoGrg', 'GarageQual'] = 0
df.loc[df.GarageQual == 'Po', 'GarageQual'] = 1
df.loc[df.GarageQual == 'Fa', 'GarageQual'] = 2
df.loc[df.GarageQual == 'TA', 'GarageQual'] = 3
df.loc[df.GarageQual == 'Gd', 'GarageQual'] = 4
if df.name == 'Train':
df.loc[df.GarageQual == 'Ex', 'GarageQual'] = 5
df.GarageQual = pd.to_numeric(df.GarageQual)
 return df
def tr_GarCo(df):
df.loc[df.GarageCond == 'NoGrg', 'GarageCond'] = 0
df.loc[df.GarageCond == 'Po', 'GarageCond'] = 1
df.loc[df.GarageCond == 'Fa', 'GarageCond'] = 2
df.loc[df.GarageCond == 'TA', 'GarageCond'] = 3
df.loc[df.GarageCond == 'Gd', 'GarageCond'] = 4
df.loc[df.GarageCond == 'Ex', 'GarageCond'] = 5
df.GarageCond = pd.to_numeric(df.GarageCond)
 return df
def tr_MSZo(df):
df.loc[(df.MSZoning == 'RH')|(df.MSZoning == 'RM'), 'MSZoning'] = 'ResMedHig'
df.loc[(df.MSZoning == 'FV'), 'MSZoning'] = 'Vil'
df.loc[(df.MSZoning == 'RL')|(df.MSZoning == 'C(all)'), 'MSZoning'] = 'ResLowCom'
return df
def tr_Alley(df):
df.loc[(df.Alley == 'Grvl')|(df.Alley == 'Pave'), 'Alley'] = 'Alley'
df.loc[df.Alley == 'NoAlley', 'Alley'] = 'NoAlley'
return df
def tr_LotSh(df):
irr = ['IR1', 'IR2', 'IR3']
df.loc[(df.LotShape.isin(irr)) , 'LotShape'] = 'Irreg'
df.loc[df.LotShape == 'Reg', 'LotShape'] = 'Reg'
return df
def tr_Cond1(df):
ArtFee = ['Artery', 'Feedr']
stat = ['RRAe', 'RRAn', 'RRNe', 'RRNn']
pos = ['PosA', 'PosN']
df.loc[(df.Condition1.isin(ArtFee)) , 'Condition1'] = 'ArtFee'
df.loc[(df.Condition1.isin(stat)) , 'Condition1'] = 'Station'
df.loc[(df.Condition1.isin(pos)) , 'Condition1'] = 'Station'
df.loc[df.Condition1 == 'Norm', 'Condition1'] = 'Norm'
return df
def tr_BldTy(df):
df.loc[(df.BldgType == '2fmCon')|(df.BldgType == 'Duplex'), 'BldgType'] = '2FamDup'
df.loc[(df.BldgType == 'Twnhs')|(df.BldgType == 'TwnhsE'), 'BldgType'] = 'Twnhs+E'
df.loc[(df.BldgType == '1Fam'), 'BldgType'] = '1Fam'
return df
def tr_HSty(df):
onepl = ['1.5Fin', '1.5Unf']
twopl = ['2.5Fin', '2.5Unf', '2Story']
spl = ['SFoyer', 'SLvl']
df.loc[df.HouseStyle.isin(onepl), 'HouseStyle'] = '1.5'
df.loc[df.HouseStyle.isin(twopl), 'HouseStyle'] = '2plus'
df.loc[df.HouseStyle.isin(spl), 'HouseStyle'] = 'Split'
df.loc[df.HouseStyle == '1Story', 'HouseStyle'] = "1Story"
return df
def tr_Found(df):
fancy = ['BrkTil', 'Stone', 'Wood']
cement = ['PConc', 'Slab']
df.loc[df.Foundation.isin(fancy), 'Foundation'] = 'Fancy'
df.loc[df.Foundation.isin(cement), 'Foundation'] = 'Cement'
df.loc[df.Foundation == 'CBlock', 'Foundation'] = 'Cider'
return df
def tr_MasVnrTy(df):
df.loc[df.MasVnrType == 'None', 'MasVnrType'] = 'None'
df.loc[df.MasVnrType == 'Stone', 'MasVnrType'] = 'Stone'
df.loc[(df.MasVnrType == 'BrkCmn')|(df.MasVnrType == 'BrkFace'), 'MasVnrType'] = 'Bricks'
return df
def tr_Elec(df):
df.loc[df.Electrical == "SBrkr", "Electrical"] = "SBrkr"
fuse = ['FuseA', 'FuseF', 'FuseP', 'Mix']
df.loc[df.Electrical.isin(fuse), "Electrical"] = "Fuse"
return df
def tr_GrgTy(df):
incl = ['Attchd', 'Basment', 'BuiltIn']
escl = ['2Types', 'CarPort', 'Detchd']
df.loc[df.GarageType.isin(incl), 'GarageType'] = 'Connected'
df.loc[df.GarageType.isin(escl), 'GarageType'] = 'NonConnected'
df.loc[df.GarageType == 'NoGrg', 'GarageType'] = 'NoGrg'
return df
def tr_PvdDr(df):
df.loc[df.PavedDrive == 'N', 'PavedDrive'] = 'N'
df.loc[df.PavedDrive == 'P', 'PavedDrive'] = 'N'
df.loc[df.PavedDrive == 'Y', 'PavedDrive'] = 'Y'
return df
def tr_Fence(df):
df.loc[df.Fence == 'NoFence', 'Fence'] = 'NoFence'
df.loc[(df.Fence == 'MnPrv')|(df.Fence == 'GdPrv'), 'Fence'] = 'Prv'
df.loc[(df.Fence == 'MnWw')|(df.Fence == 'GdWo'), 'Fence'] = 'Wo'
return df
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.333)
 history = [0] * nets
names = ["0N","32N","64N","128N","256N","512N","1024N","2048N"]
epochs = 20
for j in range(nets):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=80, epochs = epochs,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])) )<choose_model_class>
tr_train = df_train.copy()
tr_test = df_test.copy()
tr_train.name = 'Train'
tr_test.name = 'Test'
tr_train = tr_GarQu(tr_train)
 tr_test = tr_GarQu(tr_test)
 tr_train = tr_FireQu(tr_train)
 tr_test = tr_FireQu(tr_test)
 tr_train = tr_HeatQ_plus(tr_train)
 tr_test = tr_HeatQ_plus(tr_test)
 tr_train = tr_PvdDr(tr_train)
 tr_test = tr_PvdDr(tr_test)
 tr_train = tr_GrgTy(tr_train)
 tr_test = tr_GrgTy(tr_test)
 tr_train = tr_ExtQual_plus(tr_train)
 tr_test = tr_ExtQual_plus(tr_test)
 tr_train = tr_MasVnrTy(tr_train)
 tr_test = tr_MasVnrTy(tr_test)
 tr_train = tr_GarCo(tr_train)
 tr_test = tr_GarCo(tr_test)
 tr_train = tr_BsmtCo(tr_train)
 tr_test = tr_BsmtCo(tr_test)
 tr_train = tr_Elec(tr_train)
 tr_test = tr_Elec(tr_test)
 tr_train = tr_BsmtQu_plus(tr_train)
 tr_test = tr_BsmtQu_plus(tr_test)
 tr_train = tr_LotSh(tr_train)
 tr_test = tr_LotSh(tr_test)
 tr_train = tr_Alley(tr_train)
 tr_test = tr_Alley(tr_test )
nets = 8
model = [0] *nets
for j in range(8):
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 model[j].add(Dropout(j*0.1))
model[j].add(Conv2D(64,kernel_size=5,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Dropout(j*0.1))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(j*0.1))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
butch_train = tr_train[[col for col in tr_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = tr_test[[col for col in tr_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(set(df_train.columns)- set(df_test.columns))
butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
(butch_train.columns != butch_test.columns ).sum()
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.333)
 history = [0] * nets
names = ["D=0","D=0.1","D=0.2","D=0.3","D=0.4","D=0.5","D=0.6","D=0.7"]
epochs = 30
for j in range(nets):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=80, epochs = epochs,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])) )<choose_model_class>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
nets = 5
model = [0] *nets
j=0
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64,kernel_size=5,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Dropout(0.4))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 j=1
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model[j].add(Conv2D(32,kernel_size=3,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Dropout(0.4))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 j=2
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Conv2D(64,kernel_size=5,activation='relu'))
model[j].add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 j=3
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
j=4
model[j] = Sequential()
model[j].add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64,kernel_size=3,activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.2)
 history = [0] * nets
names = ["basic","32C3-32C3","32C5S2","both+BN","both+BN+DA"]
epochs = 35
for j in range(nets-1):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=64, epochs = epochs,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])))
 datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 j = nets-1
history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])) )<save_to_csv>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 **(x+epochs))
model[4].fit_generator(datagen.flow(X_train,Y_train, batch_size=64), epochs = 25,
steps_per_epoch = X_train.shape[0]//64, callbacks=[annealer], verbose=0)
 results = model[4].predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("MNIST-CNN.csv",index=False )<load_from_csv>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
candidates = ['Condition1', 'Condition2', 'YearRemodAdd', "RoofStyle",
'RoofMatl','BsmtFinSF1', 'BsmtFinSF2', 'Heating', 'Electrical',
'1stFlrSF', '2ndFlrSF', 'LowQualFinSF','Functional', 'TotRmsAbvGrd',
'GarageCars', 'YrSold', 'MoSold']
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<count_missing_values>
%%time
winners = []
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 for feat in candidates:
sel_train = tr_train.drop(feat, axis=1)
 sel_test = tr_test.drop(feat, axis=1)
 butch_train = sel_train[[col for col in sel_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = sel_test[[col for col in sel_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
 grid.fit(butch_train, target)
 score = np.sqrt(-grid.best_score_)
 if score <= 0.11065:
print(feat, score-0.11063297306583295)
 print(score)
 winners.append(feat)
 print("_"*40)
 print(winners )
train_data.isnull().sum()<count_missing_values>
sel_train = tr_train.drop('LowQualFinSF', axis=1)
 sel_test = tr_test.drop('LowQualFinSF', axis=1)
 sel_train.drop('TotRmsAbvGrd', axis=1, inplace=True)
 sel_test.drop('TotRmsAbvGrd', axis=1, inplace=True)
 sel_train.drop('RoofStyle', axis=1, inplace=True)
 sel_test.drop('RoofStyle', axis=1, inplace=True)
 sel_train.drop('Heating', axis=1, inplace=True)
 sel_test.drop('Heating', axis=1, inplace=True)
 sel_train.drop('MoSold', axis=1, inplace=True)
 sel_test.drop('MoSold', axis=1, inplace=True)
 sel_train.drop('Electrical', axis=1, inplace=True)
 sel_test.drop('Electrical', axis=1, inplace=True)
 sel_train.drop('YrSold', axis=1, inplace=True)
 sel_test.drop('YrSold', axis=1, inplace=True)
 sel_train.drop('RoofMatl', axis=1, inplace=True)
 sel_test.drop('RoofMatl', axis=1, inplace=True)
 sel_train.drop('BsmtFinSF1', axis=1, inplace=True)
 sel_test.drop('BsmtFinSF1', axis=1, inplace=True)
 sel_train.drop('2ndFlrSF', axis=1, inplace=True)
 sel_test.drop('2ndFlrSF', axis=1, inplace=True )
test_data.isnull().sum()<sort_values>
sel_train.drop('BsmtFinSF2', axis=1, inplace=True)
 sel_test.drop('BsmtFinSF2', axis=1, inplace=True)
 sel_train.drop('LandSlope', axis=1, inplace=True)
 sel_test.drop('LandSlope', axis=1, inplace=True )
train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<filter>
butch_train = sel_train[[col for col in sel_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = sel_test[[col for col in sel_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(set(df_train.columns)- set(df_test.columns))
butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis=1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
(butch_train.columns != butch_test.columns ).sum()
train_data[train_data.Embarked.isnull() ]<data_type_conversions>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
train_data = train_data.fillna({"Embarked": "S"} )<drop_column>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
train_data = train_data.drop(['Ticket', 'Cabin'], axis=1)
 test_data = test_data.drop(['Ticket', 'Cabin'], axis=1 )<feature_engineering>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_data.head()<categorify>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
mr_age = train_data[train_data["Title"] == 1]["AgeGroup"].mode()
miss_age = train_data[train_data["Title"] == 2]["AgeGroup"].mode()
mrs_age = train_data[train_data["Title"] == 3]["AgeGroup"].mode()
master_age = train_data[train_data["Title"] == 4]["AgeGroup"].mode()
royal_age = train_data[train_data["Title"] == 5]["AgeGroup"].mode()
rare_age = train_data[train_data["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train_data["AgeGroup"])) :
if train_data["AgeGroup"][x] == "Unknown":
train_data["AgeGroup"][x] = age_title_mapping[train_data["Title"][x]]
for x in range(len(test_data["AgeGroup"])) :
if test_data["AgeGroup"][x] == "Unknown":
test_data["AgeGroup"][x] = age_title_mapping[test_data["Title"][x]]<categorify>
modelname.append('Feat_Sel_EDA')
 lassoscore.append(0.1099)
 ridgescore.append(0.1107)
 kaggle_lasso.append(0.11953)
 kaggle_ridge.append(0.11861)
 timeelapsed.append(18000)
 maecv_lasso.append(13161.658)
 maecv_ridge.append(13273.673 )
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train_data['AgeGroup'] = train_data['AgeGroup'].map(age_mapping)
 test_data['AgeGroup'] = test_data['AgeGroup'].map(age_mapping)
 train_data.head()
train_data = train_data.drop(['Age'], axis = 1)
 test_data = test_data.drop(['Age'], axis = 1 )<drop_column>
eng_train = sel_train.copy()
eng_test = sel_test.copy()
eng_test['LotDepth'] = eng_test['LotFrontage'] / eng_test['LotArea']
eng_train['LotDepth'] = eng_train['LotFrontage'] / eng_train['LotArea']
train_data = train_data.drop(['Name'], axis = 1)
 test_data = test_data.drop(['Name'], axis = 1 )<categorify>
eng_train['TotBath'] = eng_train.FullBath + eng_train.HalfBath
eng_test['TotBath'] = eng_test.FullBath + eng_test.HalfBath
sex_mapping = {"male": 0, "female": 1}
train_data['Sex'] = train_data['Sex'].map(sex_mapping)
 test_data['Sex'] = test_data['Sex'].map(sex_mapping)
 train_data.head()<categorify>
eng_train['TotPorch'] =(eng_train['WoodDeckSF'] + eng_train['OpenPorchSF'] + eng_train['EnclosedPorch'] +
eng_train['3SsnPorch'] + eng_train['ScreenPorch'])
 eng_test['TotPorch'] =(eng_test['WoodDeckSF'] + eng_test['OpenPorchSF'] + eng_test['EnclosedPorch'] +
eng_test['3SsnPorch'] + eng_test['ScreenPorch'] )
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train_data['Embarked'] = train_data['Embarked'].map(embarked_mapping)
 test_data['Embarked'] = test_data['Embarked'].map(embarked_mapping)
 train_data.head()<drop_column>
butch_train = eng_train[[col for col in eng_test.columns if col not in mis_test]].dropna(axis=1)
 butch_test = eng_test[[col for col in eng_test.columns if col not in mis_train]].dropna(axis=1)
 butch_train.drop("Id", axis=1, inplace=True)
 butch_test.drop("Id", axis=1, inplace=True)
 butch_train = pd.get_dummies(butch_train)
 butch_test = pd.get_dummies(butch_test)
 print(set(df_train.columns)- set(df_test.columns))
butch_train = butch_train[[col for col in butch_test.columns if col in butch_train.columns]]
butch_test = butch_test[[col for col in butch_train.columns]]
butch_train = butch_train.drop([col for col in butch_train.columns if 'NoGrg' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoGrg' in col], axis = 1)
 butch_train = butch_train.drop([col for col in butch_train.columns if 'NoBsmt' in col], axis=1)
 butch_test = butch_test.drop([col for col in butch_test.columns if 'NoBsmt' in col], axis=1)
 butch_train = butch_train[butch_train.GrLivArea < 4500]
target = np.log1p(df_train['SalePrice'])
(butch_train.columns != butch_test.columns ).sum()
train_data = train_data.drop(['Fare'], axis = 1)
 test_data= test_data.drop(['Fare'], axis = 1 )<split>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
X = train_data.drop(['Survived', 'PassengerId'], axis=1)
 Y = train_data["Survived"]
x_train, x_val, y_train, y_val = train_test_split(X,Y, test_size = 0.22, random_state = 0 )<train_model>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
G = GaussianNB()
G.fit(x_train, y_train)
 y_pred = G.predict(x_val)
 Accuracy_for_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_gaussian: ",Accuracy_for_gaussian)
 L = LogisticRegression()
L.fit(x_train, y_train)
 y_pred = L.predict(x_val)
 Accuracy_for_logistic_regression = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_logistic_regression: ",Accuracy_for_logistic_regression)
 R = RandomForestClassifier()
R.fit(x_train, y_train)
 y_pred = R.predict(x_val)
 Accuracy_for_Random_forest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_Random_forest: ",Accuracy_for_Random_forest)
 SVC = LinearSVC()
SVC.fit(x_train, y_train)
 y_pred = SVC.predict(x_val)
 Accuracy_for_LinearSVC = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_LinearSVC: ",Accuracy_for_LinearSVC)
 clf = svm.SVC()
clf.fit(x_train, y_train)
 y_pred = clf.predict(x_val)
 Accuracy_for_SVM = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_SVM: ",Accuracy_for_SVM)
 MNB = MultinomialNB()
MNB.fit(x_train, y_train)
 y_pred = MNB.predict(x_val)
 Accuracy_for_MultinomialNB = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_MultinomialNB: ",Accuracy_for_MultinomialNB)
 DTC = DecisionTreeClassifier()
DTC.fit(x_train, y_train)
 y_pred = DTC.predict(x_val)
 Accuracy_for_DecisionTreeClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_DecisionTreeClassifier: ",Accuracy_for_DecisionTreeClassifier)
 GBC = GradientBoostingClassifier()
GBC.fit(x_train, y_train)
 y_pred = GBC.predict(x_val)
 Accuracy_for_GradientBoostingClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_GradientBoostingClassifier: ",Accuracy_for_GradientBoostingClassifier)
 SGD = SGDClassifier(loss="log", penalty="l2", max_iter=5)
 SGD.fit(x_train, y_train)
 y_pred = SGD.predict(x_val)
 Accuracy_for_SGDClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_SGDClassifier: ",Accuracy_for_SGDClassifier)
 KN = KNeighborsClassifier()
KN.fit(x_train, y_train)
 y_pred = KN.predict(x_val)
 Accuracy_for_KNeighborsClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_KNeighborsClassifier: ",Accuracy_for_KNeighborsClassifier )<save_to_csv>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : [0.0001, 0.0005, 0.00075,
0.001, 0.005, 0.0075,
0.01, 0.05, 0.075,
0.1, 0.5, 0.75,
1, 5, 7.5,
10, 15, 17.5]}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
print(pd.DataFrame(grid.cv_results_)[['params','mean_test_score','std_test_score']] )
ID = test_data['PassengerId']
Predictions = GBC.predict(test_data.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ID, 'Survived': Predictions })
 output.to_csv('submission.csv', index=False )<install_modules>
dol = get_dollars(best_ridge, kfolds, butch_train, target, old_target)
 dol
!pip install -U cufflinks
!pip install lemminflect
!pip install twitter-text-python
!pip install folium<set_options>
coefs = get_coef(best_ridge, butch_train.columns)
 coefs.head(10 )
init_notebook_mode(connected=False)
 cf.go_offline()
pd.options.display.max_rows = 200
pd.options.display.max_columns = 200
pd.options.display.max_colwidth = 200
plt.style.use('ggplot' )<load_from_csv>
learning_print(best_lasso, butch_train, target, kfolds )
DATA_DIR = '/kaggle/input/nlp-getting-started/'
train = pd.read_csv(pjoin(DATA_DIR, 'train.csv'))
test = pd.read_csv(pjoin(DATA_DIR, 'test.csv'))
train['is_train'] = True
test['is_train'] = False
df = pd.concat(
 [train, test],
sort=False, ignore_index=True
).set_index('id' ).sort_index()
print(train.shape, test.shape, df.shape)
 df.head()<count_values>
learning_print(best_ridge, butch_train, target, kfolds )
df.loc[df.is_train, 'target'].value_counts(normalize=True )<categorify>
pipe = Pipeline([scl,('lasso', Lasso(max_iter=2000)) ])
 param_grid = [{'lasso__alpha' : np.arange(0.0001,0.001, 0.00001)}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_lasso = grid.best_estimator_
print(best_lasso)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
def lemmatize(word, upos='VERB'):
try:
lemma = getLemma(word, upos=upos, lemmatize_oov=True)
 except AssertionError:
lemma = getLemma(word, upos='VERB', lemmatize_oov=True)
 if not lemma:
lemma = getLemma(word, upos='VERB', lemmatize_oov=True)
 return lemma[0].lower()
df.keyword = df.keyword.fillna('NaN' ).astype(str ).str.replace('(%20)+', '_')
 print(df.keyword.nunique())
 top_n = 10
df.groupby('keyword' ).agg(
 tweet_cnt=pd.NamedAgg(column='target', aggfunc='count'),
mean_target=pd.NamedAgg(column='target', aggfunc='mean'),
).sort_values(by=['tweet_cnt', 'mean_target'], ascending=[False, False] ).head(top_n )<remove_duplicates>
dol = get_dollars(best_lasso, kfolds, butch_train, target, old_target)
 dol
lemmatized = dict(
 zip(
 df.keyword.drop_duplicates() ,
df.keyword.drop_duplicates().str.split('[_ ]+')
.apply(
 lambda words: '_'.join(
 lemmatize(w, upos='VERB')for w in sorted(words)) 
)
)
)
 print(
 len(set(lemmatized.keys())) ,
len(set(lemmatized.values())) ,
1 - len(set(lemmatized.values())) / len(set(lemmatized.keys()))
)<create_dataframe>
pipe = Pipeline([scl,('ridge', Ridge())])
 param_grid = [{'ridge__alpha' : np.arange(5,15,0.5)}]
grid = GridSearchCV(pipe, param_grid=param_grid,
cv=kfolds, scoring='neg_mean_squared_error', return_train_score=True, n_jobs=-1)
 %time grid.fit(butch_train, target)
 best_ridge = grid.best_estimator_
print(best_ridge)
 print("_"*40)
 print(np.sqrt(-grid.best_score_))
<groupby><EOS> 
scoresummary = pd.DataFrame({'Model Name' : modelname,
'CVScore Lasso': lassoscore,
'CVScore Ridge': ridgescore,
'KaggleScore Lasso': kaggle_lasso,
'KaggleScore Ridge': kaggle_ridge,
'MAE Lasso': maecv_lasso,
'MAE Ridge': maecv_ridge,
'Cumulative Time': timeelapsed})
 scoresummary[['Model Name', 'CVScore Lasso', 'CVScore Ridge', 'KaggleScore Lasso',
'KaggleScore Ridge', 'MAE Lasso', 'MAE Ridge', 'Cumulative Time']]