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

ML Prac 1

Uploaded by

Gaurav Zambare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

ML Prac 1

Uploaded by

Gaurav Zambare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim:- To Implement Linear Regression .

# importing the dataset

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

# Load dataset

dataset = pd.read_csv('Salary_Data.csv')

dataset.head()

# Data preprocessing

X = dataset.iloc[:, :-1].values # independent variable array

y = dataset.iloc[:, 1].values # dependent variable vector

# Splitting the dataset

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)

# Fitting the regression model

regressor = LinearRegression()

regressor.fit(X_train, y_train) # Fit the model

# Predicting the test set results

y_pred = regressor.predict(X_test)
# Model Evaluation Metrics

mse = mean_squared_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error (MSE): {mse:.2f}")

print(f"R² Score: {r2:.4f}")

# Visualizing the results

# Plot for the TRAIN set

plt.scatter(X_train, y_train, color='red')

plt.plot(X_train, regressor.predict(X_train), color='blue')

plt.title("Salary vs Experience (Training set)")

plt.xlabel("Years of experience")

plt.ylabel("Salaries")

plt.show()

# Plot for the TEST set

plt.scatter(X_test, y_test, color='red')

plt.plot(X_train, regressor.predict(X_train), color='blue')

plt.title("Salary vs Experience (Testing set)")

plt.xlabel("Years of experience")

plt.ylabel("Salaries")

plt.show()
Aim:- To Implement Multiple Linear Regression .

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

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

from sklearn.compose import ColumnTransformer

# Load dataset

dataset = pd.read_csv('50_Startups.csv')

dataset.head()

x = dataset.iloc[:, :-1].values # independent variable array

y = dataset.iloc[:, 1].values

#Catgorical data

labelencoder_x= LabelEncoder()

x[:, 3] = labelencoder_x.fit_transform(x[:,3])

# onehotencoder= OneHotEncoder(categorical_features= [3])

ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(drop='first'), [3])],


remainder='passthrough')

# x= onehotencoder.fit_transform(x).toarray()

x = x[:, 1:]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state=0)


#Fitting the MLR model to the training set:

regressor= LinearRegression()

regressor.fit(x_train, y_train)

y_train_pred = regressor.predict(x_train)

y_test_pred = regressor.predict(x_test)

print("Train Score:",regressor.score(x_train,y_train))

print("Test Score:",regressor.score(x_test,y_test))

You might also like