incorrect prediction Python ML

穿精又带淫゛_ 提交于 2020-06-17 09:07:34

问题


I want to get correct prediction result. Prediction is based on 5 values. When I had a 5 inputs with one values in every input, prediction was correct, but after changing structure to 1 input with 5 value in it, prediction became incorrect. I think it is due to select2 library. Maybe the other 4 values is not recognized, that`s why prediction is incorrect, but I don't fully understand.

Code:

app.py:

import os
import csv
import prediction
from flask import Flask, jsonify, make_response, render_template, request, redirect, url_for
import pandas as pd

app = Flask(__name__)   

with open('templates/Testing.csv') as f:
        reader = csv.reader(f)
        values = next(reader)
        values = values[:len(values)-1]

@app.route('/', methods=['GET'])
def page_show():
    return render_template('includes/default.html', values=values)

@app.route('/', methods=['POST'])
def identify():
    selected_values = []
    if request.form['value']!='' and request.form['value'] not in selected_values:
        selected_values.append(request.form['value'])

    prognosis = prediction.input_check(selected_values)
    return render_template('identify.html', prognosis=prognosis, values=values)


if __name__ == '__main__':
    app.run()

default.html:

<form name="value" method="POST" action="/">  
  <div class="wrap">
     <div class="search">
        <select id="values" name="value" multiple="multiple" style="width: 80%;">
          {% for val in values %}
            <option value="{{val}}">{{val}}</option>
          {% endfor %}
        </select>
    <button type="submit" class="searchButton" name=form>
          <i class="fa fa-search"></i>
    </button>
</div></div></form>

<script>
$(document).ready(function() {
    $('#values').select2();
});
</script>

prediction.py:

from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import csv,numpy as np,pandas as pd
import os


data = pd.read_csv(os.path.join("templates", "Training.csv"))
df = pd.DataFrame(data)
cols = df.columns
cols = cols[:-1]
x = df[cols]
y = df['prognosis']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)

dt = DecisionTreeClassifier()
clf_dt=dt.fit(x_train,y_train)

indices = [i for i in range(137)]
values = df.columns.values[:-1]

dictionary = dict(zip(values,indices))


def input_check(value):
    user_input_values = value
    user_input_label = [0 for i in range(137)]
    for i in user_input_values:
        idx = dictionary[i]
        user_input_label[idx] = 1

    user_input_label = np.array(user_input_label)
    user_input_label = user_input_label.reshape((-1,1)).transpose()
    return(dt.predict(user_input_label))

identify.html:

<p><b>{{ prognosis[0] }}</b></p>  

Please help to solve this issue. Thanks.

来源:https://stackoverflow.com/questions/62004461/incorrect-prediction-python-ml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!