用于sklearn分类模型的pickle文件的不同结果

时间:2018-05-26 16:21:39

标签: python scikit-learn classification pickle plotly-dash

我正在创建一个带破折号的仪表板,用于sklearn分类模型。 我想绘制roc曲线和一些指标来评估模型(like is shown in this image)。

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

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

  • 或者只使用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模型吗?有什么建议吗?

0 个答案:

没有答案