0

I am using the iris flower dataset to do the sorting. I need to make a confusion matrix through cross validation (fold = 10) but I don't know how to do it. I generated the confusion matrix of only one round.

      # I am using TPOT autoML library for python

      import numpy as np
      import pandas as pd

      from sklearn.model_selection import train_test_split
      from sklearn.naive_bayes import GaussianNB, MultinomialNB
      from sklearn.pipeline import make_pipeline, make_union
      from tpot.builtins import StackingEstimator

      from sklearn.preprocessing import LabelEncoder

      tpot_data = pd.read_csv('iris.csv')

      tpot_data = tpot_data.apply(LabelEncoder().fit_transform)  

      features = tpot_data.drop('species', axis=1).values

      training_features, testing_features, training_target, testing_target = \
                           train_test_split(features, tpot_data['species'].values, random_state=10)

      exported_pipeline = make_pipeline(StackingEstimator(estimator=GaussianNB()),
                                        MultinomialNB(alpha=0.01, fit_prior=False)
      )


       exported_pipeline.fit(training_features, training_target)

       results = exported_pipeline.predict(testing_features)

        from sklearn import metrics
        print("Accuracy:", metrics.accuracy_score(testing_target, results))


       pd.crosstab(testing_target, results, rownames=['Actual Class'], colnames=['Predicted Class'])


        from sklearn.model_selection import cross_val_score

        array_cross_val_score = cross_val_score(estimator=exported_pipeline, X=training_features, 
                                    y=training_target, cv=10, scoring='accuracy')

        # I would like the confusion matrix to be based on the average cross-validation
        np.mean(array_cross_val_score)
Jane Borges
  • 552
  • 5
  • 14
  • 2
    Does this answer your question? [using confusion matrix as scoring metric in cross validation in scikit learn](https://stackoverflow.com/questions/40057049/using-confusion-matrix-as-scoring-metric-in-cross-validation-in-scikit-learn) – fsl Dec 04 '19 at 21:24
  • You can do it like [this](https://stackoverflow.com/a/59316264/2119941) – Hrvoje Dec 13 '19 at 04:54

0 Answers0