0% found this document useful (0 votes)
24 views4 pages

ML Exp4

Ml experiment

Uploaded by

siddzakariya786
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)
24 views4 pages

ML Exp4

Ml experiment

Uploaded by

siddzakariya786
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/ 4

Experiment No.

4
Shaikh Ubaidur Rahman
211254
Machine Learning
CSL701
Department of Computer Engineering
MHSSCE, Mumbai.

Aim: To implement Multivariate Linear Regression.

Theory: Multivariate linear regression is a statistical method used to understand the


relationship between two or more outcomes (dependent variable) and one or more predictors
(independent variables). Essentially, it helps us predict a certain value based on several factors.

Example: Imagine you are trying to predict house buying price and house rental price in a
house price prediction dataset. In this problem:
Dependent Variable: all the details of the house (i.e. bedrooms, bathrooms, etc).
Independent Variable: Buying price and rental price of the house.

Difference between Multiple and Multivariate Regression


i)Multiple: Multiple linear regression is a type of linear regression where you use two or more
predictors (independent variables) to predict a single outcome (dependent variable).
ii)Multivariate: Multivariate linear regression is where you use multiple outcomes (dependent
variables) and predict them using one or more predictors. It’s about predicting more than one
outcome simultaneously

Program:
import pandas as pd
from
sklearn.model_selection
import train_test_split
from
sklearn.linear_model
import LinearRegression
from
sklearn.preprocessing
import StandardScaler,
LabelEncoder
from sklearn.metrics
import
mean_squared_error,
r2_score
import matplotlib.pyplot
as plt
import seaborn as sns

# Load the dataset from


car.csv data =
pd.read_csv('car.csv')
data =
pd.read_csv('/kaggle/inp
ut/fish-dataset1/Fish_dat
aset.csv')

df = pd.DataFrame(data)

# Encode categorical
variable
le = LabelEncoder()
df['Species'] =
le.fit_transform(df['Speci
es'])

# Define features and


target variable
X = df[['Weight',
'Length1', 'Length2',
'Length3', 'Height',
'Width']]
y = df['Species']

# Split the data into


training and test sets
X_train, X_test, y_train,
y_test =
train_test_split(X, y,
test_size=0.3,
random_state=42)

# Scale features
scaler =
StandardScaler()
X_train_scaled =
scaler.fit_transform(X_tr
ain)
X_test_scaled =
scaler.transform(X_test)

# Fit Linear Regression


model
model =
LinearRegression()
model.fit(X_train_scaled,
y_train)

# Predict
y_pred =
model.predict(X_test_sc
aled)

# Evaluate the model


mse =
mean_squared_error(y_t
est, y_pred)
r2 = r2_score(y_test,
y_pred)

print(f"Mean Squared
Error: {mse}")
print(f"R-squared: {r2}")

# Plot predictions vs
actual values
plt.figure(figsize=(10, 6))
plt.scatter(y_test,
y_pred, alpha=0.5)
plt.xlabel('Actual Species
(Encoded)')
plt.ylabel('Predicted
Species (Encoded)')
plt.title('Actual vs
Predicted Species')
plt.plot([y_test.min(),
y_test.max()],
[y_test.min(),
y_test.max()], 'r--', lw=2)
plt.show()

Output:
Conclusion:
Thus, we have implemented Multivariate Linear regression using a dataset of fish breeds.

You might also like