In [29]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [21]:
np.set_printoptions(precision=3, suppress=True)
In [22]:
dataset = pd.read_csv('Salary_Data.csv')
In [23]:
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
In [24]:
from sklearn.model_selection import train_test_split
In [25]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)
In [26]:
from sklearn.linear_model import LinearRegression
In [27]:
regressor = LinearRegression()
regressor.fit(X_train, y_train)
Out[27]:
In [28]:
y_pred = regressor.predict(X_test)
pd.DataFrame([y_pred, y_test])
Out[28]:
In [30]:
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()
In [31]:
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()