Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
import pandas as pd
import numpy as np
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D,AveragePooling2D,Average, Flatten, Dense, Dropout, Activation , Concatenate, Input , BatchNormalization
from keras.optimizers import SGD
from keras.utils import plot_model
from matplotlib import pyplot as plt
from keras.callbacks import EarlyStopping
from keras.callbacks import ReduceLROnPlateau
from keras import Model
from keras.preprocessing.image import ImageDataGenerator
from sklearn.utils import class_weight
le = LabelEncoder()
def prepare_data(X, building_data, weather_data, test=False):

 weather_data = timestamp_align(weather_data)
 X = X.merge(building_data, on="building_id", how="left")
 X = X.merge(weather_data, on=["site_id", "timestamp"], how="left")
 X.sort_values("timestamp")
 X.reset_index(drop=True)
 gc.collect()
X.timestamp = pd.to_datetime(X.timestamp, format="%Y-%m-%d %H:%M:%S")
 X.square_feet = np.log1p(X.square_feet)
 X["hour"] = X.timestamp.dt.hour
X["weekday"] = X.timestamp.dt.weekday
X['hour'] = X['hour'].astype('int8')
 X['weekday'] = X['weekday'].astype('int8')
 drop_features = ["timestamp", "site_id","sea_level_pressure", "precip_depth_1_hr",]
X.drop(drop_features, axis=1, inplace=True)
 X = reduce_mem_usage(X, use_float16=True)
 if test:
row_ids = X.row_id
X.drop("row_id", axis=1, inplace=True)
 return X, row_ids
else:
y = np.log1p(X.meter_reading)
 X.drop("meter_reading", axis=1, inplace=True)
 return X, y<prepare_x_and_y>
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
X_train, y_train = prepare_data(df_train, building, weather_train)
 del df_train, weather_train
gc.collect()<create_dataframe>
trainX = df_train.drop('label', axis=1 ).values
trainy = df_train['label'].values.reshape(-1,1)
 testX = df_test.values
%%time
X_half_1 = X_train[:int(X_train.shape[0] / 2)]
X_half_2 = X_train[int(X_train.shape[0] / 2):]
y_half_1 = y_train[:int(X_train.shape[0] / 2)]
y_half_2 = y_train[int(X_train.shape[0] / 2):]
categorical_features = ["building_id", "meter", "primary_use", "hour", "weekday"]
d_half_1 = lgb.Dataset(X_half_1, label=y_half_1, categorical_feature=categorical_features, free_raw_data=False)
 d_half_2 = lgb.Dataset(X_half_2, label=y_half_2, categorical_feature=categorical_features, free_raw_data=False)
 watchlist_1 = [d_half_1, d_half_2]
watchlist_2 = [d_half_2, d_half_1]
params = {
"objective": "regression",
"boosting": "gbdt",
"num_leaves": 40,
"learning_rate": 0.05,
"feature_fraction": 0.85,
"reg_lambda": 2,
"metric": "rmse",
"num_threads": 2
}
print("Building model with first half and validating on second half:")
 model_half_1 = lgb.train(params, train_set=d_half_1, num_boost_round=1000, valid_sets=watchlist_1, verbose_eval=200, early_stopping_rounds=200)
 print("Building model with second half and validating on first half:")
 model_half_2 = lgb.train(params, train_set=d_half_2, num_boost_round=1000, valid_sets=watchlist_2, verbose_eval=200, early_stopping_rounds=200 )<predict_on_test>
print('Train: X=%s, y=%s' %(trainX.shape, trainy.shape))
print('Test: X=%s, y=%s' %(testX.shape))
%%time
pred = np.zeros(len(X_train))
pred1 = model_half_1.predict(X_half_2)
 pred2 = model_half_2.predict(X_half_1 )<compute_test_metric>
trainX = trainX.reshape(( trainX.shape[0], 28, 28, 1))
testX = testX.reshape(( testX.shape[0], 28, 28, 1))
trainy = to_categorical(trainy )
pred = np.zeros(len(X_train))
pred[:len(pred2)] += pred2
pred[len(pred2):] += pred1
print(f"Half/Half RMSE: {rmse(pred, y_train):.4f}" )<load_pretrained>
trainX = trainX.astype('float32')
 testX = testX.astype('float32')
 trainX = trainX / 255.0
