This project demonstrates how to use ML.NET to build a machine learning model that predicts house prices based on input features like size, bedrooms, and bathrooms. The trained model is then integrated into an ASP.NET Core Web API to serve predictions.
HousePricePredictor/ │ ├── HousePriceModel/ # Console app (training) │ └── Program.cs # Trains the model and saves it │ ├── HousePriceModelAPI/ # ASP.NET Core Web API │ ├── Controllers/ │ │ └── HousePriceController.cs │ ├── Models/ │ │ ├── HouseData.cs │ │ └── HousePrediction.cs │ ├── HousePriceModel.zip # Trained ML model (copied from console app) │ └── Program.cs │ ├── README.md
yaml Copy code
- Train a regression model using ML.NET.
- Save the model as
HousePriceModel.zip. - Load the model in an ASP.NET Core Web API.
- Accept house feature input and return predicted price.
Size(float) - Total square footageBedrooms(float) - Number of bedroomsBathrooms(float) - Number of bathrooms
- Open
HousePriceModel/Program.cs - Run the console app (
Ctrl+F5ordotnet run) - After training, a model file named
HousePriceModel.zipwill be created in: /HousePriceModel/bin/Debug/net6.0/HousePriceModel.zip
markdown Copy code
- Copy
HousePriceModel.zipinto the Web API project root: /HousePriceModelAPI/HousePriceModel.zip
yaml Copy code
- Open the
HousePriceModelAPIproject - Run it using Visual Studio or terminal:
dotnet run
Navigate to Swagger or use Postman to test:
bash
Copy code
POST http://localhost:{port}/api/houseprice
📦 Sample Request
🔹 POST /api/houseprice
json
Copy code
{
"size": 1800,
"bedrooms": 3,
"bathrooms": 2
}
🔹 Response
json
Copy code
{
"size": 1800,
"bedrooms": 3,
"bathrooms": 2,
"predictedPrice": 354800.52
}
🧪 Quick Test Endpoint
You can also test a sample prediction at:
bash
Copy code
GET /api/houseprice/test
🖼️ Screenshots
### 🧠 Home Price Prediction App

✅ Web API Response
❗ Troubleshooting
Issue Fix
Model file not found Make sure HousePriceModel.zip is copied to /HousePriceModelAPI
PredictedPrice = 0 Ensure your API's HouseData class has no Price property. It should match the model input schema
Could not find label column 'Label' Add [ColumnName("Label")] to Price in training model
Console works but API doesn't You're likely using mismatched classes or an outdated .zip in API
🙌 Credits
Built with:
.NET 6
ML.NET
ASP.NET Core Web API
📬 Contribute or Contact
Have feedback or want to contribute? Open an issue or PR!