Predicting Salaries with Linear Regression
1. Exploring the data
# Import all usefull modules
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
%matplotlib inline
# Exploring the dataset
data = pd.read_csv('Salary.csv')
data.head()
# Describe the dataset
data.describe()
2. Extracting independent and dependent variables
# our independent variable is the years of experience
# x = data.iloc[:, 0].values.reshape(1,-1).T
x = data.iloc[:, :1].values
# our dependent variable is the salary
y = data.iloc[:, 1].values
# Now, let's separate some values for train and some others for test
# Let's use 80% or training
# Note: We can use random_state = 0 for always getting the same random values (static seed)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
3. Constructing our ML model
# These next two lines are enough to create our model
regressor = LinearRegression()
regressor.fit(x_train, y_train)
4. Plotting results
# y Prediction from the train model
y_predicted = regressor.predict(x_train)
# Plotting train data
plt.scatter(x_train, y_train)
plt.plot(x_train, y_predicted, color = 'black')
# Description axis
plt.title('Salary vs Experience')
plt.xlabel('Experience')
plt.ylabel('Salary')
# Show plot
plt.show()
# Plotting test data
plt.scatter(x_test, y_test, color = 'green')
plt.plot(x_train, y_predicted, color = 'black')
# Description axis
plt.title('Salary vs Experience')
plt.xlabel('Experience')
plt.ylabel('Salary')
# Show plot
plt.show()
# Verify the accuracy of our model
regressor.score(x_test, y_test)
5. Testing with more data
Execute the following lines of code for generating larger synthetic data. Then, execute again from steps 2 to 4 to see the new results.
import numpy as np
def get_dataset(m, b, size):
''' Read the pendient, the constant, and the number
of elements that will be genearted for a linear function
'''
x = np.linspace(1, size, size) # start, end, number of elements
# x = np.arange(-size/2, size/2, 1) # start, end, increment
y = m*x + b
# adding noise to y
noise = np.random.normal(size/10, size, y.shape)
y = y + noise
return x, y
# getting the linear function with <size> elements and plot it
x, y = get_dataset(m=10, b=1000, size=1000)
plt.scatter(x, y)
data = pd.DataFrame()
data['years'] = x
data['salary'] = y
data