Exemple #1
0
def test_pred(md_clf):
    unseen = mpimg.imread("unseen_dig.png")
    unseen = interpol_im(unseen, plot_new_im=True)
    unseen = rescale_pixel(unseen)
    result = md_clf.predict(unseen.reshape(1, -1))
    print("The prediction: ", result[0])
Exemple #2
0
from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred

################################################

phys_dict = {0: 'Bohr', 1: 'Einstein'}
bohr = 10
flat_bohr = []
ein = 11
flat_ein = []
y = []

# first load the images and flatten them and store the flattened image objects into a list with length=21,
# then concatenate them into a 21x2700 array
for i in range(bohr):
    im = mpimg.imread('bohr' + str(i) + '.jpeg')
    flat_bohr.append(interpol_im(im, dim1=45, dim2=60))
    y.append(0)
for i in range(ein):
    im = mpimg.imread('ein' + str(i) + '.jpeg')
    flat_ein.append(interpol_im(im, dim1=45, dim2=60))
    y.append(1)

# concatenate into a 21x2700 array
X = np.vstack((np.array(flat_bohr), np.array(flat_ein)))

###################################### Training ########################################

### instantiating PCA and CLF
md_pca, X_proj = pca_X(X)
md_clf = svm_train(X_proj, y)
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn import preprocessing
from sklearn import svm


bohr = 10
ein = 11
flat_bohr = []
flat_ein = []
y = []
phys_dict = {0: "Bohr", 1: "Einstein"}

for i in range(bohr):
    im = mpimg.imread("bohr" + str(i) + ".jpeg")
    flat_bohr.append(interpol_im(im, dim1=45, dim2=60, plot_new_im=False, cmap="binary", axis_off=True))
    y.append(0)
for i in range(ein):
    im = mpimg.imread("ein" + str(i) + ".jpeg")
    flat_ein.append(interpol_im(im, dim1=45, dim2=60, plot_new_im=False, cmap="binary", axis_off=True))
    y.append(1)

X = np.vstack((np.array(flat_bohr), np.array(flat_ein)))

md_pca, X_proj = pca_X(X)

perc = 0
tot = 21

for select_idx in range(tot):
    #     set_trace()
Exemple #4
0
from sklearn import preprocessing

import warnings
warnings.filterwarnings("ignore")

from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred

################################################

dig_data = load_digits()
X = dig_data.data
y = dig_data.target
index = 15
dig_img = dig_data.images
unseen = mpimg.imread('unseen_dig.png')
flatten_unseen = interpol_im(unseen, plot_new_im = False)
rescaled_unseen = rescale_pixel(X, flatten_unseen, index, plot_ref = True, plot_unseen = True)

print('Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively'.format(index, np.mean(rescaled_unseen), np.mean(X[15])))
print('The rescaled unseen has all the pixel values as integers, flatten array shown as following:')
print(rescaled_unseen)

###################### Trianing ##########################

dig_data = load_digits()
X = dig_data.data
y = dig_data.target

md_clf = svm_train(X[0:60], y[0:60], gamma = 0.001, C = 100)

####################### Validation #########################
Exemple #5
0
import warnings

warnings.filterwarnings("ignore")

from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred

################################################

dig_data = load_digits()
X = dig_data.data
y = dig_data.target
index = 15
dig_img = dig_data.images
unseen = mpimg.imread('unseen_dig.png')
flatten_unseen = interpol_im(unseen, plot_new_im=False)
rescaled_unseen = rescale_pixel(X,
                                flatten_unseen,
                                index,
                                plot_ref=True,
                                plot_unseen=True)

print(
    'Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively'
    .format(index, np.mean(rescaled_unseen), np.mean(X[15])))
print(
    'The rescaled unseen has all the pixel values as integers, flatten array shown as following:'
)
print(rescaled_unseen)

###################### Trianing ##########################
tot_end = 80
for i in range(tot_start, tot_end):
    ans = y[i]
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    if ans != pre:
        mis += 1
        plt.imshow(X[i].reshape((8, 8)), cmap='binary')
        plt.show()
        print("--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}".format(i, ans, pre))
    if ans == pre:
        perc += 1.
        
percentage = (perc/(tot_end-tot_start))*100.
print("Total number of mis-identifications: {:d}".format(mis))
print("Success rate: {:f}".format(percentage))

############################################################################
############################## unseen_dig.png ##############################
############################################################################

unseen = mpimg.imread('unseen_dig.png')
unseen_flat = interpol_im(unseen, plot_new_im=True)

plt.imshow(X[15].reshape((8, 8)), cmap='binary')
plt.show()

unseen_scaled = rescale_pixel(X, unseen_flat, ind=15)

unseen_pre = md_clf.predict(unseen_flat.reshape(1, -1))
unseen_scaled_pre = md_clf.predict(unseen_scaled.reshape(1, -1))
print("Predictions for unscaled and scaled unseen image: {:d}, {:d}".format(unseen_pre[0], unseen_scaled_pre[0]))
# applying md_clf for the next 20 images
for i in range(tot_start, tot_end):
    ans = y[i]
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    if ans != pre:
        mis += 1
        plt.imshow(X[i].reshape((8, 8)), cmap='binary')
        plt.show()
        print(
            "--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}".
            format(i, ans, pre))
    if ans == pre:
        perc += 1.

percentage = (perc / (tot_end - tot_start))
print("Total number of mis-identifications: {:d}".format(mis))
print("Success rate: {:1.2f}".format(percentage))
# Testing the unseen_dig
unseen = mpimg.imread('unseen_dig.png')
unseen_flat = interpol_im(unseen, plot_new_im=True)
# reshaping the image
plt.imshow(X[15].reshape((8, 8)), cmap='binary')
plt.show()

unseen_scaled = rescale_pixel(X, unseen_flat, ind=15)
# predictions for scaled and unscaled images
unseen_pre = md_clf.predict(unseen_flat.reshape(1, -1))
unseen_scaled_pre = md_clf.predict(unseen_scaled.reshape(1, -1))
print("Predictions for unscaled and scaled unseen image: {:d}, {:d}".format(
    unseen_pre[0], unseen_scaled_pre[0]))
from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred

################################################

phys_dict = {0: 'Bohr', 1: 'Einstein'}
bohr = 10
flat_bohr = []
ein = 11
flat_ein = []
y = []

# first load the images and flatten them and store the flattened image objects into a list with length=21, 
# then concatenate them into a 21x2700 array
for i in range(bohr):
    im = mpimg.imread('bohr'+str(i)+'.jpeg')
    flat_bohr.append(interpol_im(im, dim1 = 45, dim2 = 60))
    y.append(0)
for i in range(ein):
    im = mpimg.imread('ein'+str(i)+'.jpeg')
    flat_ein.append(interpol_im(im, dim1 = 45, dim2 = 60))
    y.append(1)

# concatenate into a 21x2700 array
X = np.vstack((np.array(flat_bohr), np.array(flat_ein)))

###################################### Training ########################################
 
### instantiating PCA and CLF
md_pca, X_proj = pca_X(X)
md_clf = svm_train(X_proj, y)