Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<data_type_conversions>
scale = 70
seed = 7
subscale = 299
mapping = {'male':1, 'female':0}
train['Sex'] = train['Sex'].replace(mapping ).astype(np.float64)
 test['Sex'] = test['Sex'].replace(mapping ).astype(np.float64 )<data_type_conversions>
subtrainingset = np.asarray(subtrainingset)
 subtraininglabels = pd.DataFrame(subtraininglabels )
test['Fare'].fillna(value = test['Fare'].mode() [0], inplace = True)
<define_variables>
labels = preprocessing.LabelEncoder()
labels.fit(traininglabels[0])
 print('Classes'+str(labels.classes_))
encodedlabels = labels.transform(traininglabels[0])
 clearalllabels = np_utils.to_categorical(encodedlabels)
 classes = clearalllabels.shape[1]
print(str(classes))
traininglabels[0].value_counts().plot(kind='pie' )
def detect_outliers(df,n,features):
outlier_indices = []
for col in features:
Q1 = np.percentile(df[col], 25)
 Q3 = np.percentile(df[col],75)
 IQR = Q3 - Q1
outlier_step = 1.5 * IQR
outlier_list_col = df[(df[col] < Q1 - outlier_step)|(df[col] > Q3 + outlier_step)].index
outlier_indices.extend(outlier_list_col)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(k for k, v in outlier_indices.items() if v > n)
 return multiple_outliers
Outliers_to_drop = detect_outliers(train,2,[["Pclass", "Sex", "Age", "Fare","Family_Size","Survived"]])
 train.loc[Outliers_to_drop]
train = train.drop(Outliers_to_drop, axis = 0 ).reset_index(drop=True )<prepare_x_and_y>
new_train = new_train/255
x_train,x_test,y_train,y_test = train_test_split(new_train,clearalllabels,test_size=0.1,random_state=seed,stratify=clearalllabels )
features = ["Pclass", "Sex", "Age", "Fare", "Family_Size"]
X_train = train[features]
y_train = train["Survived"]
X_test = test[features] 
<split>
generator = ImageDataGenerator(rotation_range = 180,zoom_range = 0.1,width_shift_range = 0.1,height_shift_range = 0.1,horizontal_flip = True,vertical_flip = True)
 generator.fit(x_train )
X_training, X_valid, y_training, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
<import_modules>
np.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(scale, scale, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary()
from sklearn.model_selection import cross_val_score
from sklearn.metrics import f1_score, make_scorer
from sklearn.ensemble import RandomForestClassifier 
<set_options>
y_pred = model.predict(x_test)
 y_class = np.argmax(y_pred, axis = 1)
 y_check = np.argmax(y_test, axis = 1)
 cmatrix = confusion_matrix(y_check, y_class)
 print(cmatrix )
scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro')
 warnings.filterwarnings('ignore', category = ConvergenceWarning)
 warnings.filterwarnings('ignore', category = DeprecationWarning)
 warnings.filterwarnings('ignore', category = UserWarning )<compute_train_metric>
new_sub_train = []
sets = []; getEx = True
for i in subtrainingset:
blurr = cv2.GaussianBlur(i,(5,5),0)
 hsv = cv2.cvtColor(blurr,cv2.COLOR_BGR2HSV)
 lower =(25,40,50)
 upper =(75,255,255)
 mask = cv2.inRange(hsv,lower,upper)
 struc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(21,21))
mask = cv2.morphologyEx(mask,cv2.MORPH_CLOSE,struc)
 boolean = mask>0
new = np.zeros_like(i,np.uint8)
 new[boolean] = i[boolean]
new_sub_train.append(new)
 if getEx:
plt.subplot(2,3,1);plt.imshow(i)
 plt.subplot(2,3,2);plt.imshow(blurr)
 plt.subplot(2,3,3);plt.imshow(hsv)
 plt.subplot(2,3,4);plt.imshow(mask)
 plt.subplot(2,3,5);plt.imshow(boolean)
 plt.subplot(2,3,6);plt.imshow(new)
 plt.show()
getEx = False
new_sub_train = np.asarray(new_sub_train)
 print(len(new_sub_train))
model_results = pd.DataFrame(columns = ['model', 'cv_mean', 'cv_std'])
 def cv_model(X_train, y_train, model, name, model_results = None, sort = True):
cv_scores = cross_val_score(model, X_train, y_train, cv = 10,
scoring = scorer, n_jobs = -1)
 print('Mean ', round(cv_scores.mean() , 5),'STd', round(cv_scores.std() , 5))
if model_results is not None:
model_results = model_results.append(pd.DataFrame({'model':name,
'cv_mean': cv_scores.mean() ,
'cv_std' : cv_scores.std() },
index = [0]), ignore_index = True)
 return model_results
