<jupyter_start><jupyter_text>Load Processed Vectorized Data<jupyter_code>data = pickle.load(open('data_face_features.pickle', mode='rb'))
data.keys()<jupyter_output><empty_output><jupyter_text>Splitting Data<jupyter_code>X = np.array(data['data'])
y = np.array(data['label'])
X.shape,y.shape
X = X.reshape(-1,128)
X.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.8, random_state=0)
X_train.shape,X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Training Models<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import classification_report, accuracy_score, f1_score<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>model_log = LogisticRegression()
model_log.fit(X_train,y_train)
y_pred_train = model_log.predict(X_train)
y_pred_test = model_log.predict(X_test)

#accuracy score
acc_train = accuracy_score(y_train,y_pred_train)
acc_test = accuracy_score(y_test,y_pred_test)

# f1-score 
f1_score_train = f1_score(y_train,y_pred_train,average='macro')
f1_score_test= f1_score(y_test,y_pred_test,average='macro')<jupyter_output><empty_output><jupyter_text>Logistic Regression Evaluation Metrics<jupyter_code>print(f'accuracy score for training set = {acc_train}')
print(f'accuracy score for test set = {acc_test}')
print(f'f1 score for training set = {f1_score_train}')
print(f'f1 score score for test set = {f1_score_test}')
#function to evaluate different trained models
def evaluation(model,x_train,y_train,x_test,y_test):
    y_pred_train = model.predict(X_train)
    y_pred_test = model.predict(X_test)

    #accuracy score
    acc_train = accuracy_score(y_train,y_pred_train)
    acc_test = accuracy_score(y_test,y_pred_test)

    # f1-score 
    f1_score_train = f1_score(y_train,y_pred_train,average='macro')
    f1_score_test= f1_score(y_test,y_pred_test,average='macro')
    print(f'accuracy score for training set = {acc_train}')
    print(f'accuracy score for test set = {acc_test}')
    print(f'f1 score for training set = {f1_score_train}')
    print(f'f1 score score for test set = {f1_score_test}')<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>model_svc = SVC(probability=True)
model_svc.fit(X_train,y_train)
evaluation(model_svc,X_train,y_train,X_test,y_test) #svc performs better than Log<jupyter_output>accuracy score for training set = 0.8319641523525019
accuracy score for test set = 0.7074626865671642
f1 score for training set = 0.8325899156931432
f1 score score for test set = 0.7150649550951717<jupyter_text>Random Forest<jupyter_code>model_rf = RandomForestClassifier()
model_rf.fit(X_train,y_train)
evaluation(model_rf,X_train,y_train,X_test,y_test) #overfit<jupyter_output>accuracy score for training set = 1.0
accuracy score for test set = 0.6626865671641791
f1 score for training set = 1.0
f1 score score for test set = 0.6598291568556367<jupyter_text>Voting Classifier<jupyter_code>model_voting = VotingClassifier(estimators=[
    ('log',LogisticRegression()),
    ('svm',SVC(probability=True)),
    ('rf',RandomForestClassifier())
], voting='soft', weights=[2,3,1])
model_voting.fit(X_train,y_train)
evaluation(model_voting,X_train,y_train,X_test,y_test)
from sklearn.model_selection import GridSearchCV
model_grid = GridSearchCV(model_voting,
                          param_grid={
                              'svm__C':[3,5,7,10],
                              'svm__gamma':[0.1,0.3,0.5],
                              'rf__n_estimators':[5,10,20],
                              'rf__max_depth':[3,5,7],
                              'voting':['soft','hard']
                              
                          },scoring='accuracy',cv=3,n_jobs=1,verbose=2)
model_grid.fit(X_train,y_train)
model_grid.best_params_
#model_grid.best_estimator_
model_grid.best_score_
model_best_estimator = model_grid.best_estimator_<jupyter_output><empty_output><jupyter_text>Save Model<jupyter_code>pickle.dump(model_best_estimator,open('models/machinelearning_face_person_identity2.pkl',mode='wb'))<jupyter_output><empty_output>