44Functionality for making predictions with an estimator
55"""
66import logging
7+ import numpy as np
78
89log = logging .getLogger (__name__ )
910
@@ -106,7 +107,7 @@ def estimate_probability_multilabel(estimator, streamer, precision):
106107 )
107108
108109
109- def estimate_probability_multiclass (estimator , streamer , precision ):
110+ def estimate_probability_multiclass (estimator , streamer , precision , oneclass ):
110111 """
111112 Generate probabilities for a one-label, multiclass estimator
112113
@@ -119,12 +120,24 @@ def estimate_probability_multiclass(estimator, streamer, precision):
119120
120121 """
121122 texts = (doc .text for doc in streamer )
122- probs = estimator .pipeline .predict_proba (texts ).round (precision )
123- yield from (
124- (docidx , (class_ , probability ))
125- for docidx , doc_probs in zip (streamer .index , probs )
126- for class_ , probability in zip (estimator .pipeline .classes_ , doc_probs )
127- )
123+ probs = estimator .pipeline .predict_proba (texts )
124+ # If oneclass flag is true, only returns the predicted class
125+ if oneclass :
126+ class_indices = list (i [- 1 ] for i in np .argsort (probs , axis = 1 ))
127+ yield from (
128+ (docidx , (estimator .pipeline .classes_ [class_index ],
129+ doc_probs [class_index ].round (precision )))
130+ for docidx , doc_probs , class_index in zip (
131+ streamer .index , probs , class_indices )
132+ )
133+ # Else returns probabilty values for all classes
134+ else :
135+ yield from (
136+ (docidx , (class_ , probability .round (precision )))
137+ for docidx , doc_probs in zip (streamer .index , probs )
138+ for class_ , probability in zip (
139+ estimator .pipeline .classes_ , doc_probs )
140+ )
128141
129142
130143def estimate_probability_multilabel_multiclass (estimator , streamer , precision ):
@@ -140,7 +153,7 @@ def estimate_probability_multilabel_multiclass(estimator, streamer, precision):
140153
141154 """
142155 texts = (doc .text for doc in streamer )
143- probs = estimator .pipeline .predict_proba (texts )
156+ probs = estimator .pipeline .predict_proba (texts ). round ( precision )
144157 yield from (
145158 (docidx , (label_name , class_ , prob ))
146159 for label_name , label_probs in zip (estimator .label_names , probs )
@@ -149,7 +162,8 @@ def estimate_probability_multilabel_multiclass(estimator, streamer, precision):
149162 )
150163
151164
152- def estimate (estimator , corpus , probability , precision = 4 , * args , ** kwargs ):
165+ def estimate (estimator , corpus , probability , precision = 4 , oneclass = False ,
166+ * args , ** kwargs ):
153167 """
154168 Estimate label values for documents in corpus
155169
@@ -171,7 +185,7 @@ def estimate(estimator, corpus, probability, precision=4, *args, **kwargs):
171185 estimator , streamer , precision )
172186 elif estimator .multiclass : # Multiclass probability
173187 yield from estimate_probability_multiclass (
174- estimator , streamer , precision )
188+ estimator , streamer , precision , oneclass )
175189 else : # Simple probability
176190 yield from estimate_probability (
177191 estimator , streamer , precision )
0 commit comments