如何只预测标签而不是概率

时间:2018-07-20 07:15:32

标签: machine-learning tflearn

当我预测输出时,我得到每个标签(例如,输出= [0.002345,0.997655])而不是正确标签的概率。有什么方法可以只获得标签,而不是获得概率吗?

 
import tflearn    
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.data_utils import load_csv
import numpy as np

data, labels = load_csv("C:/Users/Animkush/Desktop/train.csv", 
categorical_labels=True, n_classes=2, target_column=30)


def preprocess(data, col):
    for id in sorted(col, reverse=True):
    [r.pop(id) for r in data]

    return np.array(data, dtype=np.float32)

ignore = [0]

data = preprocess(data, ignore)


network = input_data(shape=[None, 28], name="input")

network = tflearn.fully_connected(network, 32, activation="relu")
network = tflearn.fully_connected(network, 64, activation="relu")
network = tflearn.fully_connected(network, 128, activation="relu")
network = tflearn.fully_connected(network, 64, activation="relu")
network = tflearn.fully_connected(network, 32, activation="relu")

network = dropout(network, 0.8)

network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.001, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(network)

model.fit(data, labels, n_epoch=100, batch_size=500, show_metric=True)

p = np.array([[-3.043540624,-3.157307121,1.08846278,2.288643618,1.35980513,-1.064822523,0.325574266,-0.067793653,-0.270952836,-0.838586565,-0.414575448,-0.50314086,0.676501545,-1.692028933,2.000634839,0.666779696,0.599717414,1.725321007,0.28334483,2.102338793,0.661695925,0.435477209,1.375965743,-0.293803153,0.279798032,-0.145361715,-0.252773123,0.035764225]])

print(model.predict(p))

1 个答案:

答案 0 :(得分:2)

Numpy方法argmax通常用于此目的:

import numpy as np

p = np.array([0.002345, 0.997655]) # array of probabilities, as per your example
y = np.argmax(p) # hard class
y
# 1

# another example:
p = np.array([0.78, 0.22])
y = np.argmax(p)
y
# 0