Kaggle competition: Plant Seedlings Classification
testing_set[columns_impute_test[21]].value_counts()<count_missing_values>
import tensorflow as tf
from tensorflow import keras
from tensorflow import one_hot
from tensorflow.keras.applications import xception
from tensorflow.keras.preprocessing import image
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from IPython.display import clear_output
import gc
testing_set[testing_set[columns_impute_test[21]].isnull() ]["BldgType"].value_counts()<feature_engineering>
CATEGORIES = os.listdir("/kaggle/input/plant-seedlings-classification/train")
 CATEGORIES
testing_set.loc[(testing_set[columns_impute_test[21]].isnull())&(testing_set["BldgType"] == "TwnhsE"),"GarageFinish"] = "Fin"
testing_set[columns_impute_test[21]].fillna(value="Unf",inplace=True )<count_values>
pretrained = xception.Xception(input_shape=[240,240, 3], include_top=False )
testing_set[columns_impute_test[22]].value_counts()<filter>
pretrained.trainable = False
testing_set[testing_set[columns_impute_test[22]].isnull() ]<count_values>
model = tf.keras.Sequential([
pretrained,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(100, activation="relu"),
tf.keras.layers.BatchNormalization(trainable = True,axis=1),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(50, activation="relu"),
tf.keras.layers.BatchNormalization(trainable = True,axis=1),
tf.keras.layers.Dense(12,activation='softmax')
 ] )
testing_set[testing_set["SaleCondition"]=="Alloca"][columns_impute_test[22]].value_counts()<data_type_conversions>
model.compile(optimizer=tf.keras.optimizers.Nadam(learning_rate=0.001),
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'] )
testing_set[columns_impute_test[22]].fillna(value=2.0,inplace=True )<count_values>
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
brightness_range=[0.4,1],
rescale=1.0/255.0)
 test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0/255.0)
 pathh = "/kaggle/input/plant-seedlings-classification/train/"
testing_set[columns_impute_test[23]].value_counts()<filter>
result = model.fit(train_datagen.flow_from_directory(pathh,
target_size=(240, 240),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
shuffle=True),
epochs=20,
verbose=1 )
testing_set[testing_set[columns_impute_test[23]].isnull() ]<filter>
pretrained.trainable = True
model.get_layer('xception' ).trainable
testing_set[testing_set["SaleCondition"]=="Alloca"][columns_impute_test[23]].mean()<set_options>
model.compile(optimizer=tf.keras.optimizers.Nadam(learning_rate=0.0006),
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'] )
testing_set[columns_impute_test[23]].fillna(value=499.09,inplace=True )<count_values>
result = model.fit(train_datagen.flow_from_directory(pathh,
target_size=(240, 240),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
shuffle=True),
epochs=40,
initial_epoch=20,
verbose=1 )
testing_set[columns_impute_test[24]].value_counts()<rename_columns>
gc.collect()
testing_set[columns_impute_test[24]].fillna(value="TA",inplace=True )<count_values>
model.compile(optimizer=tf.keras.optimizers.Nadam(learning_rate=0.000006),
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'] )
testing_set[columns_impute_test[25]].value_counts()<rename_columns>
result = model.fit(train_datagen.flow_from_directory(pathh,
target_size=(240, 240),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
shuffle=True),
epochs=60,
initial_epoch=40,
verbose=1 )
testing_set[columns_impute_test[25]].fillna(value="TA",inplace=True )<count_values>
gc.collect()
testing_set[columns_impute_test[26]].value_counts()<filter>
valid = model.evaluate(test_datagen.flow_from_directory(pathh,
target_size=(240, 240),
color_mode='rgb',
class_mode='categorical',))
testing_set[testing_set[columns_impute_test[26]].isnull() ]<count_values>
a = test_datagen.flow_from_directory(pathh,
target_size=(240, 240),
color_mode='rgb',
class_mode='categorical',
batch_size=1 )
testing_set[testing_set["SaleCondition"]=="Normal"][columns_impute_test[26]].value_counts()<data_type_conversions>
CATEGORIES = list(a.class_indices.keys() )
testing_set[columns_impute_test[26]].fillna(value="WD",inplace=True )<filter>
R_categories = {y:x for x,y in a.class_indices.items() }
R_categories
testing_set.columns[(testing_set.isnull().sum() > 0)&(testing_set.isnull().sum() < 100)]<drop_column>
del valid
gc.collect()
testing_set.drop(columns=["Id"],inplace=True)
 testing_set.drop(columns=["Utilities"],inplace=True )<define_variables>
species = model.predict_classes(X )
col_for_dummies = ["MSZoning","Street","LotShape","LandContour","LotConfig","LandSlope","Neighborhood","Condition1","Condition2","BldgType",
"HouseStyle","RoofStyle",'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'ExterQual', 'ExterCond', 'Foundation',
'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'Heating', 'HeatingQC', 'CentralAir', 'Electrical',
'KitchenQual', 'Functional', 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond', 'PavedDrive', 'SaleType', 'SaleCondition']<categorify>
ans = pd.DataFrame(file,columns = ["file"])
 ans = ans.join(pd.DataFrame(species,columns=["species"]))
ans["species"] = ans["species"].apply(lambda x: CATEGORIES[int(x)])
 ans.head(20 )
testing_encoding_categorical = pd.get_dummies(testing_set, columns = col_for_dummies, drop_first = True )<categorify>
ans.to_csv("answers.csv",index=False )
<define_variables><EOS> 
model.save("saved_model" )