# -*- coding: utf-8 -*-
"""ML-M3-PolynomialLinearRegression.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1soqUtQwaW5ZQDLP7DR-JB1-sT351LHD7
"""

#importin libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#importing dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
Y = dataset.iloc[:, -1].values
#print(X)
#print(Y)

#here we will compare 2 models too
#implementing simple linear model

from sklearn.linear_model import LinearRegression
linear_regressor= LinearRegression()
linear_regressor.fit(X,Y)

#implementing the polynomial linear model
#step 1:we will make x_poly....
from sklearn.preprocessing import PolynomialFeatures
polynomial_regressor = PolynomialFeatures(degree = 5)
X_poly = polynomial_regressor.fit_transform(X)
#step 2:just make linear model anf fit X_poly and y
linear_regressor_2 = LinearRegression()
linear_regressor_2.fit(X_poly, Y)

#so lin1 is simple linear model and lin2 is polynomial liner model


#visualising the linear model
plt.plot(X,linear_regressor.predict(X),color='blue')
plt.scatter(X,Y,color="red")
plt.title('Linear Regression')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()
#you will observe that it is not accurate/well adapted


#visualising the polynomial model
plt.plot(X,linear_regressor_2.predict(polynomial_regressor.fit_transform(X)),color='blue')
plt.scatter(X,Y,color="red")
plt.title('polynomial Regression')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()


#optional
#visualising previous graph with high resolution
X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, Y, color = 'red')
plt.plot(X_grid, linear_regressor_2.predict(polynomial_regressor.fit_transform(X_grid)), color = 'blue')
plt.title('Polynomial Regression')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

#if you want to predict single value
#whhat it is showing?
#value=year of exp. ans=what can be his/her salary?
#print(linear_regressor.predict([[6.9]]))
#print(linear_regressor_2.predict(polynomial_regressor.fit_transform([[6.9]])))