Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<save_to_csv>
%matplotlib inline
%load_ext autoreload
%autoreload 2
final_preds = final_preds.argmax(axis=1)
 dfx.label = final_preds
dfx.to_csv("submission.csv", index=False )<save_to_csv>
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
df = pd.read_csv('/kaggle/input/finalsub3/finalsub2.csv')
 df.to_csv('submission.csv', index=False )<set_options>
print("Training Data : ")
 train_data.head(3 ).iloc[:,:17]
warnings.filterwarnings("ignore" )<load_from_csv>
train_data_norm = train_data.iloc[:, 1:] / 255.0
test_data_norm = test_data / 255.0
train_data = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 test_data = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 train_data.describe().drop('count' ).T<filter>
num_examples_train = train_data.shape[0]
num_examples_test = test_data.shape[0]
n_h = 32
n_w = 32
n_c = 3
train_data[train_data['winPlacePerc'].isnull() ]<feature_engineering>
Train_input_images = np.zeros(( num_examples_train, n_h, n_w, n_c))
Test_input_images = np.zeros(( num_examples_test, n_h, n_w, n_c))
mapper = lambda x: 'solo' if('solo'in x)else 'duo' if('duo' in x)or('crash'in x)else 'squad'
train_data['matchType'] = train_data['matchType'].apply(mapper)
 match_type_counts=train_data.groupby('matchId')['matchType'].first().value_counts().sort_values(ascending=False )<concatenate>
for example in range(num_examples_train):
Train_input_images[example,:28,:28,0] = train_data.iloc[example, 1:].values.reshape(28,28)
 Train_input_images[example,:28,:28,1] = train_data.iloc[example, 1:].values.reshape(28,28)
 Train_input_images[example,:28,:28,2] = train_data.iloc[example, 1:].values.reshape(28,28)
 for example in range(num_examples_test):
Test_input_images[example,:28,:28,0] = test_data.iloc[example, :].values.reshape(28,28)
 Test_input_images[example,:28,:28,1] = test_data.iloc[example, :].values.reshape(28,28)
 Test_input_images[example,:28,:28,2] = test_data.iloc[example, :].values.reshape(28,28 )
all_data = train_data.append(test_data, sort=False ).reset_index(drop=True)
 del train_data, test_data
gc.collect()<feature_engineering>
for example in range(num_examples_train):
Train_input_images[example] = cv2.resize(Train_input_images[example],(n_h, n_w))
for example in range(num_examples_test):
Test_input_images[example] = cv2.resize(Test_input_images[example],(n_h, n_w))
match = all_data.groupby('matchId')
 all_data['killsPerc'] = match['kills'].rank(pct=True ).values
all_data['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
all_data['walkPerc_killsPerc'] = all_data['walkDistancePerc'] / all_data['killsPerc']
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']<data_type_conversions>
Train_labels = np.array(train_data.iloc[:, 0] )
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
image_generator = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.3,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=False,
samplewise_center=True,
samplewise_std_normalization=True 
)
 validation_datagen = ImageDataGenerator()
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']<drop_column>
pretrained_model = keras.applications.resnet50.ResNet50(input_shape=(n_h, n_w, n_c),
include_top=False, weights='imagenet')
 model = keras.Sequential([
pretrained_model,
keras.layers.Flatten() ,
keras.layers.Dense(units=60, activation='relu'),
keras.layers.Dense(units=10, activation='softmax')
 ] )
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<groupby>
Optimizer = 'RMSprop'
model.compile(optimizer=Optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )
match = all_data.groupby(['matchId'])
 group = all_data.groupby(['matchId','groupId','matchType'])
 agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']<merge>
train_images, dev_images, train_labels, dev_labels = train_test_split(Train_input_images,
Train_labels,
test_size=0.1, train_size=0.9,
shuffle=True,
random_state=44)
 test_images = Test_input_images
minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
match_data = pd.merge(match_data, minKills[nKills][['matchId','groupId',c]], how='left')
 del minKills, nKills
match_data = reduce_mem_usage(match_data )<merge>
train_datagen = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.3,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2,
horizontal_flip=False)
 validation_datagen = ImageDataGenerator()
all_data = pd.merge(all_data, match_data)
 del match_data
gc.collect()
all_data['enemy.players'] = all_data['m.players'] - all_data['players']
for c in sum_col:
all_data['p.max_msum.' + c] = all_data['max.' + c] / all_data['m.sum.' + c]
all_data['p.max_mmax.' + c] = all_data['max.' + c] / all_data['m.max.' + c]
all_data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(all_data, 0 )<merge>
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')> 0.999999):
print("Stop training!")
 self.model.stop_training = True
