Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MeherajUlMahmmud/ece7adf1b8d55122d0777a657511bb06 to your computer and use it in GitHub Desktop.
Save MeherajUlMahmmud/ece7adf1b8d55122d0777a657511bb06 to your computer and use it in GitHub Desktop.
# Reading the dataset
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('SimpleLinearRegression.csv')
df.columns = ['YearsExperience', 'Salary']
X = df['YearsExperience']
y = df['Salary']
# Splitting the dataset into the Training set and Test set
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)
# Fitting the dataset into the Regression Model
regressor = fit(X_train, y_train)
# Predicting the values of the Test Set
y_pred = predict(X_test)
# Visualizing the Training set results
plt.scatter(X_train, y_train, color='red')
plt.plot(X_train, predict(X_train), color='blue')
plt.title('Salary VS Experience (Training set)')
plt.xlabel('Year of Experience')
plt.ylabel('Salary')
plt.show()
# Visualizing the Test set results
plt.scatter(X_test, y_test, color='red')
plt.plot(X_train, predict(X_train), color='blue')
plt.title('Salary VS Experience (Test set)')
plt.xlabel('Year of Experience')
plt.ylabel('Salary')
plt.show()
# Model evaluation for testing set
mae = calcMAE(y_test, y_pred)
mse = calcMSE(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("The model performance for testing set")
print("--------------------------------------")
print('MAE is %.2f'% mae)
print('MSE is %.2f'% mse)
print('R2 score is %.2f'% r2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment