我正在尝试预测一个人是否患有某种疾病。通过在HTML页面上传递输入并能够预测值,但无法在HTML页面上显示精度。Error=“ TypeError:预期的序列或类似数组的内容,得到了“。下面是我的代码。请提供一些帮助问题发生了
from flask import Flask, redirect, url_for, request, render_template
import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingClassifier,RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,precision_score,recall_score,auc,roc_curve
app = Flask(__name__)
Data = pd.read_csv('data\\new_heart.csv')
y = Data['target']
Data.drop("target", axis=1, inplace=True)
X = Data
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=42)
Model = GradientBoostingClassifier(verbose=1, learning_rate=0.5,warm_start=True)
Model.fit(x_train, y_train)
y_pred=0
def generate_prediction(input):
y_pred = Model.predict(input)
return y_pred
@app.route('/')
def home():
return render_template('disease.html')
@app.route('/get_value', methods=['GET', 'POST'])
def get_price():
input = request.form
input = np.array(list(input.values())).reshape(1,-1)
print(input)
price = generate_prediction(input)
if price == 0:
return "Not Suffering from a disease {}".format(price,accuaracy())
else:
return "Suffering from a disease {}{}".format(price,accuaracy())
def accuaracy():
print("Accuracy(GradientBoostingClassifier)\t:" + str(accuracy_score(y_test, y_pred)))
print("Precision(GradientBoostingClassifier)\t:" + str(precision_score(y_test, y_pred)))
print("Recall(GradientBoostingClassifier)\t:" + str(recall_score(y_test, y_pred)))
if __name__ == '__main__':
app.run(debug=True)
disease.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action=" http://127.0.0.1:5000/get_value" method="POST">
<p>age <input type="text" name="t1"/></p>
<p>sex <input type="text" name="t2"/></p>
<p>cp <input type="text" name="t3"/></p>
<p>trestbps <input type="text" name="t4"/></p>
<p>chol <input type="text" name="t5"/></p>
<p>fbs <input type="text" name="t6"/></p>
<p>restecg <input type="text" name="t7"/></p>
<p>thalach <input type="text" name="t8"/></p>
<p>exang <input type="text" name="t9"/></p>
<p>oldspeak <input type="text" name="t10"/></p>
<p>slope <input type="text" name="t11"/></p>
<p>ca <input type="text" name="t12"/></p>
<p>thal <input type="text" name="t13"/></p>
<p><input type="submit" value="submit"/></p>
</form>
</body>
traceback (most recent call last):
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Indu\PycharmProjects\WebApp\HeartDisease.py", line 43, in get_price
return "Suffering from a disease {}{}".format(price,accuaracy())
File "C:\Users\Indu\PycharmProjects\WebApp\HeartDisease.py", line 52, in accuaracy
print("Accuracy(GradientBoostingClassifier)\t:" + str(accuracy_score(y_test, y_pred)))
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\sklearn\metrics\classification.py", line 176, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\sklearn\metrics\classification.py", line 71, in _check_targets
check_consistent_length(y_true, y_pred)
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\sklearn\utils\validation.py", line 231, in check_consistent_length
lengths = [_num_samples(X) for X in arrays if X is not None]
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\sklearn\utils\validation.py", line 231, in <listcomp>
lengths = [_num_samples(X) for X in arrays if X is not None]
File "C:\Users\Indu\PycharmProjects\WebApp\venv\lib\site-packages\sklearn\utils\validation.py", line 138, in _num_samples
type(x))
TypeError: Expected sequence or array-like, got <class 'int'>
答案 0 :(得分:0)
在您的accuracy()
函数中,您需要将数组传递给Sklearn指标。现在,您只传递一个int值。确保y_test
和y_pred
是数组,即使它们只是一个元素。
答案 1 :(得分:0)
由于input
的重塑为(-1,1),因此出现错误。由于y_pred = Model.predict(input)
中的形状,您将始终获得整数值。因为您将输入一维清单作为输入
因此要获取list
,您的输入应等于x_test
,这样您就可以获取y_pred
。
还请注意,在accuracy_score(y_test, y_pred)
中,两个参数的长度应相同