Kaggle competition: Don't Get Kicked!
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 print(X_train.shape)
 print(Y_train.shape)
 print(X_test.shape )<choose_model_class>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
nets = 15
model = [0] *nets
ks1=3
ks2=3
ks3=5
ks4=5
ks5=7
ks6=7
init = he_normal(seed=82)
 for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size=ks1, activation='relu', kernel_initializer = init, input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=ks2, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=ks3, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size=ks4, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=ks5, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=ks6, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size=4, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
optA = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model[j].compile(optimizer=optA, loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary() 
<choose_model_class>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.10, width_shift_range=0.1, height_shift_range=0.1 )<choose_model_class>
train_data.isnull().sum()
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.31623, min_delta=1e-4)
 early_stopping = EarlyStopping(monitor='val_acc', min_delta=1e-5, patience=10, verbose=1, mode='max', restore_best_weights=True)
<train_model>
test_data.isnull().sum()
history = [0] * nets
epochs = 50
for j in range(nets):
rs = 10*j + 1
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, train_size = 0.9, random_state = rs)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64), epochs = epochs, steps_per_epoch = X_train2.shape[0]//64, validation_data =(X_val2,Y_val2), callbacks=[learning_rate_reduction, early_stopping], verbose=0)
 maxpos = history[j].history['val_acc'].index(max(history[j].history['val_acc']))
print("N{0:d}:: Max val_acc={1:.5f} at Epoch {2:d} Min_tr_loss={3:.5f} Min_val_loss={4:.5f}".format(j+1,max(history[j].history['val_acc']),maxpos+1, min(history[j].history['loss']), min(history[j].history['val_loss'])))
<save_to_csv>
train_data['IsBadBuy'].value_counts()
results = np.zeros(( X_test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("MNIST_32C3C3C564C5C7C7128C4FC10_50e_LR10_RLROP_ES_BN_DA_DO40_TR90_N15_ADAM_xx.csv",index=False)
 print(submission.shape)
<import_modules>
train_data['Model'].value_counts()
import numpy as np
import pandas as pd<load_from_csv>
train_data.drop('Model',axis=1 , inplace=True)
 test_data.drop('Model',axis=1 , inplace=True )
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 data = pd.concat([train, test], sort=False )<count_missing_values>
train_data['Trim'].value_counts()
data.isnull().sum()<count_missing_values>
train_data.drop('Trim' , inplace=True ,axis=1)
 test_data.drop('Trim' , inplace=True , axis=1 )
data.isnull().sum()<count_missing_values>
train_data['SubModel'].value_counts()
train.isnull().sum()<count_missing_values>
train_data.drop('SubModel' , axis=1 , inplace=True)
 test_data.drop('SubModel' , axis=1 , inplace=True )
train.isnull().sum()<count_missing_values>
train_data['Color'].value_counts()
test.isnull().sum()<count_missing_values>
train_data['Color'].fillna(value='Color_unknown' , inplace =True)
 test_data['Color'].fillna(value='Color_unknown' , inplace =True )
test.isnull().sum()<categorify>
print('Number of null values in color column ' + str(train_data['Color'].isnull().sum()))
print('Number of null values in color column ' + str(test_data['Color'].isnull().sum()))
data['Sex'].replace(['male','female'], [0, 1], inplace=True )<data_type_conversions>
train_data['Transmission'].value_counts()
data = data.loc[data['Embarked'].isnull() == False, :].copy()
print(data.isnull().sum())
<categorify>
test_data['Transmission'].value_counts()
data = pd.concat([data, data['Embarked'].str.get_dummies() ], axis=1)
 data.rename(columns={'S': 'Embarked_S', 'C': 'Embarked_C', 'Q': 'Embarked_Q'}, inplace=True)
 print(data.isnull().sum())
<feature_engineering>
train_data[train_data['Transmission']=='Manual']
if data['Fare'].isnull().sum() != 0:
dict_lookup_fare = data.loc[:, ['Fare', 'Pclass', 'Embarked']].groupby(['Pclass', 'Embarked'] ).mean().to_dict().get('Fare')
 data.loc[data['Fare'].isnull() , 'Fare'] = [dict_lookup_fare.get(i)for i in list(zip(data.loc[data['Fare'].isnull() , 'Pclass'],
data.loc[data['Fare'].isnull() , 'Embarked'])) ]
print(data.isnull().sum() )<feature_engineering>
train_data['Transmission'].replace("Manual" ,"MANUAL" , inplace=True )
dict_mean_age_by_pclass = data.loc[:, ['Age', 'Pclass']].groupby('Pclass' ).mean().to_dict().get('Age')
 data.loc[data['Age'].isnull() , 'Age'] = data.loc[data['Age'].isnull() , 'Pclass'].apply(lambda x: round(dict_mean_age_by_pclass.get(x)) )<count_missing_values>
train_data['Transmission'].value_counts()
print(data.isnull().sum())
<feature_engineering>
train_data['Transmission'].fillna(value='Trans_unknown' , inplace =True)
 test_data['Transmission'].fillna(value='Trans_unknown' , inplace =True )
data['FamilySize'] = data['Parch'] + data['SibSp'] + 1<feature_engineering>
train_data['WheelTypeID'].value_counts()
data['IsAlone'] = 0
data.loc[data['FamilySize'] == 1, 'IsAlone'] = 1<count_missing_values>
train_data.drop('WheelTypeID' , axis=1 , inplace=True)
 test_data.drop('WheelTypeID' , axis=1 , inplace=True )
data.isnull().sum()<count_missing_values>
train_data['WheelType'].value_counts()
data.isnull().sum()<feature_engineering>
train_data['WheelType'].fillna(value='WT_unknown' , inplace =True)
 test_data['WheelType'].fillna(value='WT_unknown' , inplace =True )
data['Title'] = data['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]<count_values>
train_data['Nationality'].value_counts()
data['Title'].value_counts()<count_values>
train_data['Nationality'].fillna(value='Nationality_unknown' , inplace =True)
 test_data['Nationality'].fillna(value='Nationality_unknown' , inplace =True )
srs_filter_title = data['Title'].value_counts() > round(len(data.index)/100 )<feature_engineering>
train_data['Size'].value_counts()
data['Title'] = data['Title'].apply(lambda x: 'Misc' if srs_filter_title[x]==False else x )<count_values>
train_data['Size'].fillna(value='size_unknown' , inplace =True)
 test_data['Size'].fillna(value='size_unknown' , inplace =True )
data['Title'].value_counts()<drop_column>
train_data['TopThreeAmericanName'].value_counts()
list_clm_to_drop = ['Name', 'PassengerId', 'Ticket', 'Cabin', 'Embarked']
data.drop(list_clm_to_drop, axis=1, inplace=True )<categorify>
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )
data = pd.get_dummies(data )<count_missing_values>
train_data['PRIMEUNIT'].value_counts()
print(data.isnull().sum() )<split>
train_data['PRIMEUNIT'].fillna(value='unk',inplace=True)
 test_data['PRIMEUNIT'].fillna(value='unk',inplace=True )
train = data.loc[data['Survived'].isnull() ==False, :]
test = data[-len(test):]<count_missing_values>
train_data['AUCGUART'].value_counts()
train.isnull().sum()<count_missing_values>
train_data['AUCGUART'].fillna(value='AUC_unk',inplace=True)
 test_data['AUCGUART'].fillna(value='AUC_unk',inplace=True )
train.isnull().sum()<count_missing_values>
train_data.drop([ 'MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice', 'MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice', 'MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice', 'MMRCurrentRetailCleanPrice',] ,inplace=True , axis=1)
 test_data.drop([ 'MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice', 'MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice', 'MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice', 'MMRCurrentRetailCleanPrice',] ,inplace=True , axis=1 )
test.isnull().sum()<count_missing_values>
train_data.drop('PurchDate' ,inplace=True , axis=1)
 test_data.drop('PurchDate' ,inplace=True , axis=1 )
test.isnull().sum()<prepare_x_and_y>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!=object
y_train = train['Survived']
X_train = train.drop('Survived', axis = 1)
 X_test = test.drop('Survived', axis = 1 )<count_missing_values>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
X_test.isnull().sum()<import_modules>
for i in not_categorical :
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test
from sklearn import svm, tree, linear_model, neighbors, naive_bayes, ensemble, discriminant_analysis, gaussian_process
from xgboost import XGBClassifier
import lightgbm as lgb
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import feature_selection
from sklearn import model_selection
from sklearn import metrics 
<choose_model_class>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
list_mlas = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
gaussian_process.GaussianProcessClassifier() ,
linear_model.LogisticRegressionCV() ,
linear_model.PassiveAggressiveClassifier() ,
linear_model.RidgeClassifierCV() ,
linear_model.SGDClassifier() ,
linear_model.Perceptron() ,
naive_bayes.BernoulliNB() ,
naive_bayes.GaussianNB() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True),
svm.NuSVC(probability=True),
svm.LinearSVC() ,
tree.DecisionTreeClassifier() ,
tree.ExtraTreeClassifier() ,
discriminant_analysis.LinearDiscriminantAnalysis() ,
discriminant_analysis.QuadraticDiscriminantAnalysis() ,
XGBClassifier() ,
lgb.LGBMClassifier()
]<randomize_order>
train_data[categorical[0]]
obj_splitter_for_cv = model_selection.ShuffleSplit(n_splits = 10, test_size =.3, train_size =.6, random_state = 42 )<create_dataframe>
pd.get_dummies(train_data[categorical[0]] )
list_mla_columns = ['ml_name', 'parameters','train_accuracy_mean', 'test_accuracy_mean', 'test_accuracy_3sigma' ,'elapsed_time']
df_result_in_each_mla = pd.DataFrame(columns = list_mla_columns)
 df_result_prediction = pd.DataFrame(y_train, columns=['answer'] )<feature_engineering>
