my_project.prediction

  1from pathlib import Path
  2
  3import pandas as pd
  4import torch
  5
  6from my_project.train import create_model
  7
  8
  9PROJECT_ROOT = Path(__file__).resolve().parent.parent
 10
 11MODEL_PATH = PROJECT_ROOT / "models" / "automobile_model.pth"
 12DATA_PATH = PROJECT_ROOT / "data" / "processed" / "automobile_dataset.parquet"
 13
 14
 15def predict_price(year: int, engine_size: float, mileage: int, horsepower: float,
 16    torque: float, owners: int, accident_history: float, service_history: str,
 17    fuel_efficiency: float, make: str, model_name: str, fuel_type: str,
 18    transmission: str, color: str, body_type: str, drivetrain: str,
 19    location: str) -> float:
 20    """
 21    Train model and return loss history.
 22        
 23    Parameters
 24    ----------
 25    year: int
 26        Data necessary for the prediction, representing one feature with its same name.
 27    engine_size: float
 28        Data necessary for the prediction, representing one feature with its same name.
 29    mileage: int
 30        Data necessary for the prediction, representing one feature with its same name.
 31    horsepower: float
 32        Data necessary for the prediction, representing one feature with its same name.
 33    torque: float
 34        Data necessary for the prediction, representing one feature with its same name.
 35    owners: int
 36        Data necessary for the prediction, representing one feature with its same name.
 37    accident_history: float
 38        Data necessary for the prediction, representing one feature with its same name.
 39    service_history: str
 40        Data necessary for the prediction, representing one feature with its same name.
 41    fuel_efficiency: float
 42        Data necessary for the prediction, representing one feature with its same name.
 43    make: str
 44        Data necessary for the prediction, representing one feature with its same name.
 45    model_name: str
 46        Data necessary for the prediction, representing one feature with its same name.
 47    fuel_type: str
 48        Data necessary for the prediction, representing one feature with its same name.
 49    transmission: str
 50        Data necessary for the prediction, representing one feature with its same name.
 51    color: str
 52        Data necessary for the prediction, representing one feature with its same name.
 53    body_type: str
 54        Data necessary for the prediction, representing one feature with its same name.
 55    drivetrain: str
 56        Data necessary for the prediction, representing one feature with its same name.
 57    location: str
 58        Data necessary for the prediction, representing one feature with its same name.
 59        
 60    Returns
 61    -------
 62    prediction : float
 63        the selling price predicted
 64    """
 65    
 66    # Load processed training data to obtain
 67    # exactly the same feature columns.
 68    df = pd.read_parquet(DATA_PATH)
 69
 70    feature_columns = [
 71        column
 72        for column in df.columns
 73        if column != "Selling_Price"
 74    ]
 75
 76    # Create an empty row with all required features.
 77    input_data = pd.DataFrame(
 78        0,
 79        index=[0],
 80        columns=feature_columns
 81    )
 82
 83    # Numerical features
 84    input_data["Year"] = year
 85    input_data["Engine_Size"] = engine_size
 86    input_data["Mileage"] = mileage
 87    input_data["Horsepower"] = horsepower
 88    input_data["Torque"] = torque
 89    input_data["Owners"] = owners
 90    input_data["Accident_History"] = accident_history
 91    input_data["Service_History"] = service_history
 92    input_data["Fuel_Efficiency"] = fuel_efficiency
 93
 94    # One-hot categorical features
 95    categorical_values = {
 96        "Make": make,
 97        "Model": model_name,
 98        "Fuel_Type": fuel_type,
 99        "Transmission": transmission,
100        "Color": color,
101        "Body_Type": body_type,
102        "Drivetrain": drivetrain,
103        "Location": location,
104    }
105
106    for prefix, value in categorical_values.items():
107
108        column_name = f"{prefix}_{value}"
109
110        if column_name in input_data.columns:
111            input_data[column_name] = 1
112
113    # Convert to tensor
114    X = torch.tensor(
115        input_data.values,
116        dtype=torch.float32
117    )
118
119    # Create model
120    model = create_model(
121        input_data.shape[1]
122    )
123
124    # Load trained weights
125    model.load_state_dict(
126        torch.load(
127            MODEL_PATH,
128            map_location="cpu"
129        )
130    )
131
132    model.eval()
133
134    # Prediction
135    with torch.no_grad():
136        prediction = model(X).item()
137
138    return prediction
PROJECT_ROOT = PosixPath('/home/alumno/Desktop/git_repositories/SDOML-project')
MODEL_PATH = PosixPath('/home/alumno/Desktop/git_repositories/SDOML-project/models/automobile_model.pth')
DATA_PATH = PosixPath('/home/alumno/Desktop/git_repositories/SDOML-project/data/processed/automobile_dataset.parquet')
def predict_price( year: int, engine_size: float, mileage: int, horsepower: float, torque: float, owners: int, accident_history: float, service_history: str, fuel_efficiency: float, make: str, model_name: str, fuel_type: str, transmission: str, color: str, body_type: str, drivetrain: str, location: str) -> float:
 16def predict_price(year: int, engine_size: float, mileage: int, horsepower: float,
 17    torque: float, owners: int, accident_history: float, service_history: str,
 18    fuel_efficiency: float, make: str, model_name: str, fuel_type: str,
 19    transmission: str, color: str, body_type: str, drivetrain: str,
 20    location: str) -> float:
 21    """
 22    Train model and return loss history.
 23        
 24    Parameters
 25    ----------
 26    year: int
 27        Data necessary for the prediction, representing one feature with its same name.
 28    engine_size: float
 29        Data necessary for the prediction, representing one feature with its same name.
 30    mileage: int
 31        Data necessary for the prediction, representing one feature with its same name.
 32    horsepower: float
 33        Data necessary for the prediction, representing one feature with its same name.
 34    torque: float
 35        Data necessary for the prediction, representing one feature with its same name.
 36    owners: int
 37        Data necessary for the prediction, representing one feature with its same name.
 38    accident_history: float
 39        Data necessary for the prediction, representing one feature with its same name.
 40    service_history: str
 41        Data necessary for the prediction, representing one feature with its same name.
 42    fuel_efficiency: float
 43        Data necessary for the prediction, representing one feature with its same name.
 44    make: str
 45        Data necessary for the prediction, representing one feature with its same name.
 46    model_name: str
 47        Data necessary for the prediction, representing one feature with its same name.
 48    fuel_type: str
 49        Data necessary for the prediction, representing one feature with its same name.
 50    transmission: str
 51        Data necessary for the prediction, representing one feature with its same name.
 52    color: str
 53        Data necessary for the prediction, representing one feature with its same name.
 54    body_type: str
 55        Data necessary for the prediction, representing one feature with its same name.
 56    drivetrain: str
 57        Data necessary for the prediction, representing one feature with its same name.
 58    location: str
 59        Data necessary for the prediction, representing one feature with its same name.
 60        
 61    Returns
 62    -------
 63    prediction : float
 64        the selling price predicted
 65    """
 66    
 67    # Load processed training data to obtain
 68    # exactly the same feature columns.
 69    df = pd.read_parquet(DATA_PATH)
 70
 71    feature_columns = [
 72        column
 73        for column in df.columns
 74        if column != "Selling_Price"
 75    ]
 76
 77    # Create an empty row with all required features.
 78    input_data = pd.DataFrame(
 79        0,
 80        index=[0],
 81        columns=feature_columns
 82    )
 83
 84    # Numerical features
 85    input_data["Year"] = year
 86    input_data["Engine_Size"] = engine_size
 87    input_data["Mileage"] = mileage
 88    input_data["Horsepower"] = horsepower
 89    input_data["Torque"] = torque
 90    input_data["Owners"] = owners
 91    input_data["Accident_History"] = accident_history
 92    input_data["Service_History"] = service_history
 93    input_data["Fuel_Efficiency"] = fuel_efficiency
 94
 95    # One-hot categorical features
 96    categorical_values = {
 97        "Make": make,
 98        "Model": model_name,
 99        "Fuel_Type": fuel_type,
100        "Transmission": transmission,
101        "Color": color,
102        "Body_Type": body_type,
103        "Drivetrain": drivetrain,
104        "Location": location,
105    }
106
107    for prefix, value in categorical_values.items():
108
109        column_name = f"{prefix}_{value}"
110
111        if column_name in input_data.columns:
112            input_data[column_name] = 1
113
114    # Convert to tensor
115    X = torch.tensor(
116        input_data.values,
117        dtype=torch.float32
118    )
119
120    # Create model
121    model = create_model(
122        input_data.shape[1]
123    )
124
125    # Load trained weights
126    model.load_state_dict(
127        torch.load(
128            MODEL_PATH,
129            map_location="cpu"
130        )
131    )
132
133    model.eval()
134
135    # Prediction
136    with torch.no_grad():
137        prediction = model(X).item()
138
139    return prediction

