Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<create_dataframe>
%matplotlib inline
Survived=a['Survived'].value_counts()
Survived=pd.DataFrame(Survived)
 Survived=Survived.reset_index()
pclass= a['Pclass'].value_counts()
pclass= pd.DataFrame(pclass)
 pclass= pclass.reset_index()
sex= a['Sex'].value_counts()
sex= pd.DataFrame(sex)
 sex= sex.reset_index()
embarked=a['Embarked'].value_counts()
embarked= pd.DataFrame(embarked)
 embarked= embarked.reset_index()
sibling=a['SibSp'].value_counts()
sibling= pd.DataFrame(sibling)
 sibling= sibling.reset_index()
parch=a['Parch'].value_counts()
parch= pd.DataFrame(parch)
 parch= parch.reset_index()
plt.style.use('seaborn')
 sns.set_style('whitegrid')
 fig = plt.figure(figsize=(15,15))
ax1 = plt.subplot2grid(( 3,2),(0,0))
plt.pie(Survived.Survived,colors=("pink","r"), autopct='%2.1f%%',labels=Survived['index'], shadow=True)
 plt.title('Titanic survival rates', fontsize=13, weight='bold')
 ax1 = plt.subplot2grid(( 3,2),(0,1))
plt.pie(pclass.Pclass,colors=("cyan","lime", 'orange'), autopct='%2.1f%%',labels=pclass['index'], shadow=True)
 plt.title('Passengers class 1,2 and 3 distributions', fontsize=13, weight='bold')
 ax1 = plt.subplot2grid(( 3,2),(1,0))
plt.pie(sex.Sex,colors=("darkcyan","purple"), autopct='%2.1f%%',labels=sex['index'], shadow=True)
 plt.title('Passengers sex', fontsize=13, weight='bold')
 ax1 = plt.subplot2grid(( 3,2),(1,1))
plt.pie(embarked.Embarked,colors=("deepskyblue","lawngreen", 'crimson'), autopct='%2.1f%%',labels=embarked['index'], shadow=True)
 plt.title('Passengers embarking port', fontsize=13, weight='bold')
 ax1 = plt.subplot2grid(( 3,2),(2,0))
plt.pie(sibling.SibSp,colors=("gold","sienna","plum","deepskyblue","lawngreen", 'crimson'), autopct='%2.1f%%',labels=sibling['index'], shadow=True)
 plt.title('Passengers siblings number', fontsize=13, weight='bold')
 ax1 = plt.subplot2grid(( 3,2),(2,1))
plt.pie(parch.Parch,colors=("gold","sienna","plum","deepskyblue","lawngreen", 'crimson', 'red'), autopct='%2.1f%%',labels=parch['index'], shadow=True)
 plt.title('Passengers parents/children number', fontsize=13, weight='bold')
 plt.show()<feature_engineering>
import tensorflow
from tensorflow.keras.preprocessing.image import load_img, img_to_array, array_to_img
from keras.preprocessing.image import ImageDataGenerator
c1['family_size']=c1['SibSp'] + c1['Parch'] + 1<count_missing_values>
Images, Labels = get_images('.. /input/train/' )
c1.isnull().sum()<drop_column>
labels = []
mapping = { 'Sugar beet': 0, 'Fat Hen': 1, 'Scentless Mayweed' : 2, 'Charlock' : 3,
'Small-flowered Cranesbill': 4, 'Maize': 5, 'Shepherds Purse' :6, 'Common wheat': 7,
'Common Chickweed': 8, 'Cleavers': 9, 'Loose Silky-bent' : 10, 'Black-grass': 11 }
for label in Labels:
labels.append(mapping[label])
 del Labels
c=c1.drop('Cabin', axis=1 )<data_type_conversions>
x_train, x_val, y_train, y_val = train_test_split(Images, Labels, test_size=.2, random_state=42, stratify = Labels )
c['Age'].fillna(28, inplace=True )<data_type_conversions>
y_train = np_utils.to_categorical(y_train,num_classes=12)
 y_val = np_utils.to_categorical(y_val,num_classes=12 )
c['Embarked'].fillna(method='ffill', inplace=True )<correct_missing_values>
train_datagen = ImageDataGenerator(
 rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2, 
)
 validation_datagen = ImageDataGenerator()
c['Fare'].fillna(method='ffill', inplace=True )<count_missing_values>
del Images
del Labels
print(c.isnull().sum() )<data_type_conversions>
train_generator = train_datagen.flow(x_train, y_train, batch_size=32)
 validation_generator = validation_datagen.flow(x_val, y_val, batch_size=16 )
