def create_classification_report_chart(classifier, X_train, X_test, y_train, y_test): """Create classification report chart. Tip: Check Sklearn-Neptune integration `documentation <https://docs-beta.neptune.ai/essentials/integrations/machine-learning-frameworks/sklearn>`_ for the full example. Args: classifier (:obj:`classifier`): | Fitted sklearn classifier object X_train (:obj:`ndarray`): | Training data matrix X_test (:obj:`ndarray`): | Testing data matrix y_train (:obj:`ndarray`): | The classification target for training y_test (:obj:`ndarray`): | The classification target for testing Returns: ``neptune.types.File`` object that you can assign to run's ``base_namespace``. Examples: .. code:: python3 import neptune.new.integrations.sklearn as npt_utils rfc = RandomForestClassifier() rfc.fit(X_train, y_train) run = neptune.init(project='my_workspace/my_project') run['visuals/classification_report'] = \ npt_utils.create_classification_report_chart(rfc, X_train, X_test, y_train, y_test) """ assert is_classifier( classifier), 'classifier should be sklearn classifier.' chart = None try: fig, ax = plt.subplots() visualizer = ClassificationReport(classifier, support=True, is_fitted=True, ax=ax) visualizer.fit(X_train, y_train) visualizer.score(X_test, y_test) visualizer.finalize() chart = neptune.types.File.as_image(fig) plt.close(fig) except Exception as e: print('Did not log Classification Report chart. Error: {}'.format(e)) return chart
def log_classification_report_chart(classifier, X_train, X_test, y_train, y_test, experiment=None): """Log classification report chart. Make sure you created an experiment by using ``neptune.create_experiment()`` before you use this method. Tip: Check `Neptune documentation <https://docs.neptune.ai/integrations/scikit_learn.html>`_ for the full example. Args: classifier (:obj:`classifier`): | Fitted sklearn classifier object X_train (:obj:`ndarray`): | Training data matrix X_test (:obj:`ndarray`): | Testing data matrix y_train (:obj:`ndarray`): | The classification target for training y_test (:obj:`ndarray`): | The classification target for testing experiment (:obj:`neptune.experiments.Experiment`, optional, default is ``None``): | Neptune ``Experiment`` object to control to which experiment you log the data. | If ``None``, log to currently active, and most recent experiment. Returns: ``None`` Examples: .. code:: python3 rfc = RandomForestClassifier() rfc.fit(X_train, y_train) neptune.init('my_workspace/my_project') exp = neptune.create_experiment() log_classification_report_chart(rfc, X_train, X_test, y_train, y_test, experiment=exp) """ assert is_classifier(classifier), 'classifier should be sklearn classifier.' exp = _validate_experiment(experiment) try: fig, ax = plt.subplots() visualizer = ClassificationReport(classifier, support=True, is_fitted=True, ax=ax) visualizer.fit(X_train, y_train) visualizer.score(X_test, y_test) visualizer.finalize() exp.log_image('charts_sklearn', fig, image_name='Classification Report') plt.close(fig) except Exception as e: print('Did not log Classification Report chart. Error: {}'.format(e))
def test_prepredict_classifier(self): """ Test the prepredict estimator with classification report """ # Make prepredictions X, y = self.multiclass.X, self.multiclass.y y_pred = GaussianNB().fit(X.train, y.train).predict(X.test) # Create prepredict estimator with prior predictions estimator = PrePredict(y_pred, CLASSIFIER) assert estimator.fit(X.train, y.train) is estimator assert estimator.predict(X.train) is y_pred assert estimator.score(X.test, y.test) == pytest.approx(0.41, rel=1e-3) # Test that a visualizer works with the pre-predictions. viz = ClassificationReport(estimator) viz.fit(None, y.train) viz.score(None, y.test) viz.finalize() self.assert_images_similar(viz)
def classification(fname="classification.png"): # Create side-by-side axes grid _, axes = plt.subplots(ncols=2, figsize=(18,6)) # Add ClassificationReport to the reft data = load_spam(split=True) oz = ClassificationReport(MultinomialNB(), classes=["ham", "spam"], ax=axes[0]) oz.fit(data.X.train, data.y.train) oz.score(data.X.test, data.y.test) oz.finalize() # Add DiscriminationThreshold to the right data = load_spam(split=False) oz = DiscriminationThreshold(LogisticRegression(), ax=axes[1]) oz.fit(data.X, data.y) oz.finalize() # Save figure path = os.path.join(FIGURES, fname) plt.tight_layout() plt.savefig(path)
def classification(fname="classification.png"): # Create side-by-side axes grid _, axes = plt.subplots(ncols=2, figsize=(18, 6)) # Add ClassificationReport to the reft data = load_spam(split=True) oz = ClassificationReport(MultinomialNB(), classes=["ham", "spam"], ax=axes[0]) oz.fit(data.X.train, data.y.train) oz.score(data.X.test, data.y.test) oz.finalize() # Add DiscriminationThreshold to the right data = load_spam(split=False) oz = DiscriminationThreshold(LogisticRegression(), ax=axes[1]) oz.fit(data.X, data.y) oz.finalize() # Save figure path = os.path.join(FIGURES, fname) plt.tight_layout() plt.savefig(path)