Real estate is defined as the land itself and any permanent, man-made, or natural improvements attached to it, such as buildings, houses, and infrastructure. It is considered an immovable asset.

Here let us predict the values of site and houses using linear regression.

PLOT PRICE PREDICTION

In [1]:
from sklearn.linear_model import LinearRegression
import numpy as np
In [2]:
# Size (sqft) → Price
X = np.array([[600], [1200], [1600], [2000]])
y = np.array([100000, 200000, 300000, 400000])

model = LinearRegression()
model.fit(X, y)
input_size = int(input("Enter the size of the house: "))
print(f"Predicted price for: {input_size} square feet is {model.predict([[input_size]])[0]:.2f}")

#print("Predicted price 2400 square feet is:", model.predict([[2400]])[0])
Enter the size of the house: 2600
Predicted price for: 2600 square feet is 518691.59

HOUSE PRICE PREDICTION

In [3]:
# Step 1: Import Libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split


# Step 2: Create Random Dataset

np.random.seed(42)  # for reproducibility

n = 100   # number of houses

# Features
carpet_area = np.random.randint(500, 2501, n)        # whole numbers
bedrooms = np.random.randint(1, 6, n)                # whole numbers
years_built = np.random.uniform(0, 30, n)            # float values (house age)

# Step 3: Create Target (House Price)
# Price formula (artificial logic)
price = (
    carpet_area * 3000 +
    bedrooms * 500000 -
    years_built * 20000 +
    np.random.normal(0, 200000, n)   # noise (float)
)

# Step 4: Combine features into X
X = np.column_stack((carpet_area, bedrooms, years_built))
y = price

# Step 5: Split Data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Step 6: Train Model
model = LinearRegression()
model.fit(X_train, y_train)

y_predict = model.predict(X_test)

# Step 7: Check Accuracy (R² Score)
print("Model Accuracy (R² Score):", model.score(X_test, y_test))

plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_predict, alpha=0.5) # alpha parameter adds transparency
plt.title('Actual vs Predicted Values')
plt.xlabel('Actual Values (y_test)')
plt.ylabel('Predicted Values (y_predict)')
plt.grid(True)

# Step 8: Predict for New House
new_house = np.array([[1500, 3, 10]])  # 1500 sqft, 3 bedrooms, 10 years old
predicted_price = model.predict(new_house)

print("Predicted Price:", predicted_price[0])
Model Accuracy (R² Score): 0.980991124597536
Predicted Price: 5799217.491447251