I thought of implementing kappaScore metrics using sklearn.metrics.cohen_kappa_score
def kappaScore(y_true,y_pred):
k = cohen_kappa_score(y_true,y_pred,weights='quadratic')
return k
Error I get when I try to run this code:
OperatorNotAllowedInGraphError: in user code:
/opt/conda/lib/python3.7/site-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/tmp/ipykernel_33/1006337667.py:2 kappaScore *
k = cohen_kappa_score(y_true,y_pred,weights='quadratic')
/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:555 inner_f *
return f(**kwargs)
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:600 cohen_kappa_score *
confusion = confusion_matrix(y1, y2, labels=labels,
/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:555 inner_f *
return f(**kwargs)
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:276 confusion_matrix *
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:81 _check_targets *
check_consistent_length(y_true, y_pred)
/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:253 check_consistent_length *
uniques = np.unique(lengths)
<__array_function__ internals>:6 unique **
/opt/conda/lib/python3.7/site-packages/numpy/lib/arraysetops.py:261 unique
ret = _unique1d(ar, return_index, return_inverse, return_counts)
/opt/conda/lib/python3.7/site-packages/numpy/lib/arraysetops.py:322 _unique1d
ar.sort()
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:900 __bool__
self._disallow_bool_casting()
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:504 _disallow_bool_casting
"using a `tf.Tensor` as a Python `bool`")
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:491 _disallow_when_autograph_enabled
" indicate you are trying to use an unsupported feature.".format(task))
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
Here the type of y_true
and y_pred
requires to be in list or numpy array
But the type of y_true
and y_pred
are,
y_true: <class 'tensorflow.python.framework.ops.Tensor'>
y_pred: <class 'tensorflow.python.framework.ops.Tensor'>
When directly try to print it (i.e, without type() function), it shows like this:
y_true: Tensor("IteratorGetNext:1", shape=(None, None), dtype=float32)
y_pred: Tensor("sequential_5/dense_5/Softmax:0", shape=(None, 5), dtype=float32)
Unable to use y_true.numpy()
(Convert a tensor to numpy array in Tensorflow?) and tf.make_ndarray(y_true)
(https://www.tensorflow.org/api_docs/python/tf/make_ndarray#:~:text=tf.make_ndarray(proto_tensor)) tried it..
How can I convert these datatypes in a way that, it can be passed to sklearn.metrics.cohen_kappa_score function? I don't want to write a code for kappa score. Is it possible to convert?