简体   繁体   中英

How to get probability and label prediction at the same time using sklearn

I have a multiclass classifier and I need to get the probability and label at the same time.

model.predict_proba(X) returns the probabilities for all trained classes for each data point.

model.predict(X) returns the label for each data point.

I don't want to predict twice each data point.

Simply use predict_proba and then to get the label use np.argmax :

dataset = read_csv('pollution.csv', header=0, index_col=0)
x = dataset[['pollution', 'dew', 'temp', 'press', 'wnd_spd', 'snow']].values
y = dataset[['wnd_dir']].values
from sklearn.ensemble import RandomForestClassifier

cls = RandomForestClassifier(random_state=0)
cls.fit(x, y)

z = cls.predict_proba(x)
labels = np.argmax(z, axis=1)
classes = cls.classes_
labels = [classes[i] for i in labels]
print(accuracy_score(y, labels))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM