Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
np.random.seed(1)
 get_ipython().magic('matplotlib inline')
 print(tf.__version__ )
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor<load_from_csv>
train_path=".. /input/digit-recognizer/train.csv"
test_path=".. /input/digit-recognizer/test.csv"
train = pd.read_csv(train_path)
 test= pd.read_csv(test_path)
 print(train.shape)
 print(test.shape)
 train.head()
np.random.seed(seed=1234)
 skiprows = np.random.rand(55 * 10 ** 7)> 0.02
skiprows[0] = False
df_ = pd.read_csv("/kaggle/input/new-york-city-taxi-fare-prediction/train.csv", skiprows=lambda x: skiprows[x])
 df_.head()<define_variables>
x = x.values.reshape(-1,28,28,1)
 print("x shape: ",x.shape)
 y = to_categorical(y, num_classes = 10)
 print("y shape: ",y.shape)
 print(y[10])
 plt.imshow(-x[10][:,:,0],cmap='gray')
 plt.axis(False)
 plt.show()
lon_min, lon_max = -75, -72
lat_min, lat_max = 40, 43<feature_engineering>
X_train, X_val, Y_train, Y_val = train_test_split(x, y, test_size = 0.2, random_state=1)
 print("x_train shape",X_train.shape)
 print("x_val shape",X_val.shape)
 print("y_train shape",Y_train.shape)
 print("y_val shape",Y_val.shape )
df = df_.copy()
df = df.drop(columns=["key"])
 df["date"] = df["pickup_datetime"].apply(lambda x: x.split() [0])
 df["time"] = df["pickup_datetime"].apply(lambda x: x.split() [1])
 df = df.drop("pickup_datetime", axis=1)
 df["year"] = df["date"].apply(lambda x: int(x.split("-")[0]))
df["month"] = df["date"].apply(lambda x: int(x.split("-")[1]))
df["day"] = df["date"].apply(lambda x: int(x.split("-")[2]))
df = df.drop("date", axis=1)
 df["time"] = df["time"].apply(lambda x: int(x[:2])* 60 + int(x[3:5]))
df["befor_shock"] =(( df["year"] <= 2011)|(( df["year"] <= 2012)&(df["month"] <= 8)) ).apply(int)
 df = df[ 
(~df["dropoff_longitude"].isnull())& 
(~df["dropoff_latitude"].isnull())
 ]
df = df[ 
(lon_min < df["pickup_longitude"])& 
(df["pickup_longitude"] < lon_max)& 
(lat_min < df["pickup_latitude"])& 
(df["pickup_latitude"] < lat_max)& 
(lon_min < df["dropoff_longitude"])& 
(df["dropoff_longitude"] < lon_max)& 
(lat_min < df["dropoff_latitude"])& 
(df["dropoff_latitude"] < lat_max)
 ]
df.head()<prepare_x_and_y>
datagen = ImageDataGenerator(rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = False,
vertical_flip = False)
 datagen.fit(X_train )
X = np.array(df.drop(
 columns=[
"fare_amount",
] 
))
y = np.array(df["fare_amount"] )<split>
Image = [[0,0,0],
[0,1,1],
[0,1,2]]
kernel = [[-4,0,0],
[0,0,0],
[0,0,4]]
result = signal.convolve(Image,kernel,'valid')
 result2 = 4*0 + 0*0 + 0*0 + 0*0 + 0*1 + 0*1 + 0*1 + 0*0 + -4*2
