Я работаю над проектом машинного обучения, который прогнозирует цены на электромобили в разных штатах США. Моя цель – закрепить свои практические навыки. Я сделал все в проекте, например, выполнил горячее кодирование, обучил модель и запустил приложение Flask на локальном хосте.
На локальном хосте я заполнил форму следующими значениями, а затем нажал кнопку «Отправить». кнопка:
Код: Выделить всё
County: Jefferson
City: PORT TOWNSEND
ZIP Code: 98368
Model Year: 2012
Make: NISSAN
Model: LEAF
Electric Vehicle Type: Battery Electric Vehicle (BEV)
CAFV Eligibility: Clean Alternative Fuel Vehicle Eligible
Legislative District: 24
После отправки формы я получаю следующую ошибку:
Код: Выделить всё
ValueError
ValueError: Found unknown categories \['98368'\] in column 2 during transform
Traceback (most recent call last)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 1498, in __call__
return self.wsgi_app(environ, start_response)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 1476, in wsgi_app
response = self.handle_exception(e)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\flask\\app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions\[rule.endpoint\])(\*\*view_args) # type: ignore\[no-any-return\]
File "G:\\Machine_Learning_Projects\\austin\\electric_vehicle_price_prediction_2\\app\\routes.py", line 38, in predict
price = predict_price(features)
File "G:\\Machine_Learning_Projects\\austin\\electric_vehicle_price_prediction_2\\app\\model.py", line 29, in predict_price
transformed_features = encoder.transform(features_df)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\sklearn\\utils_set_output.py", line 157, in wrapped
data_to_wrap = f(self, X, \*args, \*\*kwargs)
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\sklearn\\preprocessing_encoders.py", line 1027, in transform
X_int, X_mask = self.\_transform(
File "C:\\Users\\austin.conda\\envs\\electric_vehicle_price_prediction_2\\lib\\site-packages\\sklearn\\preprocessing_encoders.py", line 200, in \_transform
raise ValueError(msg)
ValueError: Found unknown categories \['98368'\] in column 2 during transform\
Я пробовал использовать следующий код:
Код файла Routes.py внутри папки приложения:
Код: Выделить всё
from flask import render_template, request, jsonify
from app import app
from app.model import predict_price
from jinja2 import Environment, FileSystemLoader, PackageLoader, select_autoescape
@app.route('/')
def index():
env = Environment(
loader=PackageLoader("app"),
autoescape=select_autoescape()
)
template = env.get_template("index.html")
return render_template(template)
@app.route('/predict', methods=\['POST'\])
def predict():
data = request.form.to_dict()
# Convert the form data into the correct format for prediction
features = [
data['county'],
data['city'],
data['zip_code'],
data['model_year'],
data['make'],
data['model'],
data['ev_type'],
data['cafv_eligibility'],
data['legislative_district']
]
# Get the prediction result
price = predict_price(features)
return jsonify({'predicted_price': price})
Код: Выделить всё
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestRegressor
import joblib
from flask import Flask, render_template
from jinja2 import Environment, FileSystemLoader, PackageLoader, select_autoescape
env = Environment(
loader=PackageLoader("app"),
autoescape=select_autoescape()
)
model = joblib.load('model/ev_price_model.pkl')
def predict_price(features):
encoder = joblib.load('model/encoder.pkl') # Load encoder if needed
features_df = pd.DataFrame([features], columns=['County', 'City', 'ZIP Code', 'Model Year', 'Make', 'Model', 'Electric Vehicle Type', 'Clean Alternative Fuel Vehicle (CAFV) Eligibility', 'Legislative District'])
# Apply encoding, scaling, etc., if necessary
transformed_features = encoder.transform(features_df)
# Make the prediction
price = model.predict(transformed_features)
return price[0] # Assuming it returns a single value
Вот ссылка на мой репозиторий:
https://github.com/SteveAustin583/elect ... prediction
Чего я ожидал?
Я ожидал получить предсказание результат без проблем. Потому что я уже выполнил горячее кодирование.
Можете ли вы помочь мне решить эту проблему?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ting-the-p