Explaned Line by Line : Linear Regression Model for House Price Prediction in Python

 import numpy as np

from sklearn.linear_model import LinearRegression


This imports the necessary libraries: NumPy for numerical computations and the scikit-learn library's LinearRegression class for training a linear regression model.

size = np.array([[100], [150], [200], [250], [300], [350], [400], [450], [500], [550]]) price = np.array([150000, 200000, 250000, 300000, 350000, 400000, 450000, 500000, 550000, 600000])


These two lines create the training data. size represents the square footage of each house, and price represents the corresponding selling price of each house. They are NumPy arrays.


model = LinearRegression() model.fit(size, price)

These two lines create a LinearRegression object and then train it on the size and price data. This means the model will find a linear equation that predicts the selling price of a house based on its size.

new_size = np.array([[400]]) predicted_price = model.predict(new_size) print(predicted_price)

These lines create a new size value to test the trained model's prediction, with a value of 400 sq.ft, then use the trained model to predict the corresponding selling price, and finally, print the predicted price.

Comments

Popular posts from this blog

Simple Python Program Code for Snake Game

Project 01: Python Code for Music Recommendation with Explanation