Simple_Linear_Regression
#!/usr/bin/env python
# coding: utf-8
# In[3]:
#importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# In[4]:
myfile="D:\Sunny115\week2_Salary_Data.csv"
dataset=pd.read_csv(myfile)
X=dataset.iloc[:,:-1].values
y=dataset.iloc[:,-1].values
# In[5]:
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)
# In[6]:
from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(X_train,y_train)
# In[7]:
print(X_train)
# In[9]:
y_pred=regressor.predict(X_test)
print(y_test)
print(y_pred)
# In[10]:
from sklearn.metrics import r2_score
r2_score(y_test,y_pred)
# In[13]:
#Visulising the training set results
plt.scatter(X_train,y_train,color='blue')
plt.plot(X_train,regressor.predict(X_train),color='green')
plt.title('Salary vs Expereince (training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# In[14]:
#Visulising the training set results
plt.scatter(X_test,y_test,color='blue')
plt.plot(X_train,regressor.predict(X_train),color='green')
plt.title('Salary vs Expereince (training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# In[ ]:
Comments
Post a Comment