Kaggle competition: Don't Get Kicked!
for index, digit in test_df.iterrows() :
filepath = test_dir
filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename )<load_pretrained>
train_data = pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
tfms = get_transforms(do_flip=False)
 data = ImageDataBunch.from_folder(
 path = train_dir,
test = test_dir,
valid_pct = 0.2,
bs = 32,
size = 28,
ds_tfms = tfms,
num_workers = 0
).normalize(imagenet_stats)
 print(data)
 print(data.classes)
 data.show_batch(figsize=(5,5))<choose_model_class>
test_data = pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
learn = cnn_learner(data, models.resnet18, metrics=accuracy, model_dir="/tmp/models" )<train_model>
train_data.isnull().sum()
learn.fit_one_cycle(4 )<choose_model_class>
test_data.isnull().sum()
interp = ClassificationInterpretation.from_learner(learn )<train_model>
train_data['IsBadBuy'].value_counts()
learn.unfreeze()<train_model>
train_data['Model'].value_counts()
learn.fit_one_cycle(1 )<find_best_params>
train_data.drop('Model', axis = 1, inplace = True)
 test_data.drop('Model', axis = 1, inplace = True )
learn.lr_find()<train_model>
train_data['Trim'].value_counts()
learn.unfreeze()
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))<predict_on_test>
train_data.drop('Trim', axis = 1, inplace = True)
 test_data.drop('Trim', axis = 1, inplace = True )
class_score, y = learn.get_preds(DatasetType.Test)
 class_score = np.argmax(class_score, axis=1 )<load_from_csv>
train_data['SubModel'].value_counts()
sample_submission = pd.read_csv(input_dir/"sample_submission.csv")
 display(sample_submission.head(2))
display(sample_submission.tail(2))<define_variables>
train_data.drop('SubModel', axis = 1, inplace = True)
 test_data.drop('SubModel', axis = 1, inplace = True )
ImageId = []
for path in os.listdir(test_dir):
path = path[:-4]
path = int(path)
 path = path + 1
ImageId.append(path )<save_to_csv>
train_data['Color'].value_counts()
submission = pd.DataFrame({
"ImageId": ImageId,
"Label": class_score
})
 submission.sort_values(by=["ImageId"], inplace = True)
 submission.to_csv("submission.csv", index=False)
 submission[:10]<choose_model_class>
test_data['Color'].value_counts()
learn = cnn_learner(data, models.resnet34, metrics=accuracy, model_dir="/tmp/models" )<find_best_params>
train_data['Color'].fillna(value = 'Color_Unknown', inplace = True)
 test_data['Color'].fillna(value = 'Color_Unknown', inplace = True )
learn.lr_find()<train_model>
print('Number of null values in Color column of train data is ' + str(train_data['Color'].isnull().sum()))
print('Number of null values in Color column of test data is ' + str(test_data['Color'].isnull().sum()))
learn.fit_one_cycle(5 )<save_model>
train_data['Transmission'].value_counts()
learn.save('stage1' )<load_pretrained>
test_data['Transmission'].value_counts()
learn.load('stage1' )<find_best_params>
train_data[train_data['Transmission'] == 'Manual']
learn.lr_find()<train_model>
train_data['Transmission'].replace('Manual', 'MANUAL', inplace = True )
learn.unfreeze()
learn.fit_one_cycle(10, max_lr=slice(1e-6,1e-4))<find_best_params>
train_data['Transmission'].value_counts()
learn.lr_find()<train_model>
train_data['Transmission'].fillna(value = 'Transmission_Unknown', inplace = True)
 test_data['Transmission'].fillna(value = 'Transmission_Unknown', inplace = True )
learn.unfreeze()
learn.fit_one_cycle(6 )<install_modules>
train_data['WheelTypeID'].value_counts()
!pip install tensorflow-gpu==2.0.0b1<load_from_csv>
train_data.drop('WheelTypeID', axis = 1, inplace = True)
 test_data.drop('WheelTypeID', axis = 1, inplace = True )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<feature_engineering>
train_data['WheelType'].value_counts()
X_train = X_train / 255.0
test = test / 255.0<normalization>
test_data['WheelType'].value_counts()
datagen = keras.preprocessing.image.ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )<choose_model_class>
train_data['WheelType'].fillna(value = 'WheelType_Unknown', inplace = True)
 test_data['WheelType'].fillna(value = 'WheelType_Unknown', inplace = True )
