2020-11-29

machine learning even or odd, with python (scikit-learn)

I'm trying to modify the scikit learn Recognizing hand-written digits so whereafter it's predicted what the image is, to then see if the digit is even or odd.

In line 30-38 I separate the predicted into its even and odd components, I do the same for y_test. But my problem, it's the coder who is filtering to what is even and odd, I'm trying to make the machine lifter to see if the number is even or odd. I might be going about this the wrong way, maybe I am thinking it about it correctly, but not sure. After that I want to create a confusion matrix like I have below line 38.

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from sklearn import datasets, svm, metrics
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np


digits = datasets.load_digits() # from skleanr tutorial page
n_samples = len(digits.images)
imshape = digits.images[0].shape # to remember what an image is...
data = digits.images.reshape((n_samples, -1))
targ = digits.target

# choose your classifier
classifier = svm.SVC( )

# split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(data, targ, test_size=0.5, shuffle=False)
n_train = len(y_train)
n_test = len(y_test)

# do it!
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)

#print(predicted)
predicted_even = (predicted[predicted%2==0])
print(len(predicted_even))
predicted_odd = (predicted[predicted%2!=0])
print(len(predicted_odd))

y_even = (y_test[y_test%2==0])
print(len(y_even))
y_odd = (y_test[y_test%2!=0])
print(len(y_odd))


data1 = {'y_test': y_test, 'predicted': predicted}
df = pd.DataFrame(data1,columns=["y_test","predicted"])
cfm = pd.crosstab(df['y_test'],df['predicted'],colnames=["True"],rownames=["Predicted"])
print(cfm)


print('number in test set:',len(y_test))
right=np.sum(y_test==predicted)
print('number correctly classified:',right)

msk = y_test!=predicted
X_fails,y_fails,y_failspred = X_test[msk],y_test[msk],predicted[msk]


# here on in, we're plotting...
_, axes = plt.subplots(4, 4)
plt.subplots_adjust(hspace=1.0)
Xy = list(zip(X_train, y_train))
for ax, (X,y) in zip(axes[:2,:].flatten()[:], Xy[:8]):
    ax.set_axis_off()
    ax.imshow(X.reshape(imshape), cmap=plt.cm.gray_r, interpolation='nearest')
    ax.set_title(f'Training: {y}')

plot_the_failures=False
#plot_the_failures=True

if plot_the_failures:
    print('plotting the bad predictions')
    # this will probably choke if there are fewer than 8 fails.
    Xyy = list(zip(X_fails, y_fails, y_failspred))
else:
    Xyy = list(zip(X_test,y_test,predicted))
for ax, (X,yt,yp) in zip(axes[2:, :].flatten()[:], Xyy[:8]):
    ax.set_axis_off()
    Xim = X.reshape(imshape)
    ax.imshow(Xim, cmap=plt.cm.gray_r, interpolation='nearest')
    ax.set_title(f'True, Predict:\n{yt}, {yp}')

ofil='tmp.png'
print('saving to file',ofil)
plt.savefig(ofil)



from Recent Questions - Stack Overflow https://ift.tt/3mo8v7e
https://ift.tt/eA8V8J

No comments:

Post a Comment