1_Linear Regression

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

# Importing Libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

# Reading the Dataset

data = pd.read_csv('C:/Users/ARYA/Salary_Data.csv')

# Displaying the First Few Rows of the Dataset

print(data.head())

# Checking the Shape of the Dataset

print(data.shape)

# Checking for Null Values

print(data.isnull().sum())

# Separating the Features and Target Variables

x = data.iloc[:, :1].values

y = data.iloc[:, 1:2].values

print(x)

print(y)

# Splitting the Dataset into Training and Testing Sets

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

# Fitting the Model (Linear Regression)

model = LinearRegression()

model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print(y_pred)

print(y_test)

# Plotting the Training Dataset

plt.scatter(x_train, y_train, color='blue')

plt.plot(x_train, model.predict(x_train), color='red')

plt.title('SALARY VS EXPERIENCE (training set)')

plt.xlabel('Experience in Years')

plt.ylabel('Salary in Rupees')

plt.show()

# Plotting the Testing Dataset

plt.scatter(x_test, y_test, color='blue')

plt.plot(x_train, model.predict(x_train), color='red')

plt.title('SALARY VS EXPERIENCE (testing set)')

plt.xlabel('Experience in Years')

plt.ylabel('Salary in Rupees')

plt.show()

You might also like