model_results = cv_model(X_train, y_train, LinearSVC() , 'LSVC',
model_results)
 model_results = cv_model(X_train, y_train,
GaussianNB() , 'GNB', model_results)
 model_results = cv_model(X_train, y_train,
MLPClassifier(hidden_layer_sizes=(16, 32, 64, 64, 32)) ,
'MLP', model_results)
 model_results = cv_model(X_train, y_train,
LinearDiscriminantAnalysis() ,
'LDA', model_results)
 model_results = cv_model(X_train, y_train,
RidgeClassifierCV() , 'RIDGE', model_results)
 for n in [5, 10, 20]:
print('
KNN with {n} neighbors
', n)
 model_results = cv_model(X_train, y_train,
KNeighborsClassifier(n_neighbors = n),
n, model_results)
 model_results = cv_model(X_train, y_train,
ExtraTreesClassifier(n_estimators = 100, random_state = 10),
'EXT', model_results)
 model_results = cv_model(X_train, y_train,
RandomForestClassifier(100, random_state=10),
'RF', model_results)
 model_results = cv_model(X_train, y_train,
XGBClassifier(n_estimators= 100, random_state=10),
'XGB', model_results)
 model_results.set_index('model', inplace = True )<train_on_grid>
sublabels = preprocessing.LabelEncoder()
sublabels.fit(subtraininglabels[0])
 print('Classes'+str(sublabels.classes_))
subencodedlabels = sublabels.transform(subtraininglabels[0])
 clearalllabels = np_utils.to_categorical(subencodedlabels)
 subclasses = clearalllabels.shape[1]
print(str(subclasses))
subtraininglabels[0].value_counts().plot(kind='pie' )
xg_clf = XGBClassifier()
parameters_rf = {'n_estimators' : [200],'learning_rate': [0.1],
'max_depth': [4], "min_child_weight":[6], "gamma":[0], "subsample":[0.80]
}
grid_rf = GridSearchCV(xg_clf, parameters_rf, scoring=make_scorer(accuracy_score))
grid_rf.fit(X_training, y_training)
 xg_clf = grid_rf.best_estimator_
pred_xg = xg_clf.predict(X_valid)
 acc_xg = accuracy_score(y_valid, pred_xg)
 print("The Score for XGBoost is: " + str(acc_xg))
print("The best Score for XGBoost is: " + str(grid_rf.best_score_)) 
<train_on_grid>
new_sub_train = new_sub_train/255
x_train,x_test,y_train,y_test = train_test_split(new_sub_train,clearalllabels,test_size=0.2,random_state=seed,stratify=clearalllabels )
rf_clf = RandomForestClassifier()
parameters_rf = {"n_estimators": [4, 5, 6, 7, 8, 9, 10, 15], "criterion": ["gini", "entropy"], "max_features": ["auto", "sqrt", "log2"],
"max_depth": [2, 3, 5, 10], "min_samples_split": [2, 3, 5, 10]}
grid_rf = GridSearchCV(rf_clf, parameters_rf, scoring=make_scorer(accuracy_score))
grid_rf.fit(X_training, y_training)
 rf_clf = grid_rf.best_estimator_
rf_clf.fit(X_training, y_training)
 pred_rf = rf_clf.predict(X_valid)
 acc_rf = accuracy_score(y_valid, pred_rf)
 print("The Score for Random Forest is: " + str(acc_rf))
print("The best Score for Random Forest is: " + str(grid_rf.best_score_))<save_to_csv>
generator = ImageDataGenerator(rotation_range = 360,zoom_range = 0.3,width_shift_range = 0.1,height_shift_range = 0.5,horizontal_flip = True,vertical_flip = True)
 generator.fit(x_train )
submission_predictions = rf_clf.predict(X_test)
 submission = pd.DataFrame({"PassengerId": test["PassengerId"],
"Survived": submission_predictions})
 submission.to_csv("titanicprediction.csv", index=False)
 print(submission.shape )<set_options>
base_model = xception.Xception(weights='imagenet', include_top=False)
 x = base_model.output
x = GlobalAveragePooling2D()(x)
 x = Dense(1024, activation='relu' )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 predictions = Dense(2, activation='sigmoid' )(x)
 submodel = Model(inputs=base_model.input, outputs=predictions)
 for layer in base_model.layers:
layer.trainable = False
submodel.compile(optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), loss='binary_crossentropy', metrics = ['accuracy'])
 submodel.summary()
<load_from_csv><EOS> 
print(submodel.evaluate(x_train, y_train))
print(submodel.evaluate(x_test, y_test))