c2=c[['Pclass','Sex','Age','Embarked','family_size','Parch','SibSp', 'Fare']]
c2['Pclass']=c2['Pclass'].astype(object )<categorify>
vgg = VGG16(include_top=
False, weights='imagenet', input_shape =(300,300,3))
c3=pd.get_dummies(c2)
 print("the shape of the original dataset",c2.shape)
 print("the shape of the encoded dataset",c3.shape)
 print("We have ",c3.shape[1]- c2.shape[1], 'new encoded features' )<split>
import tensorflow.keras.optimizers as Optimizer
from tensorflow.keras.layers import Flatten, Dense, Dropout, GlobalAvgPool2D, GlobalMaxPooling2D, Concatenate
from tensorflow.keras.models import Model
Train = c3[:na]
Test = c3[na:]<sort_values>
vgg.trainable=False
for layer in vgg.layers:
layer.trainable = False
fc1 = Concatenate(axis=-1 )([GlobalAvgPool2D()(vgg.output), GlobalMaxPooling2D()(vgg.output)])
 fc1 = Dense(400, activation='relu' )(fc1)
 fc1_dropout = Dropout(0.3 )(fc1)
 fc2 = Dense(200, activation='relu' )(fc1_dropout)
 fc2_dropout = Dropout(0.3 )(fc2)
 fc2 = Dense(75, activation='relu' )(fc1_dropout)
 output = Dense(12, activation='softmax' )(fc2_dropout)
 model = Model(vgg.input, output)
 model.compile(optimizer=Optimizer.Adam(lr=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])
 model.summary()
s=a[['Pclass','Fare','Survived']]
s.sort_values(by='Fare', ascending=False ).head(10 )<filter>
checkpoint = ModelCheckpoint('saved_model.hdf5', monitor='val_loss', verbose=1, save_weights_only=True, save_best_only=True, mode='min')
 callbacks_list = [checkpoint]
trained = model.fit_generator(train_generator,steps_per_epoch = 25, epochs=200, validation_data = validation_generator,
validation_steps=10,
verbose=1, callbacks = callbacks_list )
Train1=Train[Train['Fare'] < 200]<drop_column>
def get_test_images(directory):
Images = []
Image_names = []
for image_file in os.listdir(directory):
Image_names.append(image_file)
 image = cv2.imread(directory+r'/'+image_file)
 if image is not None:
image = cv2.resize(image,(300,300),)
 Images.append(image)
 return Images, Image_names
print('We dropped ',Train.shape[0] - Train1.shape[0],'fare outliers' )<sort_values>
vgg = VGG16(include_top=
False, weights='imagenet', input_shape =(300,300,3))
vgg.trainable=False
for layer in vgg.layers:
layer.trainable = False
fc1 = Concatenate(axis=-1 )([GlobalAvgPool2D()(vgg.output), GlobalMaxPooling2D()(vgg.output)])
 fc1 = Dense(400, activation='relu' )(fc1)
 fc1_dropout = Dropout(0.3 )(fc1)
 fc2 = Dense(200, activation='relu' )(fc1_dropout)
 fc2_dropout = Dropout(0.3 )(fc2)
 fc2 = Dense(75, activation='relu' )(fc1_dropout)
 output = Dense(12, activation='softmax' )(fc2_dropout)
 model = Model(vgg.input, output )
Train1['SibSp'].sort_values(ascending=False ).head(7 )<filter>
model.load_weights('saved_model.hdf5' )
train=Train1[Train1['SibSp'] <= 5]<drop_column>
tensorflow.keras.models.save_model(
 model,
'tf_model.hdf5',
overwrite=True,
include_optimizer=True
)
print('We dropped ', Train1.shape[0]- train.shape[0], 'sibSp outliers')
 print('And in total, we dropped ', Train.shape[0]-train.shape[0], 'outliers' )<prepare_x_and_y>
model = load_model('tf_model.hdf5' )
x=train
y=np.array(target )<split>
predictions = model.predict(test_images)
 predictions = np.argmax(predictions, axis = 1 )
x_train, x_test, y_train, y_test = train_test_split(x, y,test_size =.33, random_state=0 )<normalization>
labelled_predictions = []
mapping = {0: 'Sugar beet',1:'Fat Hen' ,2: 'Scentless Mayweed',3: 'Charlock',
4:'Small-flowered Cranesbill', 5:'Maize' ,
6: 'Shepherds Purse' ,7:'Common wheat' ,8:'Common Chickweed' ,
9:'Cleavers' ,10:'Loose Silky-bent' ,11: 'Black-grass'}
for pred in predictions:
labelled_predictions.append(mapping[pred] )
<import_modules><EOS> 
d = []
i=0
for pred in labelled_predictions:
d.append({'file': image_names[i], 'species': pred})
 i=i+1
output = pd.DataFrame(d)
 output.to_csv('submission.csv',index=False )