machine-learningscikit-learnevaluationconfusion-matrix# Get Scikit-learn classification report from confusion matrix

Found this question by @CarstenWE but it had been closed with no answer: How to get classification report from the confusion matrix?

As the question is closed, I opened this question to provide an answer.
The questions linked to the original all have answers to compute precision, recall, and f1-score. However, none seems to use the `classification_report`

as the original question asked.

Solution

I wrote a small function to do this using a confusion matrix as input, by creating a ground-truth vector and a predicted vector, as order does not matter for these metrics:

```
def classification_report_from_confusion_matrix(cm, **args):
y_true = []
y_pred = []
for target in range(len(cm)):
for pred in range(len(cm)):
y_true += [gt]*cm[target][pred]
y_pred += [pred]*cm[target][pred]
return metrics.classification_report(y_true , y_pred, **args)
```

This solution probably does not scale well for huge datasets, but it was enough for me.

**Edit:**

Here is a solution without using lists:

```
def classification_report_from_confusion_matrix(confusion_matrix, **args):
y_true = np.zeros(np.sum(confusion_matrix), dtype=int)
y_pred = np.copy(y_true)
i = 0
for target in range(len(confusion_matrix)):
for pred in range(len(confusion_matrix)):
n = confusion_matrix[target][pred]
y_true[i:i+n] = target
y_pred[i:i+n] = pred
i += n
return metrics.classification_report(y_true, y_pred, **args)
```

- Validation data without targets
- Is calling stack a LIFO queue correct?
- Why is the accuracy for my Keras model always 0 when training?
- Why KL divergence is negative in Pytorch?
- Is this classification model overfitting?
- How to calculate the computational complexity of machine learning algorithms
- Subsample size in scikit-learn RandomForestClassifier
- Common causes of nans during training of neural networks
- One class SVM - Outliers on test set very low relative to training set
- time series forecasting visit dates with customer classes graph not accurate
- How to accumulate gradients for large batch sizes in Keras
- Model precision is 0% in confusion matrix
- UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. 'recall', 'true', average, warn_for)
- Precision and F-score are ill-defined warning while using Python machine learning model
- How to fix this classification report warning?
- Is there a way to use found sequential patterns as input for a clustering algorithm
- Recursion in FP-Growth Algorithm
- How to visualize an XGBoost tree from GridSearchCV output?
- What is the role of normalization function in TensorFlow?
- Data augmentation not increasing dataset size
- CUML RandomForestClassifier TypeError An Integer is required
- scikit-learn custom transformer throws NotFittedError from underlying model
- Hyperopt set timeouts and modify space during execution
- Rescaling after feature scaling, linear regression
- Scala Support Vector Machine library
- Ways to get intermediate steps during optimization process in qiskit.algorithms.optimizers.ADAM
- r neuralnet package -- multiple output
- subplot for shap summary_plot
- How to improve accuracy score for this regression problem?
- AutoTrain advanced CLI: error: unrecognized arguments: --fp16 --use-int4