后端错误:'dict'对象没有'lower'属性
我刚开始学习如何在网页扩展工具中创建机器学习模型,简单来说,我想做一个机器学习模型,用来分析电子邮件,判断它是不是钓鱼邮件。但是当我尝试运行代码时,出现了这样的错误:
后端错误:发生了一个错误:'dict'对象没有'lower'这个属性
有没有什么建议可以解决这个问题?
dataset = pd.read_csv("mail_data.csv")
mail_data = dataset.where((pd.notnull(dataset)), '')
mail_data.loc[mail_data['Category'] == 'spam', 'Category',] = 0
mail_data.loc[mail_data['Category'] == 'authentic', 'Category',] = 1
mail_data.dropna(subset=['Message'], inplace=True)
X = mail_data['Message'].astype(str).tolist()
Y = mail_data['Category']
feature_extraction = TfidfVectorizer(min_df=1, stop_words='english', lowercase=True)
X_features = feature_extraction.fit_transform(X)
Y = Y.astype('int')
model = LogisticRegression()
model.fit(X_features, Y)
--
def detect_phishing(input_mail):
input_data_feature = feature_extraction.transform([input_mail])
prediction = model.predict(input_data_feature)
return prediction[0]
--
@app.route('/predict', methods=['POST'])
def predict_phishing():
if request.method == 'POST':
try:
payload = request.json
email_content = payload.get('email_content', None) # Assuming 'email_content' is the key in your JSON payload
print('Received data:', email_content)
# Call detect_phishing function
prediction = detect_phishing(email_content)
return jsonify({'prediction': prediction[0]})
except Exception as e:
app.logger.error(f'An error occurred: {str(e)}')
app.logger.info(traceback.format_exc())
return jsonify({'error': 'An error occurred'}), HTTPStatus.INTERNAL_SERVER_ERROR
else:
return jsonify({'error': 'Invalid method'}), HTTPStatus.METHOD_NOT_ALLOWED
错误代码
ERROR in backend: An error occurred: 'dict' object has no attribute 'lower'
INFO in backend: Traceback (most recent call last): line 80, in predict_phishing
line 60, in detect_phishing
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 2162, in transform
X = super().transform(raw_documents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 1434, in transform
_, X = self._count_vocab(raw_documents, fixed_vocab=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 1276, in _count_vocab
for feature in analyze(doc):
^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 110, in _analyze
doc = preprocessor(doc)
^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\sklearn\feature_extraction\text.py", line 68, in _preprocess
doc = doc.lower()
^^^^^^^^^
AttributeError: 'dict' object has no attribute 'lower'
127.0.0.1 - - [11/Mar/2024 02:36:05] "POST /predict HTTP/1.1" 500 -
1 个回答
0
很可能你在调用 detect_phishing
函数里的 feature_extraction.transform([input_mail])
时,传入的 input_mail
类型不对(应该是 str
类型,但实际上是 dict
类型)。
要解决这个问题,确保你传入的是一个 str
类型的值:
if not isinstance(input_mail, str):
raise TypeError('Expected str for input_mail, got: %s' % type(input_mail))
input_data_feature = feature_extraction.transform([input_mail])