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

Exp No3

lab
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)
5 views3 pages

Exp No3

lab
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

EXP NO:3 Experiment the regression model without a bias and with bias.

DATE:

Aim:
The aim is to compare the performance of a linear regression model without bias (intercept)
and with bias on a given dataset. The goal is to understand the impact of including or
excluding the bias term in the model.

Algorithm:

Without Bias:
Import libraries.
Load the dataset.
Extract features (X) and target variable (y).
Split the data into training and testing sets.
Build and train a linear regression model without bias.
Make predictions.
Evaluate model performance.
With Bias:
Import libraries.
Load the dataset.
Extract features (X) and target variable (y).
Split the data into training and testing sets.
Build and train a linear regression model with bias.
Make predictions.
Evaluate model performance.

Program:

# Import Libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics

# Load Data
df = pd.read_csv('your_dataset.csv')

# Extract Features and Target Variable


X = df[['Variable1']]
y = df['Variable2']

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

# Without Bias
model_without_bias = LinearRegression(fit_intercept=False)
model_without_bias.fit(X_train, y_train)
predictions_without_bias = model_without_bias.predict(X_test)

# With Bias
model_with_bias = LinearRegression(fit_intercept=True)
model_with_bias.fit(X_train, y_train)
predictions_with_bias = model_with_bias.predict(X_test)

# Evaluate Models
print('Without Bias - Mean Absolute Error:', metrics.mean_absolute_error(y_test,
predictions_without_bias))
print('Without Bias - Mean Squared Error:', metrics.mean_squared_error(y_test,
predictions_without_bias))
print('Without Bias - R-squared:', metrics.r2_score(y_test, predictions_without_bias))
print('\nWith Bias - Mean Absolute Error:', metrics.mean_absolute_error(y_test,
predictions_with_bias))
print('With Bias - Mean Squared Error:', metrics.mean_squared_error(y_test,
predictions_with_bias))
print('With Bias - R-squared:', metrics.r2_score(y_test, predictions_with_bias))
Input:
Variable1,Variable2
1.5, 2.5
2.0, 3.0
3.0, 4.0
4.5, 5.5
5.0, 6.0
Output:

Result:
Thus,Experiment the regression model without a bias and with bias was verified and
completed successfully.

You might also like