AttributeError: 'Functional' object has no attribute 'predict_proba'
For printing the ROC curve with multiple models, I am facing this particular error. Help is needed from tensorflow.keras.models import load_model def dense(): return (load_model('DenseNet...
stackoverflow.com
결론은 매우 단순하게도 y_pred와 y_score를 헷갈렸기 때문.
이진분류에서 y_pred_list는 [0,1,1, ....] 이런식이지만
1(참)일 확률인 y_score_list는 [0.78, 0.25,0.34 ...] 이런식이다.
# from sklearn.metrics import roc_curve
# fpr, tpr, thresholds = roc_curve(y_test, rand_clf.predict_proba(X_test)[:,1])
를
fpr, tpr, thresholds = roc_curve(y_true_test, y_score_test)
와 같이 변경하였으며
y_score_test는
for image, label in test_ds.batch(1):
...
의 반복문 안에서
y_score_val.append(model.predict_on_batch(image)[0][0])
를 추가해 주었다. ( [0][0]은 3차원을 1차원으로 뽑으려고 추가함 )
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
fpr, tpr, thresholds = roc_curve(y_true_val, y_score_val)
rand_score_val = roc_auc_score(y_true_val, y_score_val)
rand_score_val
# plt.plot([0,1], [0,1], "k--", "r+")
plt.plot([0,1], [0,1])
# plt.plot(fpr, tpr, label='DenseNet121')
plt.plot(fpr, tpr)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.title('Val Data - ROC curve(0119ver.)')
plt.text(0.75, 0, f"AUC score : {round(rand_score_val, 4)}")
plt.show()
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html
sklearn.metrics.roc_curve
Examples using sklearn.metrics.roc_curve: Species distribution modeling Species distribution modeling Visualizations with Display Objects Visualizations with Display Objects Detection error tradeof...
scikit-learn.org
항상 공식문서를 참고하자!!!
공식문서에 대놓고
y_score ndarray of shape (n_samples,)
Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
라고 친절히 적혀있었다.
'AI > tensorflow' 카테고리의 다른 글
[Tensorflow] EfficientNet 예제 (0) | 2023.02.14 |
---|---|
[keras] predict_on_batch 와 predict의 차이 ? (0) | 2023.02.01 |
[tf.data] data transform하여 dataset 추가하기 (0) | 2022.12.22 |
자주 사용하는 Tensor함수 정리 (0) | 2022.12.22 |
텐서플로우 전이학습 (0) | 2022.12.16 |