簡體   English   中英

AttributeError: dict object 沒有屬性“預測”

[英]AttributeError: dict object has no attribute 'predict'

將我的隨機森林回歸 model 分配給 flask 中的用戶輸入時,我無法使用我的預測方法。 它給了我這個錯誤:

AttributeError: dict object has no attribute 'predict' 

這是我的 app.py 代碼:

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)

我的 HTML 代碼 (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>

錯誤

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'

70號線

prediction = model.predict([[Present_Price, Kms_Driven2, Owner, Year, 
             Fuel_Type_Diesel, Fuel_Type_Petrol,Seller_Type_Individual, 
             Transmission_Mannual]])

看來保存在文件random_forest_regression_model.pkl是一個字典。 該文件中的數據似乎是錯誤的。 您可以嘗試通過在使用 pickle 加載它的開頭的行之后打印type(model)來確認這一點。

您應該創建一個您實際希望model成為的類型的 object 並將其保存在該文件中。 然后再次嘗試運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM