Simple Linear Regression
A tutorial on How to use simple Linear Regression.
- 0. Data Preprocessing
- 1. Training the Simple Linear Regression model on the Training set
- 2. Predicting the Test set results
- 3. Visualising
- 3.1 Visualising the Test set results
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Salary_Data.csv')
dataset
dataset.isna().sum()
dataset.info()
X = dataset.drop('Salary', axis=1)
X
y = dataset['Salary']
y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
regressor.score(X_test,y_test)
y_pred = regressor.predict(X_test)
d = {'y_pred': y_pred, 'y_test': y_test}
pd.DataFrame(d)
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('Salary')
plt.show()
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()