Package: disarray
disarraydisarraycalculates metrics derived from a confusion matrix and makes them directly accessible from a pandas DataFrame.If you are already usingpandas, thendisarrayis easy to use, simply importdisarray:importpandasaspddf=pd.DataFrame([[18,1],[0,1]])importdisarraydf.da.sensitivity00.94736811.000000dtype:float64Table of contentsInstallationUsagebinary classificationclass countsexport metricsmulti-class classificationsupported metricsWhy disarrayContributingInstallationInstall using pip$pipinstalldisarrayClone from GitHub$gitclonehttps://github.com/arvkevi/disarray.git
$pythonsetup.pyinstallUsageThedisarraypackage is intended to be used similar to apandasattribute or method.disarrayis registered as
apandasextension underda. For a DataFrame nameddf, access the library usingdf.da..Binary ClassificationTo understand the input and usage fordisarray, build an example confusion matrix for abinary classificationproblem from scratch withscikit-learn.(You can install the packages you need to run the demo with:pip install -r requirements.demo.txt)fromsklearnimportsvm,datasetsfromsklearn.model_selectionimporttrain_test_splitfromsklearn.metricsimportconfusion_matrix# Generate a random binary classification datasetX,y=datasets.make_classification(n_classes=2,random_state=42)X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42)# fit and predict an SVMclassifier=svm.SVC(kernel='linear',C=0.01)y_pred=classifier.fit(X_train,y_train).predict(X_test)cm=confusion_matrix(y_test,y_pred)print(cm)[[132][010]]Usingdisarrayis as easy as importing it and instantiating a DataFrame object from asquarearray ofpositiveintegers.importdisarrayimportpandasaspddf=pd.DataFrame(cm)# access metrics for each class by indexprint(df.da.precision[1])0.83Class Countsdisarraystores per-class counts of true positives, false positives, false negatives, and true negatives. Each of these are stored as capitalized abbreviations,TP,FP,FN, andTN.df.da.TP013110dtype:int64Export MetricsUsedf.da.export_metrics()to store and/or visualize many common performance metrics in a newpandasDataFrame
object. Use themetrics_to_include=argument to pass a list of metrics defined indisarray/metrics.py(default is
to use__all_metrics__).df.da.export_metrics(metrics_to_include=['precision','recall','f1'])01micro-averageprecision1.00.8333330.92recall0.8666671.00.92f10.9285710.9090910.92Multi-Class Classificationdisarrayworks with multi-class classification confusion matrices also. Try it out on the iris dataset. Notice, the
DataFrame is instantiated with anindexandcolumnshere, but it is not required.# load the iris datasetiris=datasets.load_iris()X=iris.datay=iris.targetclass_names=iris.target_names# split the training and testing dataX_train,X_test,y_train,y_test=train_test_split(X,y,random_state=0)# train and fit a SVMclassifier=svm.SVC(kernel='linear',C=0.01)y_pred=classifier.fit(X_train,y_train).predict(X_test)cm=confusion_matrix(y_test,y_pred)# Instantiate the confusion matrix DataFrame with index and columnsdf=pd.DataFrame(cm,index=class_names,columns=class_names)print(df)setosaversicolorvirginicasetosa1300versicolor0106virginica009disarraycan provide per-class metrics:df.da.sensitivitysetosa1.000versicolor0.625virginica1.000dtype:float64In a familiar fashion, one of the classes can be accessed with bracket indexing.df.da.sensitivity['setosa']1.0Currently, amicro-averageis supported for both binary and
multi-class classification confusion matrices. (Although it only makes sense in the multi-class case).df.da.micro_sensitivity0.8421052631578947Finally, a DataFrame can be exported with selected metrics.df.da.export_metrics(metrics_to_include=['sensitivity','specificity','f1'])setosaversicolorvirginicamicro-averagesensitivity1.00.6251.00.842105specificity1.01.00.7931030.921053f11.00.7692310.750.842105Supported Metrics'accuracy','f1','false_discovery_rate','false_negative_rate','false_positive_rate','negative_predictive_value','positive_predictive_value','precision','recall','sensitivity','specificity','true_negative_rate','true_positive_rate',As well as micro-averages for each of these, accessible viadf.da.micro_recall, for example.Why disarray?Working with aconfusion matrixis common in data science projects. It is useful to have performance metrics available directly frompandasDataFrames.Sincepandasversion0.23.0, users can easilyregister custom accessors,
which is howdisarrayis implemented.ContributingContributions are welcome, please refer toCONTRIBUTINGto learn more about how to contribute.