match = all_data.groupby('matchId')
 matchRank = match[numcols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 all_data = reduce_mem_usage(pd.concat([all_data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()
match = all_data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
all_data = pd.merge(all_data, matchRank)
 for c in numcols:
all_data['rank.' + c] = all_data['rank.' + c] / all_data['max.rank.' + c]
all_data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()<merge>
EPOCHS = 5
batch_size = 212
history = model.fit_generator(train_datagen.flow(train_images,train_labels, batch_size=batch_size),
steps_per_epoch=train_images.shape[0] / batch_size,
epochs=EPOCHS,
validation_data=validation_datagen.flow(dev_images,dev_labels,
batch_size=batch_size),
validation_steps=dev_images.shape[0] / batch_size,
callbacks=[callbacks] )
killMinorRank = all_data[['matchId','min.kills','max.killPlace']].copy()
group = killMinorRank.groupby(['matchId','min.kills'])
 killMinorRank['rank.minor.maxKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 killMinorRank = all_data[['matchId','max.kills','min.killPlace']].copy()
group = killMinorRank.groupby(['matchId','max.kills'])
 killMinorRank['rank.minor.minKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()<count_unique_values>
submission = pd.read_csv('.. /input/digit-recognizer-submission/submission.csv')
 submission.to_csv('digit_submission.csv', index=False )
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
all_data.drop(constant_column, axis=1, inplace=True )<feature_engineering>
mnist_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 mnist_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'])], axis=1)
 all_data.drop(['matchType'], axis=1, inplace=True)
 all_data['matchId'] = all_data['matchId'].apply(lambda x: int(x,16))
all_data['groupId'] = all_data['groupId'].apply(lambda x: int(x,16))<count_missing_values>
mnist_train_data = mnist_train.loc[:, "pixel0":]
mnist_train_label = mnist_train.loc[:, "label"]
mnist_train_data = mnist_train_data/255.0
mnist_test = mnist_test/255.0
null_cnt = all_data.isnull().sum().sort_values()<categorify>
standardized_scalar = StandardScaler()
standardized_data = standardized_scalar.fit_transform(mnist_train_data)
 standardized_data.shape
cols = [col for col in all_data.columns if col not in ['Id','matchId','groupId']]
for i, t in all_data.loc[:, cols].dtypes.iteritems() :
if t == object:
all_data[i] = pd.factorize(all_data[i])[0]
all_data = reduce_mem_usage(all_data )<prepare_x_and_y>
cov_matrix = np.matmul(standardized_data.T, standardized_data)
 cov_matrix.shape
X_train = all_data[all_data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId','groupId']].copy()
train_matchId = X_train['matchId']
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True )<train_model>
lambdas, vectors = eigh(cov_matrix, eigvals=(782, 783))
vectors.shape
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 128,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )<concatenate>
new_coordinates = np.matmul(vectors, standardized_data.T)
 print(new_coordinates.shape)
 new_coordinates = np.vstack(( new_coordinates, mnist_train_label)).T
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )<feature_engineering>
df_new = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_new.head()
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
X_test['winPlacePerc'] = X_test['winPlacePerc'].clip(lower=0,upper=1 )<feature_engineering>
pca = decomposition.PCA()
pca.n_components = 2
pca_data = pca.fit_transform(standardized_data)
 pca_data.shape
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0<save_to_csv>
pca_data = np.vstack(( pca_data.T, mnist_train_label)).T
test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )<set_options>
df_PCA = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_PCA.head()
warnings.filterwarnings('ignore')
 %matplotlib inline 
<load_from_csv>
mnist_train_data = np.array(mnist_train_data)
 mnist_train_label = np.array(mnist_train_label )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )<prepare_x_and_y>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler
X_train = train.drop(['label'], axis=1)
 y_train = train['label']<feature_engineering>
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of ytrain after encoding: ", mnist_train_label.shape )
X_train /= 255.0
test /= 255.0<train_model>
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def compile_model(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])
 def train_model(model, train, test, epochs, split):
history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history
X_train1 = X_train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1 )<categorify>
cnn_model = build_model(( 28, 28, 1))
compile_model(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2 )
y_train = to_categorical(y_train, num_classes=10 )<split>
predictions = cnn_model.predict(mnist_test_arr )
X_train, X_val , y_train, y_val = train_test_split(X_train1, y_train, test_size=0.2 )<choose_model_class>
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(filters = 64, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Conv2D(filters = 64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Conv2D(filters = 64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Conv2D(filters = 64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(10, activation='softmax'))<choose_model_class>
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_first_submission.csv', index=False )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"] )<train_model>
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import models, layers, utils
from tensorflow.keras import Sequential
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, MaxPool2D
history = model.fit(X_train, y_train, epochs=25, validation_data=(X_val, y_val))<predict_on_test>
x_train, x_val, y_train, y_val = train_test_split(mnist_train_data, mnist_train_label, test_size = 0.2, random_state = 2 )
y_pred = model.predict(test )<save_to_csv>
def define_model() :
model = Sequential()
model.add(Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(layers.BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.BatchNormalization())
 model.add(Conv2D(filters=256, kernel_size=(3,3), activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(layers.BatchNormalization())
 model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model
submission['Label'] = results
submission.to_csv('submission.csv', index=False )<import_modules>
model = define_model()
model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy'] )
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from skimage import color
from skimage import measure
from skimage.filters import try_all_threshold
from skimage.filters import threshold_otsu
from skimage.filters import threshold_local
import keras
from keras import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from keras.optimizers import Adam
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint<load_from_csv>
model.fit(x_train, y_train , epochs=30 )
df_train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 df_test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
predictions = model.predict(mnist_test_arr )
y_train = df_train['label']
X_train = df_train.drop('label', axis = 1)
 X_test = np.array(df_test )<categorify>
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))
y_train = to_categorical(y_train, num_classes = 10)
 y_train.shape<split>
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_second_submission.csv', index=False )