当我预测泰坦尼克号乘客是否幸存时,模型的输出是一个概率。我怎么能把它区分为0还是1?
这就是模型构造
inputs = keras.layers.Input(shape=(8,))
dropout = keras.layers.Dropout(0.2)(inputs)
hidden1 = keras.layers.Dense(40, activation=tf.nn.relu)(dropout)
hidden2 = keras.layers.Dense(30, activation=tf.nn.relu)(hidden1)
hidden3 = keras.layers.Dense(20, activation=tf.nn.relu)(hidden2)
out = keras.layers.Dense(1, activation=tf.nn.sigmoid)(hidden3)
mdl = keras.models.Model(inputs=inputs, outputs=out)当我用经过训练的模型预测结果时,我得到的概率不是标签(0或1)。
res = model.predict(test_data)问题:
如何将概率映射到标签(0或1)?
发布于 2020-01-08 02:50:04
下面是将概率映射到离散类标签的两种方法:
方法1:当不需要阈值时,
predicted_class = round(res) # rounds the probability value to 0 or 1方法2:当需要为分类设置阈值时
predicted_class = 1 if res>0.5 else 0 # here threshold = 0.5 and can be fine-tuned based on the observed precision and recall scoreshttps://stackoverflow.com/questions/59638610
复制相似问题