本文介绍了我们如何在Python上为自定义的ANN模型构建ROC曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python上构建自定义的ANN模型.构建模型的方法如下:

I am trying to build a customized ANN Model on Python. My method, where I have built the model, is as follows:

def binary_class(x_train,nodes,activation,n):
  #Creating customized ANN Model
  model=Sequential()
  for i in range(len(nodes)):
    if(i==0):
      if(activation=='sigmoid'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_uniform',activation='sigmoid',input_dim = len(x_train[1])))
      if(activation=='relu'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_uniform',activation='relu',input_dim = len(x_train[1])))
      if(activation=='tanh'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_normal',activation='tanh',input_dim = len(x_train[1])))
      if(activation=='softmax'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_normal',activation='softmax',input_dim = len(x_train[1])))
      if(activation== 'elu'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_normal',activation='elu',input_dim = len(x_train[1])))
      if(activation=='softplus'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_normal',activation='softplus',input_dim = len(x_train[1])))
    else:
      if(activation=='sigmoid'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_uniform',activation='sigmoid'))
      if(activation=='relu'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_uniform',activation='relu'))
      if(activation=='tanh'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_normal',activation='tanh'))
      if(activation=='softmax'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'glorot_uniform',activation='softmax'))
      if(activation=='elu'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_normal',activation='elu'))
      if(activation=='softplus'):
        model.add(Dense(units = nodes[i], kernel_initializer = 'he_normal',activation='softplus'))
    model.add(Dropout(n))
  #Adding output layer
  model.add(Dense(units=1, kernel_initializer = 'glorot_uniform',activation='sigmoid'))
  return model

我的优化器功能如下:

def optibin(model,opt,x_train,y_train,spl,bs,epochs,x_test,y_test):
  #Choosing the proper optimizer to use
  if(opt=='sgd'):
    print("Enter Momentum:")
    mom=float(input())
    lr=float(input("Enter value of Learning rate:"))
    opti=keras.optimizers.SGD(learning_rate=lr, momentum=mom, nesterov=False)
  if(opt=='Adam'):
    lr=float(input("Enter value of Learning rate:"))
    opti=keras.optimizers.Adam(learning_rate=lr)
  if(opt=='Adamax'):
    lr=float(input("Enter value of Learning rate:"))
    beta_1=float(input("Enter value of beta 1 (Generally close to 1)"))
    beta_2=float(input("Enter value of beta 2 (Generally close to 1)"))
    opti=keras.optimizers.Adamax(learning_rate=lr, beta_1=beta_1, beta_2=beta_2)
  if(opt=='Nadam'):
    lr=float(input("Enter value of Learning rate:"))
    beta_1=float(input("Enter value of beta 1 (Generally close to 1)"))
    beta_2=float(input("Enter value of beta 2 (Generally close to 1)"))
    opti=keras.optimizers.Nadam(learning_rate=lr, beta_1=beta_1, beta_2=beta_2)
  if(opt=='RMSprop'):
    lr=float(input("Enter value of Learning rate:"))
    opti=keras.optimizers.RMSprop(learning_rate=lr)
  if(opt=='Adagrad'):
    lr=float(input("Enter value of Learning rate:"))
    opti=keras.optimizers.Adagrad(learning_rate=lr)
  model.compile(optimizer = opti, loss = 'binary_crossentropy', metrics = ['accuracy'])
  model_history=model.fit(x_train, y_train,validation_split=spl, batch_size = bs,epochs = epochs)
  return model_history, model

我必须尝试创建模型的性能指标,其中之一就是构建ROC和AUC.我用sklearn制作了混淆矩阵,特异性和敏感性.但是我还需要绘制ROC曲线.如何从中建立ROC曲线?

I have to try to create the performance metrics of the model, one of which would be to build the ROC and the AUC. I used sklearn to make the confusion matrix, specificity and sensitivity. But I need to make a ROC curve as well.How can we build the ROC curve from this?

推荐答案

这种方法应该可以解决问题:

Something like this should do the trick:

from sklearn import metrics

fpr, tpr, thresholds = metrics.roc_curve(true_values, predicted_values, pos_label=1)
roc_auc = metrics.auc(fpr, tpr)

lw = 2
plt.figure()
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--', alpha=0.15)
plt.plot(fpr, tpr, lw=lw, label=f'ROC curve (area = {roc_auc: 0.2f})')

plt.xlabel('(1–Specificity) - False Positive Rate')
plt.ylabel('Sensitivity - True Positive Rate')
plt.title(f'Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()

这篇关于我们如何在Python上为自定义的ANN模型构建ROC曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 16:48