Beispiel #1
0
def pred_on_pca():
    # load data
    raw_data = load_digits()
    X = raw_data.data
    y = raw_data.target

    # organize data
    X_train = X[:60]
    y_train = y[:60]

    X_valid = X[60:80]
    y_valid = y[60:80]

    # train
    pca, X_proj = pca_X(X_train, n_comp=30)
    md_clf_new = svm_train(X_proj, y_train)

    # validate
    file = "unseen_dig.png"
    prediction = pca_svm_pred(file, pca, md_clf_new)
    print("The prediction: ", prediction[0])
Beispiel #2
0
    ytrain = np.delete(y, i, axis = 0)
    md_pca, Xproj = prf.pca_X(Xtrain, n_comp = 50)
    XtestProj = md_pca.transform(Xtest.reshape(1,-1))
    md_clf = prf.svm_train(Xproj, ytrain)
    predic = md_clf.predict(XtestProj)
    if(predic[0] != ytest):
         Errors +=1


print("success rate: ", (1 - (Errors/X.shape[0])) * 100, "%")

# START OF PART C
#NOTE MUST CHANGE PICTURE DIRECTORY TO MATCH YOUR INDIVIDUAL DIRECTORY
GuessPicture = "../Pictures/whoswho.JPG"
faces = prf.get_faces(GuessPicture)
predictions = prf.pca_svm_pred(faces, md_pca, md_clf)
print("PCA+SVM predition for person 1: {}".format(name_dic[predictions[0]]))
print("PCA+SVM predition for person 2: {}".format(name_dic[predictions[1]]))
print("PCA+SVM predition for person 3: {}".format(name_dic[predictions[2]]))

plt.imshow(cv2.imread(GuessPicture))
plt.axis("off")
plt.show()







Beispiel #3
0
    p = md_clf.predict(Xtest_proj)[0]

    if ytest == p:
        perc += 1.
        print('Pred: {} Ans: {}'.format(p, ytest))
    else:
        print('Pred: {} Ans: {} <--- incorrect; image index: {}'.format(
            p, ytest, select_idx))

print(
    'The percentage of correct predictions (for validation) is: {:1f}%'.format(
        perc * 100. / tot))

######################################## Testing #####################################

md_pca.fit(X)  # Re-fit the PCA axis onto X, the original data set

pre1 = phys_dict[pca_svm_pred('unseen_phys1.jpg',
                              md_pca,
                              md_clf,
                              dim1=45,
                              dim2=60)[0]]
print('PCA+SVM prediction for physicist 1: {}'.format(pre1))

pre2 = phys_dict[pca_svm_pred('unseen_phys2.jpg',
                              md_pca,
                              md_clf,
                              dim1=45,
                              dim2=60)[0]]
print('PCA+SVM prediction for physicist 2: {}'.format(pre2))
    #     set_trace()
    Xtrain = np.delete(X, select_idx, axis=0)
    ytrain = np.delete(y, select_idx)

    md_pca.fit(Xtrain)
    Xtrain_proj = md_pca.transform(Xtrain)

    Xtest = X[select_idx].reshape(1, -1)
    ytest = y[select_idx]
    Xtest_proj = md_pca.transform(Xtest)

    md_clf = svm_train(Xtrain_proj, ytrain)
    pre = md_clf.predict(Xtest_proj)
    if pre == ytest:
        perc += 1.0

perc /= tot / 100.0
print("Success rate: {:f}".format(perc))

unseen_phys1 = "unseen_phys1.jpg"
unseen_phys2 = "unseen_phys2.jpg"

pre1 = pca_svm_pred(unseen_phys1, md_pca, md_clf)
pre2 = pca_svm_pred(unseen_phys2, md_pca, md_clf)

pre_ein_statement = "PCA+SVM prediction for physicist 1: {:s}".format(phys_dict[pre1[0]])
pre_bohr_statement = "PCA+SVM prediction for physicist 2: {:s}".format(phys_dict[pre2[0]])

print(pre_ein_statement)
print(pre_bohr_statement)
Beispiel #5
0
    
    Xtest = X[select_idx].reshape(1, -1)
    ytest = y[select_idx]
    Xtest_proj = md_pca.transform(Xtest)

### if uses line55, the success rate will become about 81%
### because for every loop, it re-trains the svm with a 'less complete' data reference
#     md_clf = svm_train(Xtrain_proj, ytrain)

    p = md_clf.predict(Xtest_proj)[0]
    
    if ytest == p:
        perc += 1.
        print('Pred: {} Ans: {}'.format(p, ytest))
    else:
        print('Pred: {} Ans: {} <--- incorrect; image index: {}'.format(p, ytest, select_idx))
    

print('The percentage of correct predictions (for validation) is: {:1f}%'.format(perc*100./tot))

######################################## Testing #####################################

md_pca.fit(X)   # Re-fit the PCA axis onto X, the original data set

pre1 = phys_dict[pca_svm_pred('unseen_phys1.jpg', md_pca, md_clf, dim1 = 45, dim2 = 60)[0]]
print('PCA+SVM prediction for physicist 1: {}'.format(pre1))

pre2 = phys_dict[pca_svm_pred('unseen_phys2.jpg', md_pca, md_clf, dim1 = 45, dim2 = 60)[0]]
print('PCA+SVM prediction for physicist 2: {}'.format(pre2))