我正在尝试用scikit learn库计算非数字值的Cohen's kappa。有没有办法将一组标签"happy","sad","happy“转换成一个浮点数?
from skll.metrics import kappa
y_true = ["HAPPINESS","OTHER","NONE","FEAR","NONE","NONE","NONE","ANGER"]
y_pred = ["HAPPINESS","NONE","NONE","FEAR","NONE","NONE","NONE","NONE"]
kappa_val = kappa(y_true, y_pred)现在我得到了这个错误:
ValueError: could not convert string to float: "HAPPINESS"发布于 2016-02-11 18:55:06
您需要使用LabelEncoder
from sklearn import preprocessing
from sklearn.metrics import classification
import numpy as np
y_true = ["HAPPINESS","OTHER","NONE","FEAR","NONE","NONE","NONE","ANGER"]
y_pred = ["HAPPINESS","NONE","NONE","FEAR","NONE","NONE","NONE","NONE"]
enc = preprocessing.LabelEncoder()
enc.fit(np.hstack((y_pred, y_true))) #have to give it all possible labels
kappa_val = classification.cohen_kappa_score(enc.transform(y_true), enc.transform(y_pred))
print kappa_val0.578947368421https://stackoverflow.com/questions/35321173
复制相似问题