for i in categorical :
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,axis=1 ,inplace=True)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data , dummies],axis=1)
 test_data.drop(i,axis=1,inplace=True )
int_index_mla_list = 0
for alg in list_mlas:
str_ml_name = alg.__class__.__name__
df_result_in_each_mla.loc[int_index_mla_list, 'ml_name'] = str_ml_name
df_result_in_each_mla.loc[int_index_mla_list, 'parameters'] = str(alg.get_params())
 dict_cv_result = model_selection.cross_validate(alg, X_train, y_train, cv=obj_splitter_for_cv, return_train_score=True)
 df_result_in_each_mla.loc[int_index_mla_list, 'elapsed_time'] = dict_cv_result['fit_time'].mean()
df_result_in_each_mla.loc[int_index_mla_list, 'train_accuracy_mean'] = dict_cv_result['train_score'].mean()
df_result_in_each_mla.loc[int_index_mla_list, 'test_accuracy_mean'] = dict_cv_result['test_score'].mean()
df_result_in_each_mla.loc[int_index_mla_list, 'test_accuracy_3sigma'] = dict_cv_result['test_score'].std() *3
alg.fit(X_train, y_train)
 df_result_prediction[str_ml_name] = alg.predict(X_train)
 int_index_mla_list += 1 
<sort_values>
for i in train_data.drop('IsBadBuy',axis=1 ).columns :
if i not in test_data.columns :
test_data[i]=np.zeros(len(test_data))
df_result_in_each_mla.sort_values(by = ['test_accuracy_mean'], ascending = False, inplace = True)
 df_result_in_each_mla<split>
for i in test_data.columns :
if i not in train_data.columns :
train_data[i]=np.zeros(len(train_data))
X_train, X_valid, y_train, y_valid = train_test_split(X_train,
y_train,
test_size=0.3,
random_state=42,
stratify=y_train )<init_hyperparams>
test_data=test_data[train_data.drop('IsBadBuy',axis=1 ).columns]
scoring = 'f1'
dict_param_grid = {
'num_leaves' : [4, 9, 16, 25],
'max_depth' : [2, 3, 4, 5],
'colsample_bytree' : [0.5, 1.0],
'subsample': [0.2, 0.6, 1.0],
'learning_rate' : [0.01, 0.1]
}
obj_gridsearch_cv = linear_model.RidgeClassifierCV(alphas=[1e-3, 1e-2, 1e-1, 1])
 obj_gridsearch_cv.fit(X_train, y_train)
<compute_train_metric>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
y_train_pred = obj_gridsearch_cv.predict(X_train)
 y_test_pred = obj_gridsearch_cv.predict(X_test)
 ', obj_gridsearch_cv.best_params_)
 print('Scores')
 print('Close: {:.3f}'.format(obj_gridsearch_cv.score(X_train, y_train)))
 print('Open: {:.3f}'.format(obj_gridsearch_cv.score(X_valid, y_valid)) )<save_to_csv>
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )
sub = pd.DataFrame(pd.read_csv(".. /input/titanic/test.csv")['PassengerId'])
 sub['Survived'] = list(map(int, y_test_pred))