Train model and return loss history.

Parameters
  • year (int): Data necessary for the prediction, representing one feature with its same name.
  • engine_size (float): Data necessary for the prediction, representing one feature with its same name.
  • mileage (int): Data necessary for the prediction, representing one feature with its same name.
  • horsepower (float): Data necessary for the prediction, representing one feature with its same name.
  • torque (float): Data necessary for the prediction, representing one feature with its same name.
  • owners (int): Data necessary for the prediction, representing one feature with its same name.
  • accident_history (float): Data necessary for the prediction, representing one feature with its same name.
  • service_history (str): Data necessary for the prediction, representing one feature with its same name.
  • fuel_efficiency (float): Data necessary for the prediction, representing one feature with its same name.
  • make (str): Data necessary for the prediction, representing one feature with its same name.
  • model_name (str): Data necessary for the prediction, representing one feature with its same name.
  • fuel_type (str): Data necessary for the prediction, representing one feature with its same name.
  • transmission (str): Data necessary for the prediction, representing one feature with its same name.
  • color (str): Data necessary for the prediction, representing one feature with its same name.
  • body_type (str): Data necessary for the prediction, representing one feature with its same name.
  • drivetrain (str): Data necessary for the prediction, representing one feature with its same name.
  • location (str): Data necessary for the prediction, representing one feature with its same name.
Returns
  • prediction (float): the selling price predicted