-2
import numpy as np from flask import Flask, request, jsonify, render_template import pickle

#procfile.txt #web: gunicorn app:app #first file that we have to run first : flask server name app = Flask(name)

pkl_file = open('model.pkl','rb') lm = pickle.load(open('model.pkl', 'rb')) index_dict = pickle.load(pkl_file)

@app.route('/') def home():

return render_template('index.html')
@app.route('/predict',methods=['POST']) def predict():

if request.method=='POST':
    result = request.form

    index_dict = pickle.load(open('cat','rb'))
    Address_cat = pickle.load(open('Address_cat','rb'))

    new_vector = np.zeros(5)

    result_Address = result['Address']

    if result_Address not in Address_cat:
        new_vector[4] = 1
    else:
        new_vector[index_dict[str(result['Address'])]] = 1


    new_vector[index_dict[str(result['area'])]] = 1

    new_vector[0] = result['Avg_Area_Income']
    new_vector[1] = result['Avg_Area_House_Age']
    new_vector[2] = result['Avg_Area_Number_of_Rooms']
    new_vector[3] = result['Avg_Area_Number_of_Bedrooms']
    new_vector[4] = result['Area_Population']

new = [new_vector]

prediction = lm.predict(new)
#print(prediction)

return render_template('index.html', Predict_score ='Your house estimate price is  '.format(prediction))
if name == "main": app.run(debug=True)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • 1
    It means that your `result` dictionary doesn't contain the key `area`. So it seems either your form does not contain an input with that name, or it is not being sent from the browser when the form is submitted. – snakecharmerb Apr 19 '22 at 10:50

1 Answers1

0

In this line:

new_vector[index_dict[str(result['area'])]] = 1

you try to get the value from the dictionary result, that has the key 'area'. If it does not exist, a KeyError is thrown.

You can either check if the key exists (LBYL):

if 'area' in result:
    new_vector[index_dict[str(result['area'])]] = 1
else:
    ...

or try to access it and catch the exception (EAFP):

try:
    new_vector[index_dict[str(result['area'])]] = 1
except KeyError:
    ...

or use a fallback value, that is used, when the key is not found:

new_vector[index_dict[str(result.get('area', 0))]] = 1

Note that the latter is most probably not what you want and a likely source of another error.

Corbie
  • 819
  • 9
  • 31