To get a price prediction from your trained model, you need to follow these steps:
🔧 Step-by-Step Process to Predict a New Car's Price
-
Prepare a New Data Row
Create a new row of data (e.g., a dictionary or DataFrame) with the same features as your training dataset.
Example:
new_data = {
"Car_Name": "Geely Emgrand X70",
"Year": 2020,
"Kms_Driven": 15000,
"Fuel_Type": "Petrol",
"Selling_Price": None # This is what we want to predict
}
Important:
- Ensure the new data includes all features used during training (e.g.,
Year, Kms_Driven, Fuel_Type).
- If the target column (
Selling_Price) is missing, the model will predict it.
- Load the Trained Model
Use joblib to load the saved model:
import joblib
model = joblib.load("best_price_model.joblib")
-
Preprocess the New Data
Apply the same preprocessing (scaling, encoding) as during training.
Example:
# Convert the new data into a DataFrame
new_df = pd.DataFrame([new_data])
# Apply the same preprocessing pipeline
preprocessed_new = model.named_steps["prep"].transform(new_df)
Note:
- The
preprocess pipeline (from the notebook) handles:
- Scaling numeric features (
StandardScaler).
- Encoding categorical features (
OneHotEncoder).
-
Make the Prediction
Use the model to predict the price:
predicted_price = model.named_steps["model"].predict(preprocessed_new)
print(f"Predicted Price: ₹{predicted_price[0]:,.2f}")
Output Example:
Predicted Price: ₹6,50,000.00
🧠 Key Considerations
-
Feature Consistency:
The new data must match the exact structure of the training data (same columns, data types, and categories). For example:
- If
Fuel_Type has categories like Petrol, Diesel, or CNG, the new data must use the same labels.
- If a category is missing (e.g.,
LPG), the model might fail. Use handle_unknown="ignore" in OneHotEncoder to avoid this.
-
Handling Missing Values:
If the new data has missing values (e.g., Kms_Driven), impute them (e.g., using mean/median) before prediction.
-
Outliers:
If the new data contains extreme values (e.g., Kms_Driven = 100,000), the model might over/under-predict. Consider capping or normalizing such values.
📌 Example Workflow in Code
import pandas as pd
import joblib
# 1. New data
new_data = {
"Car_Name": "Geely Emgrand X70",
"Year": 2021,
"Kms_Driven": 20000,
"Fuel_Type": "Diesel"
}
new_df = pd.DataFrame([new_data])
# 2. Load model
model = joblib.load("best_price_model.joblib")
# 3. Preprocess and predict
preprocessed_new = model.named_steps["prep"].transform(new_df)
predicted_price = model.named_steps["model"].predict(preprocessed_new)
print(f"Predicted Price: ₹{predicted_price[0]:,.2f}")
🚨 Common Issues to Avoid
-
Mismatched Features:
If the new data lacks a feature (e.g., Year), the model will throw an error. Ensure all features are included.
-
Incorrect Data Types:
Numeric features (e.g., Kms_Driven) must be integers/floats, not strings.
-
Unseen Categories:
If Fuel_Type in new data is LPG (not in training data), the OneHotEncoder will fail. Use handle_unknown="ignore" in the pipeline.
✅ Final Notes
- The model will predict the price based on the input features.
- You can use this for tasks like:
- Pricing a used car.
- Comparing car values across different models.
- Building a car valuation tool.
Let me know if you need help with specific edge cases (e.g., handling new categories, scaling, or custom features)! 🚗💰