Are you curious about how machine learning can be applied to predict real-world phenomena like house prices? In this article, we'll walk through a simple yet powerful example of supervised machine learning using Python's scikit-learn library. By the end, you'll have a good grasp of the fundamental concepts behind linear regression and how it can be implemented to predict house prices based on the number of bedrooms.
Recap: Supervised Machine Learning
Supervised machine learning is a branch of artificial intelligence where models are trained on labeled data. These models learn relationships between input features and corresponding target values, enabling them to make predictions on unseen data. One popular supervised learning algorithm is linear regression, which is particularly effective for predicting numerical outcomes.
The Dataset: Number of Bedrooms vs. House Prices
For our example, let's consider a simple dataset that relates the number of bedrooms in a house to its price. We'll use this data to train a linear regression model to predict house prices based on the number of bedrooms.
Setting up the Environment
First, we import the necessary libraries:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
Loading and Preparing the Data
We initialize our dataset with arrays representing the number of bedrooms and their corresponding prices. Then, we split the data into training and testing sets:
bedrooms = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1)
prices = np.array([15000, 25000, 35000, 45000, 55000, 65000, 75000, 85000, 95000, 105000])
X_train, X_test, y_train, y_test = train_test_split(bedrooms, prices, test_size=0.2, random_state=42)
Training the Model
We create a Linear Regression model and train it using the training data:
model = LinearRegression()
model.fit(X_train, y_train)
Making Predictions
Now that our model is trained, we can use it to make predictions on the testing data:
y_pred = model.predict(X_test)
Evaluating the Model
To assess the performance of our model, we calculate the Mean Squared Error (MSE) between the predicted and actual house prices:
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
Predicting House Prices
Finally, we can use our trained model to predict the price of a house with a specific number of bedrooms. For example, let's predict the price for a house with 3.5 bedrooms:
new_bedrooms = np.array([[3.5]])
predicted_price = model.predict(new_bedrooms)
print("Predicted price for 3.5 bedrooms:", predicted_price[0])
In this article, we've explored the basics of supervised machine learning using linear regression. We've seen how to train a model, make predictions, and evaluate its performance. By applying these concepts to a real-world example of predicting house prices, we've gained valuable insight into the power of machine learning in solving practical problems. I hope this article has inspired you to dive deeper into the fascinating world of machine learning!
No comments:
Post a Comment