Reputation: 11
I can't use my predict method when assigning my random forest regression model to user input in flask. It give me this error:
AttributeError: dict object has no attribute 'predict'
This is my app.py code:
from flask import Flask, render_template, request
import jsonify
import requests
import pickle
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import sklearn
import os
from sklearn.preprocessing import StandardScaler
app = Flask(__name__)
model = pickle.load(open('random_forest_regression_model.pkl', 'rb'))
@app.route('/', methods=['GET'])
def Home():
return render_template('index.html')
standard_to = StandardScaler()
@app.route('/api', methods=['POST'])
def predict():
Fuel_Type_Diesel = 0
if request.method == 'POST':
Year = int(request.form['Year'])
Present_Price = float(request.form['Present_Price'])
Kms_Driven = int(request.form['Kms_Driven'])
Kms_Driven2 = np.log(Kms_Driven)
Owner = int(request.form['Owner'])
Fuel_Type_Petrol = request.form['Fuel_Type_Petrol']
if Fuel_Type_Petrol == 'Petrol':
Fuel_Type_Petrol = 1
Fuel_Type_Diesel = 0
else:
Fuel_Type_Petrol = 0
Fuel_Type_Diesel = 1
Year = 2020 - Year
Seller_Type_Individual = request.form['Seller_Type_Individual']
if Seller_Type_Individual == 'Individual':
Seller_Type_Individual = 1
else:
Seller_Type_Individual = 0
Transmission_Mannual = request.form['Transmission_Mannual']
if Transmission_Mannual == 'Mannual':
Transmission_Mannual = 1
else:
Transmission_Mannual = 0
prediction = model.predict([[Present_Price, Kms_Driven2, Owner, Year, Fuel_Type_Diesel, Fuel_Type_Petrol,
Seller_Type_Individual, Transmission_Mannual]])
output = round(prediction[0], 2)
if output < 0:
return render_template('index.html', prediction_text="Sorry you cannot sell this car")
else:
return render_template('index.html', prediction_text="You Can Sell The Car at {}".format(output))
else:
return render_template('index.html')
if __name__ == '__main__':
app.run(port=5000, debug=True)
My HTML code (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="color:red">
<form action="{{ url_for('predict')}}" method="post">
<h2>Predictive analysis</h2>
<h3>Year</h3>
<input id="first" name="Year" type="number ">
<h3>What is the Showroom Price?(In lakhs)</h3><br><input id="second" name="Present_Price" required="required">
<h3>How Many Kilometers Drived?</h3><input id="third" name="Kms_Driven" required="required">
<h3>How much owners previously had the car(0 or 1 or 3) ?</h3><br><input id="fourth" name="Owner" required="required">
<h3>What Is the Fuel type?</h3><br><select name="Fuel_Type_Petrol" id="fuel" required="required">
<option value="Petrol">Petrol</option>
<option value="Diesel">Diesel</option>
<option value="Diesel">CNG</option>
</select>
<h3>Are you A Dealer or Individual</h3><br><select name="Seller_Type_Individual" id="resea" required="required">
<option value="Dealer">Dealer</option>
<option value="Individual">Individual</option>
</select>
<h3>Transmission type</h3><br><select name="Transmission_Mannual" id="research" required="required">
<option value="Mannual">Manual Car</option>
<option value="Automatic">Automatic Car</option>
</select>
<br><br><button id="sub" type="submit ">Calculate Selling Price</button>
<br>
</form>
<br><br><h3>{{ prediction_text }}<h3>
</div>
</body>
</html>
Error
line 70, in predict
prediction = model.predict([[Present_Price, Kms_Driven2, Owner, Year, Fuel_Type_Diesel, Fuel_Type_Petrol,
AttributeError: type object 'dict' has no attribute 'predict'
Line 70
prediction = model.predict([[Present_Price, Kms_Driven2, Owner, Year,
Fuel_Type_Diesel, Fuel_Type_Petrol,Seller_Type_Individual,
Transmission_Mannual]])
Upvotes: 0
Views: 1897
Reputation: 1180
It appears the object saved in the file random_forest_regression_model.pkl
is a dictionary. The data in that file seems to be wrong. You can try confirming that by printing type(model)
after the line at the beginning where you load it using pickle.
You should create an object of the type that you actually want model
to be and save it in that file. Then try running again.
Upvotes: 1