model = keras.Sequential([
keras.layers.Conv2D(32,(3,3), activation ='relu', input_shape=(28, 28, 1)) ,
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(32,(3,3), activation ='relu'),
keras.layers.MaxPool2D(( 2,2)) ,
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(64,(3,3), activation ='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(64,(3,3), activation ='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(( 2,2)) ,
keras.layers.BatchNormalization() ,
keras.layers.Flatten() ,
keras.layers.Dense(1024, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>
train_data['WheelType'].value_counts()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )<train_model>
train_data['Nationality'].value_counts()
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=1000), steps_per_epoch=300, epochs=50-5 )<train_model>
test_data['Nationality'].value_counts()
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=1000), steps_per_epoch=300, epochs=5 )<predict_on_test>
train_data['Nationality'].fillna(value = 'Nationality_Unknown', inplace = True)
 test_data['Nationality'].fillna(value = 'Nationality_Unknown', inplace = True )
predictions = model.predict(test )<save_to_csv>
train_data['Size'].value_counts()
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<define_variables>
test_data['Size'].value_counts()
np.random.seed(2)
 random_seed = 2 
<load_from_csv>
train_data['Size'].fillna(value = 'Size_Unknown', inplace = True)
 test_data['Size'].fillna(value = 'Size_Unknown', inplace = True )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(train.values.shape)
 print(test.values.shape )<categorify>
train_data['TopThreeAmericanName'].value_counts()
y_train = train['label']
y_train = to_categorical(y_train, num_classes = 10)
 X_train = train.drop('label', axis=1)
 del train
print(X_train.values.shape)
 print(y_train.shape )<feature_engineering>
test_data['TopThreeAmericanName'].value_counts()
X_train = X_train / 255.0
test = test / 255.0<split>
train_data['TopThreeAmericanName'].fillna(value = 'TopThreeAmericanName_Unknown', inplace = True)
 test_data['TopThreeAmericanName'].fillna(value = 'TopThreeAmericanName_Unknown', inplace = True )
train_size = int (.9 * X_train.shape[0])
 X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size =.1, random_state=random_seed)
 print(X_train.shape)
 print(X_val.shape )<init_hyperparams>
train_data['PRIMEUNIT'].value_counts()
datagen = keras.preprocessing.image.ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
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 )<choose_model_class>
test_data['PRIMEUNIT'].value_counts()
model = keras.models.Sequential([
keras.layers.InputLayer(input_shape=[28,28,1]),
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Dropout (.25),
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2]),
keras.layers.Dropout (.25),
keras.layers.Conv2D(filters = 128, kernel_size =(3,3), padding = 'Same', activation ='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout(0.25),
keras.layers.Flatten() ,
keras.layers.Dense(units=256, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout (.3),
keras.layers.Dense(units=10, activation='softmax'),
])
 optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
train_data['PRIMEUNIT'].fillna(value = 'PRIMEUNIT_Unknown', inplace = True)
 test_data['PRIMEUNIT'].fillna(value = 'PRIMEUNIT_Unknown', inplace = True )
LR_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=4, factor=.5, min_lr=.00001)
 EarlyStop_callback = keras.callbacks.EarlyStopping(patience=20 )<train_model>
train_data['AUCGUART'].value_counts()
batch_size = 128
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = 50, validation_data =(X_val,y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[LR_callback, EarlyStop_callback] )<compute_test_metric>
test_data['AUCGUART'].value_counts()
model.evaluate(X_val, y_val )<save_to_csv>
train_data['AUCGUART'].fillna(value = 'AUCGUART_Unknown', inplace = True)
 test_data['AUCGUART'].fillna(value = 'AUCGUART_Unknown', inplace = True )
y_pred = model.predict(test)
 y_pred = np.argmax(y_pred, axis = 1)
 results = pd.Series(y_pred,name="Label")
 Id = pd.Series(range(1,28001),name = "ImageId")
 submission = pd.concat([Id,results],axis = 1)
 submission.to_csv("MySubmission.csv",index=False )<import_modules>
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 )
Conv2D, SeparableConv1D, Add, BatchNormalization, Activation, GlobalAveragePooling2D, LeakyReLU, Flatten
Lambda, Multiply, LSTM, Bidirectional, PReLU, MaxPooling1D
%matplotlib inline
print(os.listdir(".. /input/"))<data_type_conversions>
train_data['PurchDate'].value_counts()
def date_time(x):
if x==1:
return 'Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
 if x==2:
return 'Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now())
 if x==3:
return 'Date now: %s' % datetime.datetime.now()
if x==4:
return 'Date today: %s' % datetime.date.today()<define_variables>
train_data.drop('PurchDate', axis = 1, inplace = True)
 test_data.drop('PurchDate', axis = 1, inplace = True )
input_directory = r".. /input/digit-recognizer/"
output_directory = r".. /output/"
training_dir = input_directory + "train_images"
testing_dir = input_directory + r"test_images"
if not os.path.exists(output_directory):
os.mkdir(output_directory)
 figure_directory = ".. /output/figures"
