'_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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pu239
Solution 2 enzo
Solution 3 Marcus