python - 'list' 对象没有属性 'predict'
问题描述
我想使用烧瓶部署乳腺癌检测 ml 模型。这是错误:
File "C:\Users\sakshi sanket\Desktop\Breastcancer\app.py", line 22, in predict
output = model.predict(df)
AttributeError: 'list' object has no attribute 'predict'
这是代码:
应用程序.py
import numpy as np
import pandas as pd
from flask import Flask, request, render_template
import pickle
app = Flask(__name__, template_folder="template")
model = pickle.load(open('breast_cancer_detector.pickle', 'rb'))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
input_features = [float(x) for x in request.form.values()]
features_value = [np.array(input_features)]
features_name = ['id', 'diagnosis', 'radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean', 'radius_se', 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se', 'compactness_se', 'concavity_se', 'concave points_se', 'symmetry_se', 'fractal_dimension_se', 'radius_worst', 'texture_worst', 'perimeter_worst', 'area_worst', 'smoothness_worst', 'compactness_worst', 'concavity_worst', 'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst']
df = pd.DataFrame(features_value, columns=features_name)
output = model.predict(df)
if output == 0:
res_val = "** breast cancer **"
else:
res_val = "no breast cancer"
return render_template('index.html', prediction_text='Patient has {}'.format(res_val)
if __name__ == "__main__":
app.run(port='8080', debug=True)
请帮我。
解决方案
试试output = model[0].predict(df)
。由于它是一个列表,使用 [0] 应该引用单个项目。
推荐阅读
- python - TypeError:train_test_split() 得到了一个意外的关键字参数“test_size”
- javascript - 只有当我使用 IE11 时才会触发事件 addUserSignedOut
- physics - 为什么我的模拟只计算一定数量的数字,然后才改变功率大小?
- ansible - 如何通过在 localhost 上运行 playbook 来获取 inventory_hostanme 和 ansible_ssh_hosts 信息
- java - 在我的 Java 合并排序功能中,即使条件为假,它也会进入循环。为什么?
- python - 在 Pandas 中计算有条件的行
- django - 访问 Django html 模板中的模型字段
- php - 在 PHP 中保存和恢复一个类作为一种优化技术?
- ios - 对于每行有两个卡片单元的 UI,哪种布局更好?
- java - 不能用Hibernate调用PostgreSQL的11个存储过程