PyCM: a multi-class confusion matrix library that accepts prediction vectors or a ready-made matrix
Multi-class confusion matrix library in Python
At a glance
- What is it?
- sepandhaghighi/pycm is an MIT-licensed Python library for post-classification evaluation. It builds a confusion matrix from actual and predicted vectors or from a direct matrix, and adds helpers such as relabel, position, combine and NumPy export.
- Who is it for?
- PyCM suits data scientists who need a multi-class confusion matrix object with many derived statistics and who want to merge matrices across mini-batches, relabel classes or export to NumPy. It is a poor choice if you are stuck on a Python older than the cutoffs it lists, or if you expect plotting to work with only its two declared dependencies.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 1 day ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.
Star history
- sepandhaghighi/pycm1,507 ★ · +1
| Repository | 2026-09-10 | 2026-11-24 |
|---|---|---|
| sepandhaghighi/pycm | 1.5k | 1.5k |
1 Stars in the last 75 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Two input modes, vectors or a direct matrix, produce the same ConfusionMatrix object
PyCM accepts either data vectors or a matrix you already have. From vectors, you pass an actual vector and a predicted vector:
>>> from pycm import *
>>> y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
>>> y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
>>> cm = ConfusionMatrix(actual_vector=y_actu, predict_vector=y_pred)
>>> cm.classes
[0, 1, 2]From a direct matrix, you pass a nested dictionary:
>>> from pycm import *
>>> cm2 = ConfusionMatrix(matrix={"Class1": {"Class1": 1, "Class2": 2}, "Class2": {"Class1": 0, "Class2": 5}})
>>> cm2.classes
['Class1', 'Class2']The consequence is a choice at the start. Vectors keep per-observation information, which some later methods need. A direct matrix has already thrown that information away, so anything that refers to individual observations is unavailable from it. Both modes print the same way: print_matrix shows counts and print_normalized_matrix shows row proportions, which for the direct example gives 0.33333 and 0.66667 for the first class. The stat method with summary set to True prints the overall statistics block, which is where most of the evaluation metrics live.
position needs the original vectors, so a direct matrix cannot locate observations
The position method was added in version 2.8 and finds the indexes of observations in the predicted vector that made true positives, true negatives, false positives and false negatives. For the vector example above it returns a dictionary keyed by class, with lists such as TP: [1, 4, 9] for class 0:
>>> cm.position()
{0: {'FN': [], 'FP': [0, 7], 'TP': [1, 4, 9], 'TN': [2, 3, 5, 6, 8, 10, 11]}, 1: {'FN': [5, 10], 'FP': [3], 'TP': [6], 'TN': [0, 1, 2, 4, 7, 8, 9, 11]}, 2: {'FN': [0, 3, 7], 'FP': [5, 10], 'TP': [2, 8, 11], 'TN': [1, 4, 6, 9]}}This is useful for pulling out the exact rows that were misclassified. The limit follows from the input modes. A matrix built from counts only has no observation order, so the indexes cannot exist. If you need to inspect individual errors, build the object from vectors and keep your original arrays aligned with the index you pass in. The returned lists are positions in the predicted vector, so they line up with your original data only if you did not reorder either vector before the call.
combine merges matrices, which is how mini-batch evaluation works
The combine method, added in version 3.0, merges two confusion matrices, and the project says the option is useful in mini-batch learning. The README example merges cm2 with another matrix and prints a sum of counts:
>>> cm_combined = cm2.combine(cm3)
>>> cm_combined.print_matrix()
Predict Class1 Class2
Actual
Class1 2 4
Class2 0 10The result is that you can evaluate a large dataset in pieces, build a matrix per batch and merge them into one set of statistics. The merged result has the counts of both inputs added together, so the batches must use compatible class labels. If one batch names a class differently, relabel it first with the relabel method.
relabel and to_array handle naming and NumPy interop
The relabel method, added in version 1.5, changes the class names in an existing object:
>>> cm.relabel(mapping={0: "L1", 1: "L2", 2: "L3"})
>>> cm
pycm.ConfusionMatrix(classes: ['L1', 'L2', 'L3'])The to_array method, added in version 2.9, returns the matrix as a NumPy array so you can aggregate, normalise or combine it with your own code. It takes a normalized flag and a one_vs_all option with a class_name, which returns a two by two collapse for that class. The README notes that the older matrix and normalized_matrix names were renamed to print_matrix and print_normalized_matrix in version 1.5, so code from before that release needs the new names.
Other constructor options cover thresholds, weights, transposing and saved files
A set of options extends the constructor. The threshold parameter, added in version 0.9, supports real-valued predictions by activating a cutoff. The file option, added in version 0.9.5, loads a confusion matrix saved in the .obj format generated by the save_obj method. The sample_weight parameter arrived in version 1.2, and so did transpose, which transposes the input matrix but only in Direct CM mode.
The constraint on transpose matters if you receive a matrix with actual and predicted on swapped axes: you can only fix it at construction when you are in direct mode. For vector input you would swap the two vectors instead. Each of these has a worked example on the project site, Example3 for thresholds, Example4 for loading files and Example5 for sample weights.
Python version cutoffs are listed, and plotting needs an extra package
The installation notes carry three warnings. PyCM 4.3 is the last version to support Python 3.6. PyCM 3.9 is the last to support Python 3.5. PyCM 2.4 is the last to support Python 2.7 and Python 3.4. The current release, version 4.6, therefore needs something newer than 3.6, and the MATLAB route asks for Python 3.7 or newer.
The declared requirements are only art at 1.8 or newer and numpy at 1.9.0 or newer. Plotting is separate: the plot method, added in version 3.0, requires Matplotlib at 3.0.0 or newer or Seaborn at 0.9.1 or newer, and neither appears in requirements.txt. A plain pip install will therefore give you a working matrix object but an error the first time you call plot, until you install one of the two.
Install from PyPI, source, Conda or MATLAB, and the repository's last push is recent
The PyPI route pins the current version:
pip install pycm==4.6From source you download version 4.6 or the latest dev archive and run pip install . inside it. Conda users run conda install -c sepandhaghighi pycm after updating conda. MATLAB users install Python 3.7 or newer with pip, run pip install pycm, set the interpreter with pyversion and a full path, and use the examples in the MATLAB directory.
The recent releases are v4.6 on 2026-03-09, v4.5 on 2025-10-15 and v4.4 on 2025-08-16, and the last push was on 2026-11-02. The repository also has a docker directory, a paper directory, a Document folder and a CITATION.cff, which helps if you need to cite the library. Development files include a Test directory, pytest.ini, codecov.yml, dev-requirements.txt and autopep8 scripts, and the project tracks a master and a dev branch with separate CI. A first-time reader should note that dev is where the latest source archive comes from, so the dev zip is newer than the 4.6 tag.
Editorial conclusion
PyCM suits data scientists who need a multi-class confusion matrix object with many derived statistics and who want to merge matrices across mini-batches, relabel classes or export to NumPy. It is a poor choice if you are stuck on a Python older than the cutoffs it lists, or if you expect plotting to work with only its two declared dependencies. Before adopting it, pick the input mode that matches your data, and install Matplotlib or Seaborn separately if you want plots.
Frequently asked questions
What does PyCM do?
PyCM is a multi-class confusion matrix library written in Python. It builds a matrix from actual and predicted vectors or from a direct matrix, and supports most class and overall statistics for post-classification evaluation.
Does PyCM support plotting?
Yes, through the plot method added in version 3.0, but it needs Matplotlib 3.0.0 or newer, or Seaborn 0.9.1 or newer, which are not in requirements.txt.
Which Python versions does PyCM support?
PyCM 4.3 is the last version to support Python 3.6, 3.9 is the last for Python 3.5, and 2.4 is the last for Python 2.7 and 3.4. The current release is version 4.6.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/sepandhaghighi-pycm)Star history chart for your README
[](https://hysenlabs.com/projects/sepandhaghighi-pycm)Was this useful?