how to generate the confusion matrix through cross validation in python?

337 Views Asked by At

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)
0

There are 0 best solutions below