我是在网络扩展工具中创建机器学习模型的新手,所以基本上我正在尝试创建一个机器学习模型来帮助分析电子邮件是否是网络钓鱼电子邮件。但是当我尝试运行代码时它显示
后端错误:发生错误:“dict”对象没有属性 ‘较低’
有什么建议如何解决这个问题吗?
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 -
在这种情况下,必须验证功能:
feature_extraction.transform([incoming_mail]) model.predict(input_data_feature)
有必要验证从查询接收到的对象的键。当生成此类错误消息时,意味着它是必填字段,在这种情况下必须验证它是否存在,如果不存在:返回默认值,以便生成的结构满足要求。
很可能您在
input_mail
中的 str
调用中传递了错误类型的 dict
(预期:feature_extraction.transform([input_mail])
,实际:detect_phishing
)。
要解决这个问题,请确保您通过了
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])