EXP-8 To perform a simple Linear Regression with python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Sample House Data
data = {
'Area': [1000, 1200, 1500, 1800, 2000, 2200, 2500, 2700, 3000],
'Price': [200000, 240000, 300000, 360000, 400000, 440000, 500000, 540000, 600000]
df = pd.DataFrame(data)
# Features and Target
X = df[['Area']]
y = df['Price']
# Train the Model
regressor = LinearRegression()
regressor.fit(X, y)
# Predict on the same X for full-line visualization
y_pred = regressor.predict(X)
# Print Coe icients
print("Slope (coef_):", regressor.coef_)
print("Intercept:", regressor.intercept_)
# Plotting
plt.scatter(X, y, color='blue', label='Actual Data Points')
plt.plot(X, y_pred, color='red', linewidth=2, label='Regression Line')
plt.xlabel('Area (sq ft)')
plt.ylabel('Price ($)')
plt.title('Simple Linear Regression - Area vs Price')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT: