2024-10-13 16:53:07 +04:00
|
|
|
|
import pandas as pd
|
|
|
|
|
import joblib
|
|
|
|
|
from typing import List, Dict
|
|
|
|
|
from schemas.schemas import LaptopCreate, LaptopResponse, PredictPriceResponse
|
|
|
|
|
|
|
|
|
|
class LaptopService:
|
2024-10-30 01:16:58 +04:00
|
|
|
|
def __init__(self, model_path: str, feature_columns_path: str, poly_path: str, scaler_path: str):
|
2024-10-13 16:53:07 +04:00
|
|
|
|
try:
|
|
|
|
|
self.model = joblib.load(model_path)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
raise Exception(f"Model file not found at {model_path}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise Exception(f"Error loading model: {str(e)}")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.feature_columns = joblib.load(feature_columns_path)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
raise Exception(f"Feature columns file not found at {feature_columns_path}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise Exception(f"Error loading feature columns: {str(e)}")
|
|
|
|
|
|
2024-10-30 01:16:58 +04:00
|
|
|
|
try:
|
|
|
|
|
self.poly_transformer = joblib.load(poly_path)
|
|
|
|
|
self.scaler = joblib.load(scaler_path)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
raise Exception("Polynomial transformer or scaler file not found.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise Exception(f"Error loading polynomial transformer or scaler: {str(e)}")
|
|
|
|
|
|
2024-10-13 16:53:07 +04:00
|
|
|
|
def predict_price(self, data: Dict[str, any]) -> PredictPriceResponse:
|
|
|
|
|
# Преобразование данных в DataFrame
|
|
|
|
|
input_df = pd.DataFrame([data])
|
|
|
|
|
|
|
|
|
|
# Применение One-Hot Encoding к категориальным признакам
|
|
|
|
|
input_df = pd.get_dummies(input_df, columns=['processor', 'os'], drop_first=True)
|
|
|
|
|
|
2024-10-30 01:16:58 +04:00
|
|
|
|
# Добавление отсутствующих признаков
|
2024-10-13 16:53:07 +04:00
|
|
|
|
for col in self.feature_columns:
|
|
|
|
|
if col not in input_df.columns and col != 'price':
|
|
|
|
|
input_df[col] = 0
|
|
|
|
|
|
2024-10-30 01:16:58 +04:00
|
|
|
|
# Упорядочивание колонок
|
2024-10-13 16:53:07 +04:00
|
|
|
|
input_df = input_df[self.feature_columns]
|
|
|
|
|
|
2024-10-30 01:16:58 +04:00
|
|
|
|
# Преобразование с использованием PolynomialFeatures
|
|
|
|
|
input_poly = self.poly_transformer.transform(input_df)
|
|
|
|
|
|
|
|
|
|
# Масштабирование данных
|
|
|
|
|
input_scaled = self.scaler.transform(input_poly)
|
|
|
|
|
|
2024-10-13 16:53:07 +04:00
|
|
|
|
# Предсказание цены
|
2024-10-30 01:16:58 +04:00
|
|
|
|
predicted_price = self.model.predict(input_scaled)[0]
|
2024-10-13 16:53:07 +04:00
|
|
|
|
|
|
|
|
|
return PredictPriceResponse(predicted_price=round(predicted_price, 2))
|