【问题标题】:Tensorflow Model, Random Forest AUC - How to calculate using Session?Tensorflow 模型,随机森林 AUC - 如何使用 Session 计算?
【发布时间】:2019-12-29 23:53:33
【问题描述】:

试图弄清楚在使用 Session 训练时如何在 Random Forest Tensorflow 中计算 AUC。

尝试了许多类似这里提到的方法:

我认为我无法使用此处看到的代码遵循上述模式:

# Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(input_x, input_y, test_size = 0.25, random_state = 0)

data1 = data.iloc[:,:].values


# Parameters
num_steps = 50 # Total steps to train
num_classes = 2 
num_features = 14
num_trees = 10 
max_nodes = 1000 

# Input and Target placeholders 
X = tf.placeholder(tf.float32, shape=[None, num_features])
Y = tf.placeholder(tf.int64, shape=[None])


# Random Forest Parameters
hparams = tensor_forest.ForestHParams(num_classes=num_classes, num_features=num_features, num_trees=num_trees, max_nodes=max_nodes).fill()

# Build the Random Forest
forest_graph = tensor_forest.RandomForestGraphs(hparams)
train_op = forest_graph.training_graph(X, Y)
loss_op = forest_graph.training_loss(X, Y)
infer_op, _, _ = forest_graph.inference_graph(X)

### ACCURACY DEFINITION
correct_prediction = tf.equal(tf.argmax(infer_op, 1), tf.cast(Y, tf.int64))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


### AUC DEFINITION
from sklearn.metrics import roc_auc_score
HELP HERE ? 


### SESSION DEFINITION
init_vars = tf.group(tf.global_variables_initializer(), resources.initialize_resources(resources.shared_resources()))

sess = tf.Session()

# Training here
for i in range(1, num_steps + 1):
    _, l = sess.run([train_op, loss_op], feed_dict={X: X_train, Y: y_train})
    sess.run(tf.local_variables_initializer())


    if i % 50 == 0 or i == 1:

        acc = sess.run(accuracy_op, feed_dict={X: X_train, Y: y_train})
        HELP HERE TO MAKE AUC AVAILABLE TOO?        

        print('Step %i, Loss: %f, Acc: %f' % (i, l, acc))


# EVALUATION
print("Test Accuracy:", sess.run(accuracy_op, feed_dict={X: X_test, Y: y_test}))

HELP HERE TO PRINT AUC?

谁能帮助我了解如何在此处包含 AUC 计算?

【问题讨论】:

    标签: python tensorflow machine-learning neural-network random-forest


    【解决方案1】:

    这是你需要的:

    predictions=pd.DataFrame(model.predict_proba(X_test),columns=model.classes_)
    roc_score = roc_auc_score((real_test_class == 'True').astype(float), predictions['True'])
    

    real_test_class 是包含真实类的向量,predictions['True'] 是预测数据框中的列,其中包含每个样本的概率真的'

    【讨论】:

    • 对不起,如果我要问一个菜鸟问题:) real_test_class 不应该是 y_train 还是 y_test?看了上面的代码,能否请你指导我应该在哪里引入这段代码?
    • real_test_class 是你的 y_test。在使用 predict_proba 之后进行 roc_score 计算,以预测真实类的概率。不要忘记从 sklearn 导入 roc_score_auc。我将 predict_proba 添加到代码中。请注意,tensorflow 可以使用相同的 roc_auc_score 函数,只需给它正确的概率即可。
    猜你喜欢
    • 2021-06-12
    • 2019-11-08
    • 2021-04-26
    • 2016-02-21
    • 2018-06-06
    • 1970-01-01
    • 2019-07-10
    • 2017-08-11
    • 2019-11-26
    相关资源
    最近更新 更多