Kaggle competition: Digit Recognizer
warnings.filterwarnings('ignore' )<load_from_csv>
train_data = pd.read_csv(".. /input/train.csv" )
train = pd.read_csv('.. /input/telstra-recruiting-network/train.csv.zip')
 test = pd.read_csv('.. /input/telstra-recruiting-network/test.csv.zip')
 severity_type = pd.read_csv('.. /input/telstra-recruiting-network/severity_type.csv.zip', error_bad_lines= False, warn_bad_lines= False)
 resource_type = pd.read_csv('.. /input/telstra-recruiting-network/resource_type.csv.zip', error_bad_lines= False, warn_bad_lines= False)
 log_failure = pd.read_csv('.. /input/telstra-recruiting-network/log_feature.csv.zip', error_bad_lines= False, warn_bad_lines= False)
 event_type = pd.read_csv('.. /input/telstra-recruiting-network/event_type.csv.zip', error_bad_lines=False, warn_bad_lines= False )<merge>
X = train_data[label_names[1:]]
y = train_data[label_names[0]]
train_1 = train.merge(severity_type, how = 'left', left_on='id', right_on='id')
 train_2 = train_1.merge(resource_type, how = 'left', left_on='id', right_on='id')
 train_3 = train_2.merge(log_failure, how = 'left', left_on='id', right_on='id')
 train_4 = train_3.merge(event_type, how = 'left', left_on='id', right_on='id' )<remove_duplicates>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 )
train_4.drop_duplicates(subset= 'id', keep= 'first', inplace = True)
 train_4.head()<import_modules>
img_rows, img_cols = 28, 28
num_classes =10
X_train = X_train.values.reshape([-1,28,28,1])/255
X_test = X_test.values.reshape([-1,28,28,1])/255
y_train = y_train.values
y_test = y_test.values
y_train = keras.utils.to_categorical(y_train, num_classes)
 y_test = keras.utils.to_categorical(y_test, num_classes )
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import train_test_split<prepare_x_and_y>
model = Sequential()
X = train_4[['id', 'location', 'severity_type', 'resource_type',
'log_feature', 'volume', 'event_type']]
y = train_4.fault_severity<split>
model.add(tf.keras.layers.Conv2D(32,(3, 3), padding='same',
input_shape=(img_rows,img_cols,1)))
 model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.Conv2D(32,(3, 3)))
 model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
 model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Conv2D(64,(3, 3), padding='same'))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.Conv2D(64,(3, 3)))
 model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
 model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(512))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(num_classes))
model.add(tf.keras.layers.Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=tf.train.AdamOptimizer() ,
metrics=['accuracy'])
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.20, random_state=101 )<define_variables>
batch_size =128
epochs = 100
history = model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test))
categorical_features_indices = np.where(X_train.dtypes == object)[0]<create_dataframe>
test_data = pd.read_csv(".. /input/test.csv")
 y_pred = model.predict(test_data.values.reshape([-1,28,28,1])/255,batch_size=batch_size)
 y_pred = np.argmax(y_pred,axis=1)
train_dataset = Pool(data=X_train,
label=y_train,
cat_features=categorical_features_indices)
 eval_dataset = Pool(data=X_validation,
label=y_validation,
cat_features=categorical_features_indices )<choose_model_class>
save = pd.DataFrame()
save["ImageId"] = list(range(1,len(y_pred)+1))
save["Label"] = y_pred
save.to_csv("submit.csv", index=False)