Skip to content

Instantly share code, notes, and snippets.

@amansk2050
Last active June 14, 2020 17:30
Show Gist options
  • Save amansk2050/17498289f70c5a1533fcc1e53591450e to your computer and use it in GitHub Desktop.
Save amansk2050/17498289f70c5a1533fcc1e53591450e to your computer and use it in GitHub Desktop.
Linear Regression in python
#import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as mtp
# loding data set
data=pd.read_csv("/kaggle/input/years-of-experience-and-salary-dataset/Salary_Data.csv")
#having a look on data set
data.head(15)
#extracting dependent and independent variables
Y=data["Salary"]
X=data["YearsExperience"]
# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(X, Y, test_size= 1/3, random_state=0)
#converting to array and reshaping it
x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = np.array(x_test)
y_test = np.array(y_test)
x_train = x_train.reshape(-1,1)
x_test = x_test.reshape(-1,1)
#Fitting the Simple Linear Regression model to the training datase
from sklearn.linear_model import LinearRegression
regressor= LinearRegression()
regressor.fit(x_train, y_train)
#predicting value
y_pred= regressor.predict(x_test)
#getting accuracy
from sklearn.metrics import r2_score
print(r2_score(y_test,y_pred))
#visualizing the Test set results
mtp.scatter(x_test, y_test, color="blue")
mtp.plot(x_train, x_pred, color="red")
mtp.title("Salary vs Experience (Test Dataset)")
mtp.xlabel("Years of Experience")
mtp.ylabel("Salary(In Rupees)")
mtp.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment