Kaggle competition: Digit Recognizer
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<predict_on_test>
import time
import warnings
import numpy as np
import pandas as pd
averaged_models.fit(train.values, y_train)
 av_train_pred = averaged_models.predict(train.values)
 print(rmsle(y_train, av_train_pred))<predict_on_test>
sns.set_style("whitegrid")
 warnings.filterwarnings('ignore' )
av_test_pred = np.expm1(averaged_models.predict(test.values))<load_from_csv>
train=pd.read_csv(".. /input/digit-recognizer/train.csv")
 submit=pd.read_csv(".. /input/digit-recognizer/test.csv")
 print(typeInfo(train))
print(typeInfo(submit))
sample_submission = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv")
 sample_submission<save_to_csv>
x_train = train.drop('label', axis=1)
 y_train = train['label']
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = av_test_pred
sub.to_csv('submission.csv',index=False )<load_from_csv>
x = x_train
y = y_train
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.15, random_state=0 )
X_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 X_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 X_train.dropna(axis=0, subset=['SalePrice'], inplace=True)
 y_train = X_train.SalePrice
X_train.drop(['SalePrice'], axis=1, inplace=True)
 categorical_cols = [cname for cname in X_train.columns if
X_train[cname].nunique() < 20 and
X_train[cname].dtype == "object"]
numerical_cols = [cname for cname in X_train.columns if
X_train[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_train[my_cols].copy()
X_test= X_test[my_cols].copy()<categorify>
x_train = x_train.values.reshape(-1,28,28,1)
 x_test = x_test.values.reshape(-1,28,28,1)
 submit = submit.values.reshape(-1,28,28,1 )
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ] )<compute_train_metric>
model = Sequential()
model.add(Conv2D(64,(3, 3), input_shape=(28,28,1),padding="SAME"))
model.add(BatchNormalization(axis=-1))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(64,(3, 3),padding="SAME"))
model.add(BatchNormalization(axis=-1))
model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3),padding="SAME"))
model.add(BatchNormalization(axis=-1))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(128,(3, 3),padding="SAME"))
model.add(BatchNormalization(axis=-1))
model.add(Activation('relu'))
model.add(Conv2D(192,(3, 3),padding="SAME"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(192,(5, 5),strides=2,padding="SAME"))
model.add(Activation('relu'))
model.add(Flatten() )
def get_score(n_estimators):
my_pipeline = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', XGBRegressor(n_estimators=n_estimators, objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 10)) ])
 scores = -1 * cross_val_score(my_pipeline, X_train, y_train,
cv=3,
scoring='neg_mean_absolute_error')
 return scores.mean()
results = {}
n_estimators=[50,100,250,300,350,400]
for i in n_estimators:
results[i]=get_score(i)
 %matplotlib inline
plt.plot(list(results.keys()), list(results.values()))
plt.show()<save_to_csv>
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(Dense(10))
model.add(Activation('softmax'))
clf = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 10, n_estimators = 250)) ])
 clf.fit(X_train, y_train)
 preds_test = clf.predict(X_test)
 output = pd.DataFrame({'Id': X_test.index,
'SalePrice': preds_test})
 output.to_csv('submission.csv', index=False )<set_options>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3, verbose=1,factor=0.5,min_lr=0.00001)
 best_model = ModelCheckpoint('mnist_weights.h5', monitor='val_acc',
verbose=1, save_best_only=True, mode='max')
 early_stopping = EarlyStopping(monitor='val_loss', min_delta=1e-10,
patience=10,restore_best_weights=True )
warnings.filterwarnings('ignore' )<load_from_csv>
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )
df_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv',index_col='Id')
 df_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col ='Id' )<load_from_csv>
aug = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
rotation_range=10,
zoom_range = 0.,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 aug.fit(x_train )
def load_data() :
data_dir = Path('.. /input/house-prices-advanced-regression-techniques/')
 df_train = pd.read_csv(data_dir / 'train.csv', index_col = 'Id')
 df_test = pd.read_csv(data_dir / 'test.csv', index_col = 'Id')
 df = pd.concat([df_train,df_test])
 df = clean(df)
 df = encode(df)
 df = impute(df)
 df_train = df.loc[df_train.index,:]
df_test = df.loc[df_test.index, :]
return df_train, df_test 
<feature_engineering>
h = model.fit_generator(
 aug.flow(x_train, y_train, batch_size=64),
validation_data=(x_test, y_test),
steps_per_epoch=len(x_train)// 64,
epochs=20, verbose=1,
callbacks=[learning_rate_reduction,best_model,early_stopping]
)
def clean(df):
df['Exterior2nd'] = df['Exterior2nd'].replace({'Brk Cmn': 'BrkComm'})
 df['GarageYrBlt'] = df['GarageYrBlt'].where(df.GarageYrBlt <=2010, df.YearBuilt)
 df.rename(columns = {'1stFlrSF': 'FirstFlrSF', '2ndFlrSF': 'SecondFlrSF', '3SsnPorch':'Threeseasonporch'}, inplace=True)
 return df<define_variables>
y_pred = model.predict(x_test)
 y_pred = np.argmax(y_pred,axis = 1)
 accuracy_score(y_test, y_pred )
features_nom = ['MSSubClass', 'MSZoning', 'Street', 'Alley','LandContour', 'LotConfig','Neighborhood','Condition1','Condition2','BldgType','HouseStyle','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','Foundation','Heating','CentralAir','GarageType','MiscFeature','SaleType','SaleCondition']
five_levels = ['Po', 'Fa', 'TA','Gd','Ex']
ten_levels =list(range(10))
ordered_levels = {
'OverallQual':ten_levels,
'OverallCond':ten_levels,
'ExterQual':five_levels,
'ExterCond':five_levels,
'BsmtQual':five_levels,
'BsmtCond':five_levels,
'HeatingQC':five_levels,
'KitchenQual':five_levels,
'FireplaceQu':five_levels,
'GarageQual': five_levels,
'GarageCond':five_levels,
'PoolQC':five_levels,
'LotShape':['Reg', 'IR1','IR2','IR3'],
'LandSlope':['Sev', 'Mod','Gtl'],
'BsmtExposure': ['No','Mn','Av','Gd'],
'BsmtFinType1':['Unf','LwQ','Rec','BLQ','ALQ','GLQ'],
'BsmtFinType2':['Unf','LwQ','Rec','BLQ','ALQ','GLQ'],
'Functional':['Sal','Sev','Maj1','Maj2','Mod','Min2','Min1','Typ'],
'GarageFinish': ['Unf','RFn','Fin'],
'PavedDrive':['N','P','Y'],
'Utilities': ['NoSeWa','NoSewr','AllPub'],
'CentralAir':['N','Y'],
'Electrical':['Mix','FuseP','FuseF','FuseA','SBrkr'],
'Fence':['MnWw','GdWo','MnPrv','GdPrv'],
}
ordered_levels = {key:['None']+value for key, value in ordered_levels.items() }
def encode(df):
for name in features_nom:
df[name] = df[name].astype('category')
 if 'None' not in df[name].cat.categories:
df[name].cat.add_categories('None', inplace=True)
 for name, levels in ordered_levels.items() :
df[name] = df[name].astype(CategoricalDtype(levels, ordered=True))
return df<categorify>
result = model.predict(submit)
 results = np.argmax(result,axis = 1)
 results
def impute(df):
for name in df.select_dtypes('number'):
df[name] = df[name].fillna(0)
 for name in df.select_dtypes('category'):
df[name] = df[name].fillna('None')
 return df<compute_train_metric>
Label = pd.Series(results, name = 'Label')
 ImageId = pd.Series(range(1,28001), name = 'ImageId')
 submission = pd.concat([ImageId,Label], axis = 1)
 submission.to_csv('submission.csv', index = False )