Esempio n. 1
0
    def __init__(self,
                 vectorizer=None,
                 model=None,
                 x_train=None,
                 y_train=None,
                 x_test=None,
                 y_test=None,
                 feature_names=None,
                 metadata=None,
                 *args,
                 **kwargs):
        if model:
            self._vectorizer = vectorizer
            self._model = model
            self._data = {
                'x_train': x_train,
                'x_train_vec': vectorizer.fit_transform(x_test),
                'y_train': y_train,
                'x_test': x_test,
                'x_test_vec': vectorizer.transform(x_test),
                'y_test': y_test,
            }
            now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')

            if metadata:
                self.metadata = metadata
            else:
                pred = model.predict(self._data['x_test_vec'])
                precision, recall, fscore, support = precision_recall_fscore_support(
                    y_test, pred)
                fpr, tpr, thresholds = roc_curve(y_test, pred)
                auc_score = auc(fpr, tpr)

                self.metadata = {
                    'vectorizer': vectorizer_to_str(vectorizer),
                    'model': str(model),
                    'created_at': now,
                    'feature_names': feature_names,
                    'git_hash': get_git_hash(),
                    'precision': [float(p) for p in precision],
                    'recall': [float(r) for r in recall],
                    'fscore': [float(f) for f in fscore],
                    'support': [int(s) for s in support],
                    'auc': auc_score
                }

            log.info('Initialized PackagedModel %s' % now)
Esempio n. 2
0
    def __init__(self,
                 pipeline=None,
                 feature_union=None,
                 x_train=None,
                 y_train=None,
                 x_test=None,
                 y_test=None,
                 metadata=None,
                 *args,
                 **kwargs):
        if pipeline:
            self._feature_union = feature_union  # will this maintain state?
            self._pipeline = pipeline
            self._data = {
                'x_train': x_train,
                'y_train': y_train,
                'x_test': x_test,
                'y_test': y_test,
            }
            now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

            if metadata:
                self.metadata = metadata
            else:
                x_test_vec = feature_union.transform(self._data['x_test'])
                pred = pipeline.predict(x_test_vec)
                precision, recall, fscore, support = precision_recall_fscore_support(
                    y_test, pred)
                fpr, tpr, thresholds = roc_curve(y_test, pred)
                auc_score = auc(fpr, tpr)

                self.metadata = {
                    'pipeline': str(pipeline),
                    'feature_union': str(feature_union),
                    'created_at': now,
                    'git_hash': get_git_hash(),
                    'precision': [float(p) for p in precision],
                    'recall': [float(r) for r in recall],
                    'fscore': [float(f) for f in fscore],
                    'support': [int(s) for s in support],
                    'auc': auc_score
                }

            log.info('Initialized PackagedPipeline %s' % now)
Esempio n. 3
0
    def __init__(
        self,
        vectorizer=None, model=None,
        x_train=None, y_train=None, x_test=None, y_test=None,
        feature_names=None, metadata=None,
        *args, **kwargs
    ):
        if model:
            self._vectorizer = vectorizer
            self._model = model
            self._data = {
                'x_train': x_train,
                'x_train_vec': vectorizer.fit_transform(x_test),
                'y_train': y_train,
                'x_test': x_test,
                'x_test_vec': vectorizer.transform(x_test),
                'y_test': y_test,
            }
            now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')

            if metadata:
                self.metadata = metadata
            else:
                pred = model.predict(self._data['x_test_vec'])
                precision, recall, fscore, support = precision_recall_fscore_support(y_test, pred)
                fpr, tpr, thresholds = roc_curve(y_test, pred)
                auc_score = auc(fpr, tpr)

                self.metadata = {
                    'vectorizer': vectorizer_to_str(vectorizer),
                    'model': str(model),
                    'created_at': now,
                    'feature_names': feature_names,
                    'git_hash': get_git_hash(),
                    'precision': [float(p) for p in precision],
                    'recall': [float(r) for r in recall],
                    'fscore': [float(f) for f in fscore],
                    'support': [int(s) for s in support],
                    'auc': auc_score
                }

            log.info('Initialized PackagedModel %s' % now)
Esempio n. 4
0
    def __init__(
        self,
        pipeline=None, feature_union=None,
        x_train=None, y_train=None, x_test=None, y_test=None,
        metadata=None,
        *args, **kwargs
    ):
        if pipeline:
            self._feature_union = feature_union  # will this maintain state?
            self._pipeline = pipeline
            self._data = {
                'x_train': x_train,
                'y_train': y_train,
                'x_test': x_test,
                'y_test': y_test,
            }
            now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

            if metadata:
                self.metadata = metadata
            else:
                x_test_vec = feature_union.transform(self._data['x_test'])
                pred = pipeline.predict(x_test_vec)
                precision, recall, fscore, support = precision_recall_fscore_support(y_test, pred)
                fpr, tpr, thresholds = roc_curve(y_test, pred)
                auc_score = auc(fpr, tpr)

                self.metadata = {
                    'pipeline': str(pipeline),
                    'feature_union': str(feature_union),
                    'created_at': now,
                    'git_hash': get_git_hash(),
                    'precision': [float(p) for p in precision],
                    'recall': [float(r) for r in recall],
                    'fscore': [float(f) for f in fscore],
                    'support': [int(s) for s in support],
                    'auc': auc_score
                }

            log.info('Initialized PackagedPipeline %s' % now)