Функция для построения двух разных графиков в al oop in python - PullRequest
0 голосов
/ 18 июня 2020

У меня есть две следующие функции для построения кривой RO C и кривой точного отзыва.

def plotRoc(preds, truth, label):
    fpr, tpr, thresholds = roc_curve(truth, preds)
    roc_auc = auc(fpr,tpr)

    c = (np.random.rand(), np.random.rand(), np.random.rand())
    plt.plot(fpr, tpr, color = c, label = str(label) + ': AUC = %0.3f' % roc_auc)
    plt.plot([0, 1], [0, 1], 'r--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver Operating Characteristic (ROC) Curve')
    plt.legend(loc = 'lower right')

def plotPRCurve(preds, truth, label):
    precision, recall, thresholds = precision_recall_curve(truth, preds)
    pr_auc = auc(recall,precision)
    avg_precision = average_precision_score(truth, preds)

    c = (np.random.rand(), np.random.rand(), np.random.rand())
    plt.plot(recall, precision, color = c,  label = str(label) + ': AVP = %0.3f' % avg_precision)
    plt.xlim([0.0, 1.0])
    plt.ylim([0, 1.05])
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.title('Precision Recall Curve')
    plt.legend(loc = 'lower right')


Я хочу построить 2 графика с каждой из 5 точек, показанных на обоих графиках для AU C и Precision Recall. Однако когда я запускаю свой код, все они строятся на одном графике. Как я могу разделить их на два разных участка?

skf = StratifiedKFold(n_splits=5, shuffle = True, random_state = None)
skf.get_n_splits(X,y)

roc = []

classifier = LogisticRegression(C = 1e9)

for i, (train_index, test_index) in enumerate(skf.split(X, y)):
    X_train, X_test = X.loc[train_index], X.loc[test_index]
    y_train, y_test = y.loc[train_index], y.loc[test_index]

    classifier.fit(X_train,y_train)
    prediction = classifier.predict_proba(X_test)[:,1] ## probability of 1
    score = roc_auc_score(y_test,prediction)
    roc.append(score)

    plotPRCurve(prediction, y_test,"Fold"+str(i+1))
    plotRoc(prediction, y_test,"Fold"+str(i+1))

print('Scores:',roc)
print('Stratified 5-Fold Cross Validation Score:',round(np.array(roc).mean(),3))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...