我正在创建一个使用鸢尾花数据集预测鸢尾花种类的应用程序。我正在使用Flask构建应用程序。我给应用程序提供了不同的输入,但是与kaggle模型相比,得到的结果却有所不同。我相信代码的机器学习部分没有任何问题,因为我已经在Kaggle上执行了它,并且不同的输入导致了不同的预测输出。我将机器学习与HTML集成后,似乎不了解发生了什么以及为什么输出没有改变。因为我是Flask的初学者,所以如果将说明保持简单,将不胜感激。
```
from flask import Flask, render_template, url_for, request, redirect
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
from forms import InputForm
from io import StringIO
import pickle
app = Flask(__name__)
app.config['SECRET_KEY'] = '0dfd576186d78d2318e8c2987d56ec42'
@app.route('/')
def home():
form = InputForm()
return render_template('home.html', form=form)
@app.route('/predict', methods=['GET', 'POST'])
def predict():
icd = pd.read_csv("F:\Alekhya\Projects\IrisProject\data\irisdata.csv")
features = ['sepal_length_cm', 'sepal_width_cm', 'petal_length_cm', 'petal_width_cm']
X = icd[features]
y = icd['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42, stratify=y)
knn = KNeighborsClassifier(n_neighbors=7)
knn.fit(X_train,y_train)
pickle.dump(knn, open('model.pkl','wb'))
model = pickle.load(open('model.pkl', 'rb'))
if request.method == 'POST':
pl = request.form['pl']
sl = request.form['sl']
pw = request.form['pw']
sw = request.form['sw']
df = pd.DataFrame({'sepal_length_cm': [sl], 'sepal_width_cm': [sw], 'petal_length_cm':[pl], 'petal_width_cm':[pw]})
prediction = model.predict(df)
return render_template('prediction.html', prediction=prediction[0])
这是我的Home.html
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action="{{ url_for('predict') }}">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Type the measurements in cm</legend>
<div class="form-group">
{{ form.Petal_length.label(class="form-control-label") }}
{% if form.Petal_length.errors %}
{{ form.Petal_length(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.Petal_length.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
<input name="pl" class="form-control form-control-lg" type="text"></input>
{% endif %}
</div>
<div class="form-group">
{{ form.Sepal_length.label(class="form-control-label") }}
{% if form.Sepal_length.errors %}
{{ form.Sepal_length(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.Sepal_length.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
<input name="sl" class="form-control form-control-lg" type="text"v></input>
{% endif %}
</div>
<div class="form-group">
{{ form.Petal_width.label(class="form-control-label") }}
{% if form.Petal_width.errors %}
{{ form.Petal_width(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.Petal_width.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
<input name="pw" class="form-control form-control-lg" type="text"></input>
{% endif %}
</div>
<div class="form-group">
{{ form.Sepal_width.label(class="form-control-label") }}
{% if form.Sepal_width.errors %}
{{ form.Sepal_width(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.Sepal_width.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
<input name="sw" class="form-control form-control-lg" type="text"></input>
{% endif %}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
<div class="p-3 mb-2 bg-dark text-white">
{{ prediction }}
</div>
</div>
{% endblock content %}
这是我的预测。html
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<div class="form-group">
<div class="p-3 mb-2 bg-dark text-white">
{{ prediction }}
</div>
</div>
<div class="form-group">
<button class="btn btn-outline-info"><a href={{ url_for('home') }}>Go Back</a></button>
</div>
</div>
{% endblock content %}