| 243 | |
| 244 | |
| 245 | class BNSClassifier(BaseEstimator, ClassifierMixin): |
| 246 | |
| 247 | def __init__(self, estimator, use_bns, |
| 248 | use_text, use_frc, k, n_jobs=1): |
| 249 | assert(use_text or use_frc) |
| 250 | self.estimator = estimator |
| 251 | self.n_jobs = n_jobs |
| 252 | self.use_bns = use_bns |
| 253 | self.use_text = use_text |
| 254 | self.use_frc = use_frc |
| 255 | self.k = k |
| 256 | |
| 257 | def fit(self, X, y): |
| 258 | self.label_binarizer = LabelBinarizer(sparse_output=True) |
| 259 | Y = self.label_binarizer.fit_transform(y) |
| 260 | Y = Y.tocsc() |
| 261 | self.classes = self.label_binarizer.classes_ |
| 262 | columns = (col.toarray().ravel() for col in Y.T) |
| 263 | |
| 264 | self.pipelines = Parallel(n_jobs=self.n_jobs)( |
| 265 | delayed(_fit_binary)( |
| 266 | self.estimator, self.use_bns, |
| 267 | self.use_text, self.use_frc, self.k, X, column) |
| 268 | for column in columns) |
| 269 | |
| 270 | def predict(self, X): |
| 271 | if(hasattr(self.pipelines[0], 'decision_function') and |
| 272 | is_classifier(self.pipelines[0])): |
| 273 | thresh = 0 |
| 274 | else: |
| 275 | thresh = 0.5 |
| 276 | |
| 277 | n_samples = X['count'].shape[0] |
| 278 | if self.label_binarizer.y_type_ == 'multiclass': |
| 279 | maxima = np.empty(n_samples, dtype=float) |
| 280 | maxima.fill(-np.inf) |
| 281 | argmaxima = np.zeros(n_samples, dtype=int) |
| 282 | for i, p in enumerate(self.pipelines): |
| 283 | pred = _predict_binary(p, X) |
| 284 | np.maximum(maxima, pred, out=maxima) |
| 285 | argmaxima[maxima == pred] = i |
| 286 | return self.classes[np.array(argmaxima.T)] |
| 287 | else: |
| 288 | indices = array.array('i') |
| 289 | indptr = array.array('i', [0]) |
| 290 | for p in self.pipelines: |
| 291 | indices.extend(np.where(_predict_binary(p, X) > thresh)[0]) |
| 292 | indptr.append(len(indices)) |
| 293 | data = np.ones(len(indices), dtype=int) |
| 294 | indicator = csc_matrix((data, indices, indptr), |
| 295 | shape=(n_samples, len(self.pipelines))) |
| 296 | return self.label_binarizer.inverse_transform(indicator) |
| 297 | |
| 298 | class Classifier(): |
| 299 | |