【问题标题】:AttributeError: 'str' object has no attribute 'predict'AttributeError:“str”对象没有属性“predict”
【发布时间】:2022-07-05 18:54:47
【问题描述】:

我正在尝试使用 Flask 部署基于 NLP 的垃圾邮件检测模型。 下面是我的 app.py 代码

import numpy as np
import pandas as pd
import nltk
import re
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')

from nltk.corpus import stopwords

stop_words=stopwords.words('english')

#词形还原

from nltk.stem import WordNetLemmatizer
lemmatizer=WordNetLemmatizer()
from flask import Flask,request,jsonify,render_template,escape
import pickle
import joblib

model = joblib.load('final_pickle_model.pkl')
model ='final_pickle_model.pkl'
app=Flask(__name__,template_folder='template')

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

@app.route('/prediction')
def prediction():
    return render_template('prediction.html')

@app.route('/prediction',methods=[ 'POST'])

def predict():
'''
For rendering results on HTML GUI
'''
int_features=[str(x) for x in request.form.values()]
a=int_features

msg=str(a)

filter_sentence=''

sentence=re.sub(r'[^\w\s]','',msg) #cleaning

words=nltk.word_tokenize(sentence)#tokenize

words=[w for w in words if not w in stop_words]

for word in words:
    filter_sentence=filter_sentence + ' ' +str(lemmatizer.lemmatize(word)).lower()


    data=(filter_sentence)

print(data)


my_prediction=model.predict(data)
my_prediction=int(my_prediction)
print(my_prediction)

if my_prediction==1:
    print("This tweet is real")
    return render_template('prediction.html',prediction_text="This tweet is real")

else:
    print("This tweet is spam")
    return render_template('prediction.html', prediction_text="This tweet is spam")

if __name__=="__main__":
     app.run(debug=True)

如果我只运行我的 ML 模型,它可以完美运行而不会出错。但是当我使用烧瓶(上面的代码)部署它并输入文本并按下预测按钮时,我收到以下错误:- AttributeError: 'str' 对象没有属性 'predict'。

如何解决这个错误

【问题讨论】:

  • 你问错问题了。它应该是“为什么我在这里有一个str,我的代码需要一个predict() 成员?”作为这里的新用户,也请使用tour 并阅读How to Ask。此外,确保提取并提供minimal reproducible example,包括它产生的输出。你的问题特别缺乏回溯。
  • 你可以尝试格式化你的格式predict功能更好,很难阅读

标签: python flask


【解决方案1】:
model = joblib.load('final_pickle_model.pkl')
model ='final_pickle_model.pkl'

您的model 变量似乎被重新定义为str。这就是错误发生的原因,也许你可以把这个model换个名字?

【讨论】:

    【解决方案2】:

    我想你的问题在这里:

    model = joblib.load('final_pickle_model.pkl') <-- looks correct
    model ='final_pickle_model.pkl' <-- looks out of place (probably a mistake?)
    

    您已将model 定义为一个简单的字符串,显然字符串没有predict() 方法。

    【讨论】:

      【解决方案3】:

      看我不知道如何解决它,但我认为错误是'str'(字符串模块)没有预定义的函数'predict',所以函数'predict'不能与字符串变量一起使用

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      • 这与问题完全无关,并且在许多层面上都是错误的。 str 不是模块。 predict 是某种类型的方法,而不是函数。请注意,我们希望得到能够真正回答问题而不是猜测的消息灵通的答案。
      【解决方案4】:

      'str' 对象没有属性'predict'--> 我有同样的错误 模型 = pickle.load(open('CarPrice_prediction_Model.pkl','rb'))

      prediction = model.predict(pd.DataFrame([[model, company, year, k_traveled, f_type]],data=np.array(['name','company','year','kms_driven', 'fuel_type']).reshape(1,5)))

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-10
        • 2021-10-04
        • 2019-12-02
        • 2021-09-25
        相关资源
        最近更新 更多