Problem: Predicting House Prices Using Linear Regression
In this example, we analyze a dataset containing house features (e.g., size, number of rooms) to predict house prices using linear regression, a core technique in data science.
Objective
- Train a linear regression model to predict house prices based on square footage.
- Visualize the results, including the data points and the regression line.
Dataset
We will simulate a dataset of house sizes (in square feet) and corresponding prices (in $1000s).
This ensures reproducibility and simplicity.
Python Code
1 | import numpy as np |
Explanation of the Code
- Simulated Data:
- House sizes are sampled randomly between $500$ and $3500$ square feet.
- Prices are calculated using a simple linear relationship $( \text{Price} = 0.15 \times \text{Size} + \text{Noise} )$, where the noise simulates real-world variability.
- Model Training:
- A
LinearRegressionmodel fromsklearnis trained using house sizes as the feature and prices as the target.
- A
- Prediction:
- The trained model predicts house prices, which are plotted alongside the actual prices.
- Model Evaluation:
- The Mean Squared Error (MSE) quantifies the model’s accuracy.
Visualization and Insights

- Scatter Plot:
- Blue dots represent the actual prices for different house sizes.
- Regression Line:
- The red line shows the best-fit line learned by the linear regression model.
- Model Performance:
- The MSE indicates the average squared difference between the predicted and actual prices.
Applications in Data Science
- Regression Analysis:
- This approach is foundational in predictive modeling for understanding relationships between variables.
- Real Estate Analytics:
- Real-world data would involve more features like location, number of bedrooms, and year built.
- Scalability:
- This method easily extends to multivariate regression with multiple predictors.
This example demonstrates the workflow of data science: data preprocessing, modeling, and visualization, providing insights into how features like size influence house prices.








