简体   繁体   中英

Why is there an error while printing the accuracy score?

For testing the accuracy of my trained model I am using the accuracy_score function but it is not working.

from sklearn.metrics import accuracy_score
y_test = pd.read_csv('Test.csv')
labels = y_test["ClassId"].values
imgs = y_test["Path"].values
data=[]
for img in imgs:
   image = Image.open(img)
   image = image.resize((30,30))
   data.append(np.array(image))
X_test=np.array(data)
pred = model.predict(X_test)
classes_x=np.argmax(X_test,axis=1)
#Accuracy with the test data
from sklearn.metrics import accuracy_score
print(accuracy_score(labels, pred))

ERROR: This is what it shows

It seems like the problem has something to do with the format you use to represent the output of the model. I will assume that you are using One hot coding, so you do:

pred = model.predict(X_test)
classes_x=np.argmax(X_test,axis=1)

On np.argmax should go axis=-1:

predictions = np.argmax(model.predict(X_test), axis=-1)

At the end, on accuracy function you're sending pred, no classes_x.

print(accuracy_score(labels, pred))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM