xgboost.core.XGBoostError:不支持 Unicode

问题描述

我正在使用 xgboost 制作一个心脏保护程序,它运行良好,直到我继续部署。我错过了 requirements.txt 文件中的 xgboost,添加了它,然后部署了它。 I 现在 heroku 日志显示 _pickle.UnpicklingError: NEWOBJ class argument isn't a type object。我运行 app.py 文件(包含烧瓶),它在进行预测时向我显示了这一点。

xgboost.core.XGBoostError: [22:26:04] c:\users\administrator\workspace\xgboost-win64_release_1.4.0\src\data\array_interface.h:352: Unicode is not supported. 127.0.0.1 - - [23/Apr/2021 22:26:04] "POST /predict HTTP/1.1" 500 -

我的 xgb.pkl 文件就是这样制作的

#Extreme Gradient Boost
xgb = XGBClassifier(learning_rate=0.01,n_estimators=25,max_depth=15,gamma=0.6,subsample=0.52,colsample_bytree=0.6,seed=27,reg_lambda=2,booster='dart',colsample_bylevel=0.6,colsample_bynode=0.5)

xgb.fit(X_train,y_train)
xgb_pred = xgb.predict(X_test)
xgb_con_matrix = confusion_matrix(y_test,xgb_pred)
xgb_acc = accuracy_score(y_test,xgb_pred)
print("Confusion Matrix\n",xgb_con_matrix)
print('\n')
print("Accuracy of Extreme Gradient Boost:",xgb_acc*100,'\n')
print(classification_report(y_test,xgb_pred))

import pickle
file='xgb.pkl'
with open(file,'wb') as f:
  pickle.dump(xgb,f)

我的 app.py 看起来像这样:

# -*- 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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)