Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_on_grid>
import numpy
import pandas
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping
model_grid = {'max_depth': [None, 8], 'min_samples_split': [4,9,16], 'min_samples_leaf':[1,4], 'max_features':['sqrt', 0.5, None]}
type_model = ExtraTreesClassifier(n_estimators=25, random_state=SEED)
 grid = RandomizedSearchCV(type_model, model_grid, n_iter=10, cv=5, scoring="roc_auc")
 grid.fit(X, y)
 print("Best parameters for Type Model:")
 print(grid.best_params_)
 type_model.set_params(**grid.best_params_)
 type_model.fit(X, y)
 imputations = type_model.predict(tofill.drop(["T_FC", "T_IL", "revenue"], axis=1))
df.loc[(df.T_FC==0)&(df.T_IL==0), "T_FC"] = imputations
df = df.drop(["T_IL"], axis=1)
 print("% labeled FC in the training set:"), df.T_FC.mean()
print("% of imputed values labeled FC:"), np.mean(imputations )<categorify>
train_data = pandas.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pandas.read_csv('.. /input/digit-recognizer/test.csv' )
print("Pre-binarizing columns:"), len(df.columns)
 for col in df.columns:
if col[0] == 'P':
print(col), len(df[col].unique()), "unique values"
df = df.join(pd.get_dummies(df[col], prefix=col))
df = df.drop([col, df.columns[-1]], axis=1)
 print("Post-binarizing columns:"), len(df.columns )<prepare_x_and_y>
train_y = to_categorical(train_data["label"])
 train_x = train_data.loc[:, train_data.columns != "label"]
train_x /= 256
train = df.loc[pd.notnull(df.revenue)]
test = df.loc[pd.isnull(df.revenue)].drop(['revenue'], axis=1)
 y = train.revenue.apply(np.sqrt)
 X = train.drop(["revenue"], axis=1 )<train_on_grid>
callback = EarlyStopping(monitor='loss', patience=8, restore_best_weights=True )
model_grid = [{'normalize': [True, False], 'alpha': np.logspace(0,10)}]
model = Ridge()
grid = GridSearchCV(model, model_grid, cv=LeaveOneOut() , scoring='neg_mean_squared_error')
 grid.fit(X, y)
 print("Best parameters set found on development set:")
 print(grid.best_params_)
 model.set_params(**grid.best_params_)
 model.fit(X, y )<save_to_csv>
model = Sequential()
model.add(Conv2D(64,(3,3), activation='relu', input_shape=(28,28, 1)))
 model.add(MaxPooling2D(( 2,2)))
 model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D(( 2,2)))
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
submission = pd.DataFrame(columns=['Prediction'],index=test.index, data=model.predict(test))
submission.Prediction = submission.Prediction.apply(np.square)
 submission.index.name='Id'
submission.to_csv("TFI_Ridge.csv")
 submission.describe().astype(int )<set_options>
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )
plt.style.use("seaborn-whitegrid")
 plt.rc("figure", autolayout=True)
 plt.rc(
 "axes",
labelweight="bold",
labelsize="large",
titleweight="bold",
titlesize=14,
titlepad=10, 
)
 warnings.filterwarnings('ignore' )<load_from_csv>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 datagen.fit(train_x )
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<load_from_csv>
history = model.fit(datagen.flow(train_x, train_y, batch_size=32), epochs=80, callbacks=[callback] )
data_dir = Path(".. /input/house-prices-advanced-regression-techniques/")
 df = pd.read_csv(data_dir / "train.csv", index_col="Id")
 df.Exterior2nd.unique()<feature_engineering>
test_data /= 256
test_x = test_data.values.reshape(-1, 28, 28, 1)
 y_pred = model.predict(test_x)
 y_pred = numpy.argmax(y_pred, axis=1)
 y_pred = pandas.Series(y_pred,name='Label')
 submission = pandas.concat([pandas.Series(range(1, 28001), name='ImageId'), y_pred], axis=1 )
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>
submission.to_csv('my_submission.csv', index=False )