Using Keras/sklearn with CalibratedClassifierCV from sklearn.calibration

Viewed 969

Is it possible to use Keras model objects with CalibratedClassifierCV from sklearn.calibration? Or is there another way to performa isotonic regression in sklearn/other python packages without having to pass it a model object.

I tried using the sklearn wrapper for Keras, but it didn't work. Here is the doc for the CalibratedClassifierCV class.

1 Answers

You can train an isotonic regression a posteriori, after prediction. Let 'file1' be a csv containing your predictions pred and real observed events obs on a subset of data. Ideally, this subset has never been used before (not even in Keras training). Let file2 contain the predictions you want to calibrate (Keras predictions for the test set).

    import pandas as pd
    from sklearn.isotonic import IsotonicRegression
    never_seen=pd.read_csv('file1') 
    uncalibrated=pd.read_csv('file2')   
    
    ir = IsotonicRegression( out_of_bounds = 'clip' )   
    ir.fit( never_seen.pred,never_seen.obs )
    p_calibrated = ir.transform( uncalibrated.pred )
Related