Kaggle competition: Digit Recognizer
model.fit(XTrain, YTrain )<predict_on_test>
warnings.filterwarnings("ignore")
 train1=pd.read_csv(".. /input/digit-recognizer/train.csv")
 train=pd.read_csv(".. /input/new-digit-dataset/Digit_Train_Final.csv")
 test=pd.read_csv(".. /input/digit-recognizer/test.csv")
 train.head()
prediction = model.predict(XTest)
 prediction[prediction < 0] = 0
prediction[prediction > 1] = 1<compute_test_metric>
x=train.drop(labels=["label"],axis=1)
 y=train["label"]
mean_absolute_error(prediction, YTest )<load_from_csv>
del train['Unnamed: 0']
test_df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/test_V2.csv")
 test_df<categorify>
x=train.drop(['label'],axis=1)
 y=train['label']
encoded_ = onehot(test_df.matchType, "matchType" )<merge>
norm=MinMaxScaler()
x=norm.fit_transform(x)
 test=norm.fit_transform(test)
test_df_j = test_df.copy()
train_df_j = test_df_j.drop('matchType', axis = 1)
 test_df_j = test_df_j.join(encoded_[0] )<create_dataframe>
y=to_categorical(y,num_classes=10)
 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.009,random_state=33 )
out_df = pd.DataFrame(np.array([test_df_j.Id.values, model.predict(test_df_j[features])] ).T, columns = ['Id', 'winPlacePerc'] )<save_to_csv>
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size = 3,padding = 'Same',input_shape =(28,28,1)))
 model.add(Conv2D(filters = 128, kernel_size = 3,padding = 'Same',input_shape =(28,28,1)))
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.1))
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same'))
model.add(Conv2D(filters = 64, kernel_size = 3,padding = 'Same'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.1))
out_df.to_csv('submission.csv', index = False )<load_from_csv>
model.add(Flatten())
 model.add(Dense(256))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
 model.add(Dense(10, activation = "softmax"))
model.summary()
def BuildFeature(is_train=True):
y = None
test_idx = None
if is_train:
print("Reading train.csv")
 df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/train_V2.csv")
 df = df[df['maxPlace'] > 1]
else:
print("Reading test.csv")
 df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/test_V2.csv")
 test_idx = df.Id
df = reduce_mem_usage(df)
 print("Delete Unuseful Columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 if is_train:
print("Read Labels")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("Read Group mean features")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train:
df_out = agg.reset_index() [['matchId','groupId']]
else:
df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group max features")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group min features")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("Read Group size features")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("Read Match mean features")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("Read Match size features")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
return X, y, feature_names, test_idx<prepare_x_and_y>
optimizer=Adamax(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
X_Train, Y_Train, train_columns, _ = BuildFeature(is_train=True)
 X_test, _, _ , test_idx = BuildFeature(is_train=False )<sort_values>
model.compile(optimizer = optimizer , loss = "binary_crossentropy", metrics=["accuracy"] )
total = X_Train.isnull().sum().sort_values(ascending= False)
 print(total )<sort_values>
lr_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.1, min_lr=0.00001)
 b_l=BaseLogger(stateful_metrics=None)
 e_stop = EarlyStopping(monitor='val_acc', patience=4, verbose=1 )
total = X_test.isnull().sum().sort_values(ascending= False)
 print(total )<prepare_x_and_y>
callbacks=[lr_reduction,b_l,e_stop]
batch_size=89
epochs=20
X_Train['traveldistance']=X_Train['walkDistance_min']+X_Train['rideDistance_min']+X_Train['swimDistance_min']
X_Train['killingwithoutmoving']=(X_Train['kills_min']>0)&(X_Train['traveldistance']== 0)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['killingwithoutmoving']==True].index,axis = 0)
 X_Train.drop(X_Train[X_Train['killingwithoutmoving']==True].index,inplace=True)
 X_Train.drop(['killingwithoutmoving'],axis=1,inplace=True)
 X_Train.drop(['traveldistance'],axis=1,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['walkDistance_max']>10000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['walkDistance_max']>10000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['swimDistance_max']>2000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['swimDistance_max']>2000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['rideDistance_max']>20000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['rideDistance_max']>20000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['longestKill_max']>1000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['longestKill_max']>1000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['kills_max']>30].index,axis = 0)
 X_Train.drop(X_Train[X_Train['kills_max']>30].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['roadKills_max']>12].index,axis = 0)
 X_Train.drop(X_Train[X_Train['roadKills_max']>12].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['weaponsAcquired_max']>70].index,axis = 0)
 X_Train.drop(X_Train[X_Train['weaponsAcquired_max']>70].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['heals_max']>35].index,axis = 0)
 X_Train.drop(X_Train[X_Train['heals_max']>35].index,inplace=True)
 X_Train.info()<choose_model_class>
datagen = ImageDataGenerator()
datagen.get_random_transform(img_shape=(28,28,1),seed=3)
 datagen.flow(x=x, y=y, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None)
 datagen.fit(x_train )
model = Sequential()
model.add(Dense(256, input_dim=X_Train.shape[1] ,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(128,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(64,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(32,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(1,kernel_initializer='normal', activation = 'relu'))
model.compile(optimizer= 'adam', loss= 'mae',metrics= ['mse'])
 model.fit(X_Train, Y_Train, batch_size= 2000, epochs = 15, verbose = 1)
 y_pred = model.predict(X_test, batch_size= 10)
 result = test_idx.reset_index(level=None, drop=False, name=None, inplace=False)
 result['index'] = y_pred
result.set_index(["Id"], inplace=True)
 result.rename(columns={'index': 'winPlacePerc'}, inplace=True)
 result.to_csv('submission.csv', index= True )<save_to_csv>
information = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),epochs=epochs,validation_data =(x_test, y_test), steps_per_epoch=x_train.shape[0] // batch_size,callbacks=callbacks )
<set_options>
karsilastirma=pd.DataFrame()
karsilastirma["prediction"]=Y_head_class
karsilastirma["true"]=Y_true
%matplotlib inline 
<load_from_csv>
karsilastirma['correct'] = karsilastirma.prediction == karsilastirma.true
karsilastirma["correct"].value_counts()
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 train_df.head()<train_model>
karsilastirma[karsilastirma['correct']==False]
X = train_df.drop('label', axis=1 ).values.reshape(( -1, 28, 28, 1)) / 255
y = utils.to_categorical(train_df['label'], 10)
 print(X.shape, y.shape )<train_model>
predicted= model.predict(test )
train_datagen = ImageDataGenerator(
 rotation_range=8,
shear_range=0.3,
zoom_range=0.08,
width_shift_range=0.08,
height_shift_range=0.08,
validation_split=0.1)
 train_datagen.fit(X )<choose_model_class>
predicted = np.argmax(predicted,axis = 1)
 predicted = pd.Series(predicted,name="Label" )
<choose_model_class><EOS> 
result = pd.concat([pd.Series(range(1,len(test)+1),name = "ImageId"),predicted],axis = 1)
 result.to_csv("Digit_Recognizer_predicted.csv",index=False )