Path: blob/master/Part 2 - Regression/Simple Linear Regression/simple_linear_regression.py
1009 views
# Simple Linear Regression12# Importing the libraries3import numpy as np4import matplotlib.pyplot as plt5import pandas as pd67# Importing the dataset8dataset = pd.read_csv('Salary_Data.csv')9X = dataset.iloc[:, :-1].values10y = dataset.iloc[:, 1].values1112# Splitting the dataset into the Training set and Test set13from sklearn.cross_validation import train_test_split14X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)1516# Feature Scaling17"""from sklearn.preprocessing import StandardScaler18sc_X = StandardScaler()19X_train = sc_X.fit_transform(X_train)20X_test = sc_X.transform(X_test)21sc_y = StandardScaler()22y_train = sc_y.fit_transform(y_train)"""2324# Fitting Simple Linear Regression to the Training set25from sklearn.linear_model import LinearRegression26regressor = LinearRegression()27regressor.fit(X_train, y_train)2829# Predicting the Test set results30y_pred = regressor.predict(X_test)3132# Visualising the Training set results33plt.scatter(X_train, y_train, color = 'red')34plt.plot(X_train, regressor.predict(X_train), color = 'blue')35plt.title('Salary vs Experience (Training set)')36plt.xlabel('Years of Experience')37plt.ylabel('Salary')38plt.show()3940# Visualising the Test set results41plt.scatter(X_test, y_test, color = 'red')42plt.plot(X_train, regressor.predict(X_train), color = 'blue')43plt.title('Salary vs Experience (Test set)')44plt.xlabel('Years of Experience')45plt.ylabel('Salary')46plt.show()4748