In [29]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [21]:
np.set_printoptions(precision=3, suppress=True)

Importing a dataset


In [22]:
dataset = pd.read_csv('Salary_Data.csv')

In [23]:
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

Splitting the dataset into a Training set and a Test set


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)

Fitting Simple Linear Regression to the Training set


In [26]:
from sklearn.linear_model import LinearRegression

In [27]:
regressor = LinearRegression()
regressor.fit(X_train, y_train)


Out[27]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

Predicting the Test set results


In [28]:
y_pred = regressor.predict(X_test)
pd.DataFrame([y_pred, y_test])


Out[28]:
0 1 2 3 4 5 6 7 8 9
0 40835.105909 123079.399408 65134.556261 63265.367772 115602.645454 108125.891499 116537.239698 64199.962017 76349.687193 100649.137545
1 37731.000000 122391.000000 57081.000000 63218.000000 116969.000000 109431.000000 112635.000000 55794.000000 83088.000000 101302.000000

Visualising the Training set results


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()


Visualising the Test set results


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()