Python 属性错误:';功能性';对象没有属性';预测概率';
对于使用多个模型打印ROC曲线,我面临着这个特殊的错误。需要帮助Python 属性错误:';功能性';对象没有属性';预测概率';,python,matplotlib,scikit-learn,roc,Python,Matplotlib,Scikit Learn,Roc,对于使用多个模型打印ROC曲线,我面临着这个特殊的错误。需要帮助 from tensorflow.keras.models import load_model def dense(): return (load_model('DenseNet201.h5')) def mobile(): return(load_model('MobileNet.h5')) def res(): return(load_model('ResNet50V2
from tensorflow.keras.models import load_model
def dense():
return (load_model('DenseNet201.h5'))
def mobile():
return(load_model('MobileNet.h5'))
def res():
return(load_model('ResNet50V2.h5'))
def vgg():
return(load_model('VGG16.h5'))
models = [
{
'label': 'DenseNet201',
'model': dense(),
},
{
'label': 'MobileNet',
'model':mobile(),
},
{
'label': 'ResNet50V2',
'model':res(),
},
{
'label': 'VGG16',
'model':vgg(),
}]
from sklearn import metrics
import matplotlib.pyplot as plt
from tensorflow.keras.utils import to_categorical
plt.figure()
# Below for loop iterates through your models list
for m in models:
model = m['model'] # select the model
#model.fit(X_train, y_train) # train the model
y_pred=model.predict(X_test) # predict the test data
# Compute False postive rate, and True positive rate
#fpr, tpr, thresholds = metrics.roc_curve(y_test, model.y_pred_bin(X_test)[:,1])
fpr, tpr, thresholds = metrics.roc_curve(y_test, model.predict_proba(X_test)[:,1])
# Calculate Area under the curve to display on the plot
auc = metrics.roc_auc_score(y_test,model.predict(X_test))
# Now, plot the computed values
plt.plot(fpr, tpr, label='%s ROC (area = %0.2f)' % (m['label'], auc))
# Custom settings for the plot
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('1-Specificity(False Positive Rate)')
plt.ylabel('Sensitivity(True Positive Rate)')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show() # Display
我将预先训练好的模型加载到一个函数中,并使用以下代码返回它。我做了一个列表,它将被迭代,它将调用加载这些模型的函数,因此每个模型的ROC曲线将被绘制
完全回溯
> AttributeError Traceback (most recent call
> last) <ipython-input-43-f353a6208636> in <module>()
> 11 # Compute False postive rate, and True positive rate
> 12 #fpr, tpr, thresholds = metrics.roc_curve(y_test, model.y_pred_bin(X_test)[:,1])
> ---> 13 pred_prob = model.predict_proba(X_test)
> 14 fpr, tpr, thresholds = metrics.roc_curve(y_test, pred_prob[:,1])
> 15 # Calculate Area under the curve to display on the plot
>
> AttributeError: 'Functional' object has no attribute 'predict_proba'
>属性错误回溯(最近的调用
>最后)在()
>11#计算假阳性率和真阳性率
>12#fpr,tpr,thresholds=metrics.roc_曲线(y#u检验,model.y#u pred#u bin(X#检验)[:,1])
>-->13预测概率=模型预测概率(X检验)
>14 fpr,tpr,阈值=度量。roc_曲线(y_检验,预测概率[:,1])
>15#计算曲线下的面积以显示在绘图上
>
>AttributeError:“Functional”对象没有属性“predict\u proba”
您可以使用此-
模型。批量预测(X检验)
请注意,roc_曲线或f1_分数只接受单值输出。像[[1],[0],…[1]]
在我的例子中,我把它转换成了category,比如[[0,1],[1,0],…,[0,1]],并在输出中使用了softmax
因此,模型。批量预测(X_检验)也给出了输出likr[[0.3,0.7],[0.9,0.1]…[0.2,0.8]]
因此,我必须将其转换为单值输出,然后使用以下函数通过roc_曲线或sklearn的f1_分数:
def y_(y):
r = []
for i in y:
if i[0] > 0.5:
r.append([0])
else:
r.append([1])
return np.array(r)
您可以包括完整的回溯吗?
predict\u proba
仅适用于某些类型的模型。。。很可能您的列表中有一个模型不支持该方法。。。您可以使用try:
模型找出导致错误的模型。predict\u proba(X\u test)
除外:
打印(模型名称)
请查看。我用完整的回溯更新了代码