print("Result with scipy: {}
Result with manual calculs : {}".format(result,result2))
print("
With 'same':
{}".format(signal.convolve(Image,kernel,'same')))
 print("
With 'padding':
{}".format(signal.convolve(Image,kernel,'full')) )
np.random.seed(seed=1234)
 train_rows = np.random.rand(y.size)> 0.2
X_train, y_train = X[train_rows, :], y[train_rows]
X_valid, y_valid = X[~train_rows, :], y[~train_rows]<train_model>
def define_model(actifun="elu",actifundense1="elu",actifundense2="softsign",optimizer="Adam"):
model = model = Sequential([
Conv2D(32,(3, 3), padding = 'same', activation = 'relu', input_shape =(28,28,1)) ,
BatchNormalization() ,
Conv2D(32,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.2),
Conv2D(64,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
Conv2D(64,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.2),
Conv2D(128,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
Conv2D(128,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.2),
Flatten() ,
Dense(256, activation = 'relu'),
Dropout(0.25),
Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 return model
model = define_model()
model.summary()
model = RandomForestRegressor(max_depth=30, n_estimators=100, n_jobs=-1)
 model.fit(X_train, y_train)
 y_valid_pred = model.predict(X_valid)
 rmse(y_valid, y_valid_pred )<load_from_csv>
%%time
X_train_train, X_train_val, Y_train_train, Y_train_val = train_test_split(X_train, Y_train, test_size = 0.3, random_state = 0)
 models={}
history={}
optim_list = ["Adam","RMSprop"]
lr_sched = ReduceLROnPlateau(monitor = 'val_acc',
patience = 10,
verbose = 1,
factor = 0.1,
min_lr = 0.00001)
 early_stopping = EarlyStopping(monitor = 'val_acc',
patience = 10,
verbose = 1,
mode = 'auto',
restore_best_weights = True)
 epochs=50
batch_size = 200
for optim in optim_list:
for fun in ["elu","relu"]:
fun1 = fun
fun2 = fun
print(" ")
 print(" ")
 mtype = optim + ' + ' + fun1 + ' + ' + fun2 + ' + ' + "softsign"
print(mtype)
 print(" ")
 models[mtype] = define_model(fun1,fun2,"softsign",optim)
 history[mtype] = models[mtype].fit(X_train,Y_train,
validation_split=0.2,epochs=epochs
,shuffle=True,steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[lr_sched,early_stopping] )
test = pd.read_csv("/kaggle/input/new-york-city-taxi-fare-prediction/test.csv")
 test.head()<feature_engineering>
rslts={}
k=0
for optim in optim_list:
for fun in ["elu","relu"]:
fun1 = fun
fun2 = fun
fun3 = "softsign"
mtype = optim + ' + ' + fun1 + ' + ' + fun2 + ' + ' + fun3
rslts[mtype] =(models[mtype].evaluate(X_val, Y_val, verbose = 0)[1])
 k=k+1
for item in sorted(rslts.items() , key=lambda x: x[1],reverse=True):
print(item[0])
 print(item[1])
 print(" ")
 plt.figure(figsize=(30,30))
i=0
fun ="elu"
fun1 = fun
fun2 = fun
fun3 = "softsign"
mtype = optim + ' + ' + fun1 + ' + ' + fun2 + ' + ' + fun3
plt.subplot(2,1,i+1)
 plt.plot(history[mtype].history['loss'], label='loss')
 plt.plot(history[mtype].history['val_loss'], color='b', label='val_loss')
 plt.title("mtype")
 plt.xlabel("Number of Epochs")
 plt.ylabel("Loss")
 plt.legend()
i = i +1
plt.show()
test["date"] = test["pickup_datetime"].apply(lambda x: x.split() [0])
 test["time"] = test["pickup_datetime"].apply(lambda x: x.split() [1])
 test = test.drop("pickup_datetime", axis=1)
 test["year"] = test["date"].apply(lambda x: int(x.split("-")[0]))
test["month"] = test["date"].apply(lambda x: int(x.split("-")[1]))
test["day"] = test["date"].apply(lambda x: int(x.split("-")[2]))
test = test.drop("date", axis=1)
 test["time"] = test["time"].apply(lambda x: int(x[:2])* 60 + int(x[3:5]))
test["before_shock"] =(( test["year"] <= 2011)|(( test["year"] <= 2012)&(test["month"] <= 8)) ).apply(int)
 test.head()<save_to_csv>
datagen.fit(x)
 X_train, X_val, Y_train, Y_val = train_test_split(x, y, test_size = 0.1, random_state = 0)
 lr_sched = ReduceLROnPlateau(monitor = 'val_acc',
patience = 10,
verbose = 1,
factor = 0.1,
min_lr = 0.00001)
 early_stopping = EarlyStopping(monitor = 'val_acc',
patience = 10,
verbose = 1,
mode = 'auto',
restore_best_weights = True)
 epochs=50
batch_size = 200
model = define_model("elu","elu","softsign","RMSprop")
 history_final = model.fit(datagen.flow(X_train,Y_train, batch_size = batch_size),
epochs = epochs,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data =(X_val, Y_val),
callbacks = [lr_sched, early_stopping] )
X_test = np.array(test.drop(columns=[
"key",
]))
y_pred = model.predict(X_test)
 test["fare_amount"] = y_pred
submission= test[["key", "fare_amount"]]
submission.to_csv("./submission.csv", index=False )<load_from_csv>
test_path=".. /input/digit-recognizer/test.csv"
test = pd.read_csv(test_path)
 test = test / 255.0
test = test.values.reshape(-1,28,28,1)
 Y_pred_test = model.predict(test)
 Y_pred_classes_test = np.argmax(Y_pred_test,axis = 1)
 np.savetxt('submission.csv',
np.c_[range(1,len(test)+1), Y_pred_classes_test],
delimiter=',',
header = 'ImageId,Label',
comments = '',
fmt='%d' )