我需要specificity我的分类,定义为:
TN/(TN+FP)
我正在写一个自定义得分手功能:
from sklearn.metrics import make_scorer
def specificity_loss_func(ground_truth, predictions):
print predictions
tp, tn, fn, fp = 0.0,0.0,0.0,0.0
for l,m in enumerate(ground_truth):
if m==predictions[l] and m==1:
tp+=1
if m==predictions[l] and m==0:
tn+=1
if m!=predictions[l] and m==1:
fn+=1
if m!=predictions[l] and m==0:
fp+=1
`return tn/(tn+fp)
score = make_scorer(specificity_loss_func, greater_is_better=True)
Run Code Online (Sandbox Code Playgroud)
然后,
from sklearn.dummy import DummyClassifier
clf_dummy = DummyClassifier(strategy='most_frequent', random_state=0)
ground_truth = [0,0,1,0,1,1,1,0,0,1,0,0,1]
p = [0,0,0,1,0,1,1,1,1,0,0,1,0]
clf_dummy = clf_dummy.fit(ground_truth, p)
score(clf_dummy, ground_truth, p)
Run Code Online (Sandbox Code Playgroud)
当我运行这些命令时,我p打印为:
[0 0 0 0 0 …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码来比较多个模型的性能:
from sklearn import model_selection
X = input data
Y = binary labels
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
results = []
names = []
scoring = 'accuracy'
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=7)
cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold,scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %.2f (%.2f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
Run Code Online (Sandbox Code Playgroud)
我可以使用“准确性”和“回忆”作为评分,这些将提供准确性和敏感性。我怎样才能创建一个给我“特异性”的记分员
特异性= TN/(TN+FP)
其中 TN 和 FP 是混淆矩阵中的真负值和假正值
我试过这个
def tp(y_true, y_pred):
error= confusion_matrix(y_true, y_pred)[0,0]/(confusion_matrix(y_true,y_pred)[0,0] + confusion_matrix(y_true, y_pred)[0,1])
return error …Run Code Online (Sandbox Code Playgroud)