简体   繁体   中英

_pickle.UnpicklingError: NEWOBJ class argument isn't a type object

I ran this code on cmd and I face this error. _pickle.UnpicklingError: NEWOBJ class argument isn't a type object

my app.py goes like this:

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 21 19:18:42 2021

@author: Agni Sain
"""
from flask import Flask,request,render_template
import pandas as pd
import numpy as np
import pickle

app=Flask(__name__)
with open('xgb.pkl','rb') as f:
    xgb=pickle.load(f)

@app.route('/')    
def home():
    return render_template("index.html")   

    
@app.route('/predict',methods=["POST"])
def predict_heart_disease():
    
    age=request.form['age']
    sex=request.form['sex']
    cp = request.form['cp']
    trestbps = request.form['trestbps']
    chol = request.form['chol']
    fbs = request.form['fbs']
    restecg = request.form['restecg']
    thalach = request.form['thalach']
    exang = request.form['exang']
    oldpeak = request.form['oldpeak']
    slope = request.form['slope']
    ca = request.form['ca']
    thal = request.form['thal']
    
    pvalues=[[age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal]]
    pvalues=np.array(pvalues).reshape((1,-1))
    pred=xgb.predict(pvalues)
    predf=float(pred)
    return render_template('result.html', data=predf)

@app.route('/predict_file',methods=["POST"])    
def predict_heart_disease_file():
    df_test=pd.read_csv(request.files.get("file"))
    
    prediction=xgb.predict(df_test)
    return str((list(prediction)))
    
if (__name__=='__main__'):
    app.run()

I did read about this error here UnpicklingError: NEWOBJ class argument isn't a type object , but didn't find any such help from it. Can anyone put it simply why is this error coming and how to resolve it?

The reason, as discussed in this thread is that since XGBClassifier directly interacts with the scikit-learn API, that needs to be installed on the remote server as well. If you use the xgboost.booster class (native xgboost model, not scikit-learn type model), it would work fine.
Also, saving and loading as per the model I/O page should work without errors.
The other 'answer' is incorrect, the error does not originate from the dataframe being empty/None.

Make sure you're using the same scikit-learn version when creating the XGBClassifier before pickling and when unpickling

you need to add if statement

df_test=pd.read_csv(request.files.get("file"))
if not df_test.empty:
   prediction=xgb.predict(df_test)
   return str((list(prediction)))
else:
   return None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM