1

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?

Agni Sain
  • 11
  • 2
  • Hey! Thanks for the support, but I had to change the pkl file with some other model. I got late with the project and could do any more harm to the deadline. Sorry for late reply. – Agni Sain May 12 '21 at 04:48
  • Yes, I did check your answer. The problem caused was mostly due to the version of xgboost. – Agni Sain May 13 '21 at 06:03
  • I have to switch to gradientboostingclassifier, n it worked fine. – Agni Sain May 13 '21 at 06:03

3 Answers3

3

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.

pu239
  • 707
  • 7
  • 17
0

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

Marcus
  • 69
  • 1
  • 4
-1

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
enzo
  • 9,861
  • 3
  • 15
  • 38
top talent
  • 615
  • 4
  • 17