if not os.path.exists(figure_directory):
os.mkdir(figure_directory)
 model_output_directory = ".. /output/models/"
if not os.path.exists(model_output_directory):
os.mkdir(model_output_directory)
 file_name_pred_batch = figure_directory+r"/result"
file_name_pred_sample = figure_directory+r"/sample"<load_from_csv>
train_data.drop(['RefId', 'IsBadBuy'], axis = 1 ).dtypes != 'object'
train_df = pd.read_csv(input_directory + "train.csv")
 train_df.rename(index=str, columns={"label": "target"}, inplace=True)
 train_df.head()<load_from_csv>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
test_df = pd.read_csv(input_directory + "test.csv")
 test_df.rename(index=str, columns={"label": "target"}, inplace=True)
 test_df.head()<prepare_x_and_y>
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
def get_data(train_X=None, train_Y=None, test_X=None, batch_size=32):
print("Preprocessing and Generating Data Batches.......
 ")
 rescale = 1.0/255
train_batch_size = batch_size
validation_batch_size = batch_size*5
test_batch_size = batch_size*5
train_shuffle = True
val_shuffle = True
test_shuffle = False
train_datagen = ImageDataGenerator(
 horizontal_flip=False,
vertical_flip=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
rescale=rescale,
validation_split=0.25)
 train_generator = train_datagen.flow(
 x=train_X,
y=train_Y,
batch_size=batch_size,
shuffle=True,
sample_weight=None,
seed=42,
save_to_dir=None,
save_prefix='',
save_format='png',
subset='training')
 validation_generator = train_datagen.flow(
 x=train_X,
y=train_Y,
batch_size=validation_batch_size,
shuffle=False,
sample_weight=None,
seed=42,
save_to_dir=None,
save_prefix='',
save_format='png',
subset='validation')
 test_datagen = ImageDataGenerator(rescale=rescale)
 test_generator = test_datagen.flow(
 x=test_X,
y=None,
batch_size=test_batch_size,
shuffle=False,
sample_weight=None,
seed=42,
save_to_dir=None,
save_prefix='',
save_format='png')
 class_weights = get_weight(np.argmax(train_Y, axis=1))
steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 print("
Preprocessing and Data Batch Generation Completed.
 ")
 return train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps
def get_weight(y):
class_weight_current = cw.compute_class_weight('balanced', np.unique(y), y)
 return class_weight_current<choose_model_class>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
def get_model(model_name, input_shape=(96, 96, 3), num_class=2, weights='imagenet', dense_units=1024, internet=False):
inputs = Input(input_shape)
 if model_name == "Xception":
base_model = Xception(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50":
base_model = ResNet50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101":
base_model = keras.applications.resnet.ResNet101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152":
base_model = keras.applications.resnet.ResNet152(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet50V2":
base_model = resnet_v2.ResNet50V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet101V2":
base_model = resnet_v2.ResNet101V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNet152V2":
base_model = resnet_v2.ResNet152V2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt50":
base_model = resnext.ResNeXt50(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "ResNeXt101":
base_model = resnext.ResNeXt101(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionV3":
base_model = InceptionV3(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "InceptionResNetV2":
base_model = InceptionResNetV2(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "DenseNet201":
base_model = DenseNet201(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetMobile":
base_model = NASNetMobile(include_top=False, weights=weights, input_shape=input_shape)
 elif model_name == "NASNetLarge":
base_model = NASNetLarge(include_top=False, weights=weights, input_shape=input_shape)
 x = base_model.output
x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 x = GlobalAveragePooling2D()(x)
 x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 x = Dense(dense_units )(x)
 x = BatchNormalization()(x)
 x = Activation(activation='relu' )(x)
 x = Dropout(0.5 )(x)
 if num_class>1:
outputs = Dense(num_class, activation="softmax" )(x)
 else:
outputs = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=base_model.input, outputs=outputs)
 model.summary()
return model
def get_conv_model(num_class=2, input_shape=None, dense_units=256):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'Same', activation ='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'Same', activation ='relu', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(dense_units, activation = "relu"))
model.add(Dropout(0.5))
if num_class>1:
model.add(Dense(num_class, activation='softmax'))
else:
model.add(Dense(num_class, activation='sigmoid'))
print(model.summary())
 return model 
<choose_model_class>
train_data[categorical[0]]
print("Settting Callbacks")
 def step_decay(epoch, lr):
lrate = lr
if epoch==2:
lrate = 0.0001
return lrate
checkpoint = ModelCheckpoint(
 model_file,
monitor='val_acc',
save_best_only=True)
 early_stopping = EarlyStopping(
 monitor='val_loss',
patience=10,
verbose=1,
restore_best_weights=True)
 reduce_lr = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.6,
patience=2,
min_lr=0.0000001,
verbose=1)
 learning_rate_scheduler = LearningRateScheduler(step_decay, verbose=1)
 callbacks = [reduce_lr, early_stopping]
print("Set Callbacks at ", date_time(1))<choose_model_class>
pd.get_dummies(train_data[categorical[0]] )
print("Getting Base Model", date_time(1))
dim = 28
input_shape =(dim, dim, 1)
 num_class = len(set(train_df["target"].values))
weights = 'imagenet'
dense_units = 256
model = get_conv_model(num_class=num_class, input_shape=input_shape, dense_units=dense_units)
 print("Loaded Base Model", date_time(1))<define_variables>
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,inplace=True,axis=1)
 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,inplace=True,axis=1 )
loss = 'categorical_crossentropy'
metrics = ['accuracy'] 
<prepare_x_and_y>
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))
train_X = train_df.drop(columns=["target"] ).values
train_X = train_X.reshape(train_X.shape[0], dim, dim,1)
 train_Y = train_df["target"].values
train_Y = keras.utils.to_categorical(train_Y, 10)
 test_X = test_df.values
test_X = test_X.reshape(test_X.shape[0], dim, dim,1 )<define_variables>
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
batch_size = 128
train_generator, validation_generator, test_generator, class_weights, steps_per_epoch, validation_steps = get_data(train_X=train_X, train_Y=train_Y, test_X=test_X, batch_size=batch_size)
<choose_model_class>
test_data = test_data[train_data.drop("IsBadBuy",axis=1 ).columns]
print("Starting Trainning...
 ")
 start_time = time.time()
print(date_time(1))
print("
Compliling Model...
 ")
 learning_rate = 0.001
optimizer = Adam(learning_rate)
 model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
 steps_per_epoch = len(train_generator)
 validation_steps = len(validation_generator)
 verbose = 1
epochs = 100
print("Trainning Model...
 ")
 history = model.fit_generator(
 train_generator,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_data=validation_generator,
validation_steps=validation_steps,
class_weight=class_weights)
 elapsed_time = time.time() - start_time
elapsed_time = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
print("
Elapsed Time: " + elapsed_time)
 print("Completed Model Trainning", date_time(1))<predict_on_test>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
ypreds = model.predict_generator(generator=test_generator, steps = len(test_generator), verbose=1)
<prepare_output>
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )
ypred = np.argmax(ypreds, axis=1 )<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
sample_df = pd.read_csv(input_directory + "sample_submission.csv")
 sample_df.head()<define_variables>
from sklearn.neighbors import KNeighborsClassifier
test_gen_id = test_generator.index_array
sample_submission_id = sample_df["ImageId"]
len(test_gen_id), len(sample_submission_id )<save_to_csv>
from sklearn.neighbors import KNeighborsClassifier
sample_list = list(sample_df.ImageId)
 ids = [x+1 for x in test_generator.index_array]
pred_dict = dict(( key, value)for(key, value)in zip(ids, ypred))
pred_list_new = [pred_dict[f] for f in sample_list]
test_df = pd.DataFrame({'ImageId': sample_list,'Label': pred_list_new})
 test_df.to_csv('submission.csv', header=True, index=False )<load_from_csv>
KNN = KNeighborsClassifier(n_neighbors = 11, n_jobs = -1)
 KNN.fit(X_train,y_train )
os.chdir('/kaggle/input/house-prices-advanced-regression-techniques')
 raw_data = pd.read_csv('train.csv')
 test_data = pd.read_csv('test.csv')
 raw_data.head()<define_variables>
KNN.score(X_test,y_test )
print(f"Amount of columns = {len(list(raw_data)) }
Amount of rows = {len(raw_data)}
So our dataset has dimensions 1460x81" )<prepare_x_and_y>
predict=KNN.predict(test_data.drop('RefId',axis=1))
target = raw_data['SalePrice']
test_Id = test_data['Id']
raw_len, test_len = len(raw_data), len(test_data)
 raw_data = raw_data.drop(['SalePrice'], axis=1)
 raw_data = pd.concat([raw_data, test_data], axis=0)
 raw_data = raw_data.drop(['Id'], axis=1 )<count_missing_values>
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
messy_features = []
less_messy_features = []
for feature in list(raw_data):
nan_count = raw_data[feature].isna().sum()
if nan_count:
print(f"{feature} contains, {nan_count} NaN values")
 if nan_count > 0.40 * len(raw_data):
messy_features.append(feature)
 else:
less_messy_features.append(feature )<drop_column>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )
print(f"Here are the extremely messy feature we collected:
{messy_features}
Let's remove them for now." )<drop_column>
Submission.head()
Submission.to_csv('Submission.csv' )