0% found this document useful (0 votes)
0 views3 pages

Program

The program implements a linear regression model using synthetic data generated with a linear relationship plus noise. It splits the data into training and testing sets, fits the model, and evaluates its performance using mean squared error and R-squared score. Finally, it visualizes the actual vs predicted values with a scatter plot and a regression line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Program

The program implements a linear regression model using synthetic data generated with a linear relationship plus noise. It splits the data into training and testing sets, fits the model, and evaluates its performance using mean squared error and R-squared score. Finally, it visualizes the actual vs predicted values with a scatter plot and a regression line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM:

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, r2_score

np.random.seed(42)

X = 2 * np.random.rand(100, 1)

y = 4+ 3 * X + np.random.randn(100, 1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LinearRegression()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

print(f"Intercept: {model.intercept_[0]}")

print(f"Coefficient: {model.coef_[0][0]}")

print(f"Mean Squared Error: {mean_squared_error(y_test, y_pred)}")

print(f"R-squared Score: {r2_score(y_test, y_pred)}")

plt.scatter(X_test, y_test, color='blue', label='Actual')

plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted')

plt.xlabel("X")

plt.ylabel("y")

plt.title("Linear Regression Model")


plt.legend()

plt.show()
OUTPUT:

You might also like