0

我正在为 sklearn 分类模型创建一个带有破折号的仪表板。我想绘制 roc 曲线和一些指标来评估模型(如图所示)。

问题是破折号不允许超过一个输出,所以我有两个解决方案:

  • 我再次生成模型(这将花费大量时间)

  • 或者只是使用 pickle 来保存 sklearn 生成的模型。但是当我加载腌制模型时,结果与图像中显示的结果不同,例如:原始 AUC(曲线下面积)=0.7 和来自选取的文件 =0.93。我尝试对 joblib 做同样的事情,但同样的问题。

请原谅我这是一个代码示例,因为它很长:

    @app.callback(Output('modelReport', 'rows'),
                  [Input('report', 'n_clicks'),
                  Input('model', 'value'),])
    def modelClassifierReport (button, mod): 
        if (button ==None):
            return [{}]
        else:
 ## saving and generating models ###   
            if (mod == 'logreg'):
                Title='Logistic regression'
                logreg = LogisticRegression()
                logreg.fit(x_train,y_train)
                model=logreg
                with open("python_logreg_model.pkl", "wb") as file_handler:
                    pickle.dump(logreg, file_handler)

            elif (mod =='mlp' ):
                mlp=MLPClassifier()
                mlp=mlp.fit(x_train, y_train)
                with open("python_mlp_model.pkl", "wb") as file_handler:
                    pickle.dump(mlp, file_handler)
                model=mlp
            elif :
                .......#other models#

            ####------------###
            #### comput indicator to evaluate models ####
            ####------------###

            report=pd.DataFrame({'creteria':['Accuracy','erreur I','erreurII' ,'AUC','CV ACU','AIC','som error']})
            report['Value']=[model_score,fnr,fpr,AUC, cv_mean,AIC,RSS]
            return  report.to_dict('records')
    #####################
    @app.callback(Output('my-graph', 'figure'),
                  [Input ('roc','n_clicks'),])

    def RocPlot (button):
        if (button ==None):
            return [{}]
        else:
### loading models 
            if(mod == 'logreg'):
                with open("python_logreg_model.pkl", "rb") as file_handler:
                    model = pickle.load(file_handler)
            elif (mod=='mlp' ):
                with open("python_mlp_model.pkl", "rb") as file_handler:
                    model = pickle.load(file_handler)
            #### load other models ####
            ####------------###
            ####------------###
            fp, tp, threshold= metrics.roc_curve(y_test, model.predict_proba(x_test)[:,1])
            AUC= metrics.auc(fp, tp)
            lw = 2
            trace1 = go.Scatter(x=fp, y=tp, 
                                mode='lines', 
                                line=dict(color='darkorange', width=lw),
                                name='ROC curve (area = %0.2f)' % AUC )
            trace2 = go.Scatter(x=[0, 1], y=[0, 1], 
                                mode='lines', 
                                line=dict(color='navy', width=lw, dash='dash'),
                                showlegend=False)
            layout = go.Layout(title='Receiver operating characteristic example',
                                xaxis=dict(title='False Positive Rate'),
                                yaxis=dict(title='True Positive Rate'))
            print('plot done')
            return{ 'data': [trace1, trace2], 'layout': layout }

我如何用腌制模型解决这个问题?还有其他方法可以保存 sklearn 模型吗?有什么建议吗?

4

0 回答 0