def test(self, X, y, verbose=True):
# if we don't need 3d inputs...
if type(self.model.input_shape) is tuple:
X = np.array(X)
if len(self.model.input_shape) == 2:
X = X.reshape((X.shape[0], -1))
else:
raise LanguageClassifierException('Mult-input models are not supported yet')
if verbose:
print("Getting predictions on the test set")
predictions = self.predict(X)
if len(predictions) != len(y):
raise LanguageClassifierException("Non comparable arrays")
if self.binary:
acc = (predictions == y).mean()
prec = np.sum(np.bitwise_and(predictions, y)) * 1.0 / np.sum(predictions)
recall = np.sum(np.bitwise_and(predictions, y)) * 1.0 / np.sum(y)
if verbose:
print("Test set accuracy of {0:.3f}%".format(acc * 100.0))
print("Test set error of {0:.3f}%".format((1 - acc) * 100.0))
print("Precision for class=1: {0:.3f}".format(prec))
print("Recall for class=1: {0:.3f}".format(recall))
return (acc, prec, recall)
else:
# TODO: Obtain more metrics for the multiclass problem
acc = (predictions == y).mean()
if verbose:
print("Test set accuracy of {0:.3f}%".format(acc * 100.0))
print("Test set error of {0:.3f}%".format((1 - acc) * 100.0))
return acc
评论列表
文章目录