sub.to_csv("submission.csv", index=False )<set_options>
from sklearn.neighbors import KNeighborsClassifier
%matplotlib inline<define_variables>
from sklearn.neighbors import KNeighborsClassifier
def Extract(file,labels=False):
	print("Beginning the extraction process...")
 	with open(file,'r')as f:
		data = f.read()
		data = data.split('
')
 	
	Data = []
	label = []
	
	dataLen = len(data)
 	
	
	print("preparing data into {} samples.. ".format(dataLen))
	
	for i in range(1,dataLen):
		if data[i] != '':
			if labels == False:
				Data.append(np.array(data[i].split(','), dtype=np.float32))
			else:
				Data.append(np.array(data[i].split(',')[1:], dtype=np.float32))
				label.append(np.array(data[i].split(',')[0], dtype=np.int8))
			
	Data = np.array(Data)/255
	del data
	if labels == False:
		return Data
	else:
		return(Data,label )<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
( x_train,y_train),(x_test,y_test)= load_data()
data = Extract('/kaggle/input/digit-recognizer/test.csv')
(moreTrain,label)= Extract('/kaggle/input/digit-recognizer/train.csv',labels=True )<data_type_conversions>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
y_test = to_categorical(y_test)
 x_train = x_train.astype(np.float32)/ 255
x_test = x_test.astype(np.float32)/ 255
x_test = x_test.reshape(len(x_test),28,28,1)
 moreTrain /= 255
print(x_train.shape)
 print(y_train.shape)
 x_train = np.concatenate(( x_train,moreTrain.reshape(len(moreTrain),28,28)) ,axis=0)
 y_train = np.concatenate(( y_train,label),axis=0)
 x_train = x_train.reshape(len(x_train),28,28,1)
 y_train = to_categorical(y_train)
 print(x_train.shape)
 print(y_train.shape )<choose_model_class>
KNN.score(X_test,y_test )
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1), name='conv1'))
model.add(Conv2D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1), name='conv2'))
model.add(MaxPool2D(pool_size=(2, 2),name='pool1'))
model.add(Conv2D(filters=32, kernel_size=5, strides=1,padding='same', activation='relu',name='conv3'))
model.add(Conv2D(filters=32, kernel_size=3, strides=1,padding='same', activation='relu',name='conv4'))
model.add(MaxPool2D(pool_size=(2, 2),name='pool2'))
model.add(Conv2D(filters=64, kernel_size=3, strides=1,padding='same', activation='relu',name='conv5'))
model.add(Conv2D(filters=64, kernel_size=3, strides=1,padding='valid', activation='relu',name='conv6'))
model.add(Flatten(name='flat'))
model.add(Dense(512,activation='relu',name='dense1'))
model.add(Dropout(0.2,name='dropout'))
model.add(Dense(10,activation='softmax',name='res'))
model.summary()<train_model>
predict=KNN.predict(test_data.drop('RefId' , axis=1))
class Callback(ks.callbacks.Callback):
def on_epoch_end(self,epoch,logs={}):
if(logs['val_acc'] >= 0.999):
print("
Valication accuracy reached more than 99.9%, training stopped")
 self.model.stop_training=True<choose_model_class>
submission=pd.DataFrame(data=predict , columns=['IsBadBuy'])
 submission.head()
earlyStop = Callback()
adam = ks.optimizers.Adam(lr=1e-3)
 model.compile(loss='categorical_crossentropy',optimizer=adam, metrics=['accuracy'])
 checkpoint = ks.callbacks.ModelCheckpoint('model.h5',save_best_only=True, monitor='val_acc', mode='max' )<train_model>
submission['RefId']=test_data['RefId']
submission.set_index('RefId' , inplace=True )
history = model.fit(x_train, y_train, validation_data=(x_test,y_test),batch_size=1024, epochs=100, shuffle=True, verbose=1, callbacks=[checkpoint,earlyStop] )<compute_test_metric>
submission.to_csv('submission.csv' )