0% found this document useful (0 votes)
11 views

Assignment 2_.ipynb - Colab

Uploaded by

hbashir659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assignment 2_.ipynb - Colab

Uploaded by

hbashir659
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

keyboard_arrow_down Name: Hamza Bashir

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

keyboard_arrow_down Part(a)
data = {
"y": [240, 236, 270, 264, 301, 316, 300, 296, 267, 276, 282, 261],
"x1": [25, 31, 45, 50, 65, 72, 84, 85, 75, 62, 50, 38],
"x2": [24, 29, 25, 26, 21, 26, 25, 25, 24, 25, 23, 23],
"x3": [91, 91, 88, 90, 91, 92, 93, 92, 88, 91, 89, 89],
"x4": [100, 95, 110, 98, 94, 97, 96, 96, 90, 105, 98, 98],
}

df = pd.DataFrame(data)

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 12 entries, 0 to 11
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 y 12 non-null int64
1 x1 12 non-null int64
2 x2 12 non-null int64
3 x3 12 non-null int64
4 x4 12 non-null int64
dtypes: int64(5)
memory usage: 608.0 bytes

y = df["y"]
X = df[["x1", "x2", "x3", "x4"]]

model = LinearRegression()
model.fit(X, y)

▾ LinearRegression i ?

LinearRegression()

beta_0 = model.intercept_
beta_coefficients = model.coef_

y_pred = model.predict(X)

print("Beta coefficients:")
print(f"Intercept (beta_0): {beta_0}")
for i, coef in enumerate(beta_coefficients):
print(f"Beta_{i+1}: {coef}")

Beta coefficients:
Intercept (beta_0): -108.02395826086968
Beta_1: 0.9211727963165789
Beta_2: -3.5723740712029395
Beta_3: 3.5939152683638547
Beta_4: 0.9643774944297512

keyboard_arrow_down Part (b)


mse = mean_squared_error(y, y_pred)

print(f"\nMean Square Error (MSE): {mse}")

Mean Square Error (MSE): 120.5094174391496

You might also like