testX = testX / 255.0
pickle.dump(model_half_1, open("model_half_1.pkl", "wb"))
pickle.dump(model_half_2, open("model_half_2.pkl", "wb"))
pickle.dump(pred, open("pred_L1.pkl", "wb"))<drop_column>
datagen = ImageDataGenerator(
 validation_split = 0.2,
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 )
del X_train, X_half_1, X_half_2, y_half_1, y_half_2, d_half_1, d_half_2,
del watchlist_1, watchlist_2, pred, pred1, pred2
gc.collect()<load_from_csv>
batch_size = 128
Training_data = datagen.flow(trainX,
y=trainy,
batch_size = batch_size,
subset = 'training')
 Validation_data = datagen.flow(trainX,
y=trainy,
batch_size = batch_size,
subset = 'validation' )
weather_test = weather_df = pd.read_csv(DATA_PATH + 'weather_test.csv' )<feature_engineering>
class_weights = class_weight.compute_class_weight(
 'balanced',
np.unique(Training_data.classes),
Training_data.classes )
%%time
df_test = pd.read_feather('.. /input/ashrae-great-energy-predictor-iii-featherdataset/test.feather')
 weather_test = fill_weather_dataset(weather_test)
 df_test['hour'] = df_test['timestamp'].dt.hour
df_test['weekday'] = df_test['timestamp'].dt.weekday
df_test['bm'] = df_test['building_id'].apply(lambda x: str(x)) + '_' + df_test['meter'].apply(lambda x: str(x))
df_test = df_test.merge(bm, right_index=True, left_on=bm_cols, how='left')
 df_test.drop('bm', axis=1, inplace=True)
 df_test = reduce_mem_usage(df_test, use_float16=True)
 weather_test = reduce_mem_usage(weather_test, use_float16=True)
 X_test, row_ids = prepare_data(df_test, building, weather_test, test=True )<set_options>
es = EarlyStopping(monitor='val_loss',
mode='auto',
verbose=1,
patience=15)
 lrr= ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=4,
min_lr=1e-6 )
del df_test, building, weather_test
gc.collect()<predict_on_test>
input_model = Input(( trainX.shape[1],trainX.shape[2],trainX.shape[3]))
model1 = Conv2D(32,(5,5), activation='relu' )(input_model)
 model1 = BatchNormalization()(model1)
 model1 = Conv2D(32,(5,5), activation='relu', padding='same' )(model1)
 model1 = BatchNormalization()(model1)
 model1 = MaxPooling2D(( 2, 2))(model1)
 model1 = Conv2D(64,(3,3), activation='relu' ,padding='same' )(model1)
 model1 = BatchNormalization()(model1)
 model1 = Conv2D(64,(3,3), activation='relu' ,padding='valid' )(model1)
 model1 = BatchNormalization()(model1)
 model1 = AveragePooling2D(( 2, 2))(model1)
 model1 = Flatten()(model1)
 model1 = Dense(units = 512, activation = 'relu' )(model1)
 model1 = Dropout(rate = 0.4 )(model1)
 model1 = BatchNormalization()(model1)
 model1 = Dense(units = 20, activation = 'relu' )(model1)
 model1 = BatchNormalization()(model1)
 model1 = Dense(units = 15, activation = 'relu' )(model1)
 model1 = BatchNormalization()(model1)
 output1 = Dense(units = 10, activation = 'softmax' )(model1)
 model2 = Conv2D(32,(4,4), activation='relu' )(input_model)
 model2 = BatchNormalization()(model2)
 model2 = Conv2D(32,(4,4), activation='relu', padding='same' )(model2)
 model2 = BatchNormalization()(model2)
 model2 = MaxPooling2D(( 2, 2))(model2)
 model2 = Conv2D(32,(3,3), activation='relu', padding='same' )(model2)
 model2 = BatchNormalization()(model2)
 model2 = Conv2D(32,(3,3), activation='relu', padding='same' )(model2)
 model2 = BatchNormalization()(model2)
 model2 = AveragePooling2D(( 2, 2))(model2)
 model2 = Conv2D(32,(2,2), activation='relu' ,padding='same' )(model2)
 model2 = BatchNormalization()(model2)
 model2 = Conv2D(32,(2,2), activation='relu' ,padding='valid' )(model2)
 model2 = BatchNormalization()(model2)
 model2 = AveragePooling2D(( 2, 2))(model2)
 model2 = Flatten()(model2)
 model2 = Dense(units = 1024, activation = 'relu' )(model2)
 model2 = Dropout(rate = 0.5 )(model2)
 model2 = BatchNormalization()(model2)
 model2 = Dense(units = 64, activation = 'relu' )(model2)
 model2 = BatchNormalization()(model2)
 output2 = Dense(units = 10, activation = 'softmax' )(model2)
 model3 = Conv2D(32,(3,3), activation='relu' )(input_model)
 model3 = BatchNormalization()(model3)
 model3 = Conv2D(32,(3,3), activation='relu', padding='same' )(model3)
 model3 = BatchNormalization()(model3)
 model3 = MaxPooling2D(( 2, 2))(model3)
 model3 = Conv2D(32,(3,3), activation='relu', padding='same' )(model3)
 model3 = BatchNormalization()(model3)
 model3 = Conv2D(32,(3,3), activation='relu', padding='same' )(model3)
 model3 = BatchNormalization()(model3)
 model3 = Conv2D(64,(2,2), activation='relu' ,padding='valid' )(model3)
 model3 = BatchNormalization()(model3)
 model3 = Conv2D(128,(2,2), activation='relu' ,padding='valid' )(model3)
 model3 = BatchNormalization()(model3)
 model3 = MaxPooling2D(( 2, 2))(model3)
 model3 = Flatten()(model3)
 model3 = Dense(units = 512, activation = 'relu' )(model3)
 model3 = Dropout(rate = 0.2 )(model3)
 model3 = BatchNormalization()(model3)
 model3 = Dense(units = 64, activation = 'relu' )(model3)
 model3 = BatchNormalization()(model3)
 output3 = Dense(units = 10, activation = 'softmax' )(model3)
 output = Average()([output1, output2 , output3])
 model = Model(inputs= [input_model], outputs=[output] )
%%time
pred = np.expm1(model_half_1.predict(X_test, num_iteration=model_half_1.best_iteration)) / 2
del model_half_1
gc.collect()
pred += np.expm1(model_half_2.predict(X_test, num_iteration=model_half_2.best_iteration)) / 2
del model_half_2
gc.collect()<save_to_csv>
sgd = SGD(lr=0.1, momentum=0.99)
 model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'] )
submission = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(pred, 0, a_max=None)})
 submission['meter_reading'] = submission['meter_reading'].astype('float32')
 submission['row_id'] = submission['row_id'].astype('int32')
 submission.to_csv("submission.csv", index=False, chunksize=25000 )<filter>
history = model.fit_generator(Training_data,
epochs= 400,
validation_data= Validation_data,
class_weight=class_weights,
verbose=1,
callbacks=[es , lrr] )
submission[X_test['building_id'] > 105]<import_modules>
model.save_weights("MNIST_weights.h5" )
print(tf.__version__ )<load_from_csv>
pred = model.predict(testX)
 pred = pd.DataFrame(pred)
 pred['Label'] = pred.idxmax(axis=1)
 pred.head(5 )
df = pd.read_csv('.. /input/disaster-tweets-cleaned/df.csv')
 test_df = pd.read_csv('.. /input/disaster-tweets-cleaned/test_df.csv')
 print(df.shape, test_df.shape )<split>
pred['index'] = list(range(1,len(pred)+1))
pred.head()
train_txts, val_txts, y_train, y_val = train_test_split(
 df[col].values, df['target'].values,
shuffle = True, test_size = 0.15,
stratify = df['target'].values, 
)
 test_txts = test_df[col].values
y_test = test_df['target'].values
print('Train size:', train_txts.shape)
 print('Validation size:', val_txts.shape)
 print('Test size:', test_txts.shape )<load_pretrained>
submission.rename(columns={'index':'ImageId'},inplace = True)
 submission.head()
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased' )<string_transform>
submission.to_csv('submission.csv',index=False )