ML Exp4
ML Exp4
4
Shaikh Ubaidur Rahman
211254
Machine Learning
CSL701
Department of Computer Engineering
MHSSCE, Mumbai.
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.
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
df = pd.DataFrame(data)
# Encode categorical
variable
le = LabelEncoder()
df['Species'] =
le.fit_transform(df['Speci
es'])
# Scale features
scaler =
StandardScaler()
X_train_scaled =
scaler.fit_transform(X_tr
ain)
X_test_scaled =
scaler.transform(X_test)
# Predict
y_pred =
model.predict(X_test_sc
aled)
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.