"""
### Machine Learning Algorithm - Regression
"""
"""
### Polynomial Linear Regression (PLR)
"""
"""
#### 1 - Importing the libraries
"""
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns

import sklearn

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split

from sklearn.metrics import r2_score
"""
#### 2 - Importing the dataset
"""
dataset = pd.read_csv('../input/position-salaries-polynomial-linear-regression/position_salaries.csv')
type(dataset)
dataset.shape
dataset.dtypes
dataset
"""
#### 3 - EDA
"""
# dataset.info()
# dataset.describe()
dataset.columns
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values
X.shape
"""
#### 4 - Training the Linear Regression model on the whole dataset
"""
# from sklearn.linear_model import LinearRegression
lin_reg_model = LinearRegression()
lin_reg_model.fit(X, y)
y_pred1 = lin_reg_model.predict(X)
# Visualising the Linear Regression results
plt.scatter(X, y, color='green', label='')
plt.plot(X, y_pred1, color="blue", label='', marker='+')
plt.title("Truth or Bluff (Linear Regression)")
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()
"""
#### 5 - Training the Polynomial Regression model on the whole dataset
"""
# from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=3)
X_poly = poly_reg.fit_transform(X)
X_poly
lin_reg_model2 = LinearRegression()
lin_reg_model2.fit(X_poly, y)
y_pred2 = lin_reg_model2.predict(X_poly)
y_pred2
# Visualising the Polynomial Regression results
plt.scatter(X, y, color='green', label='')
plt.plot(X, y_pred2, color="blue", label='')
plt.title("Truth or Bluff (Polynomial Regression)")
plt.xlabel('Position level')
plt.ylabel('Salry')
plt.show()
# Visualising the Polynomial Regression results 
# (for higher resolution and smoother curve)
x_grid = np.arange(min(X), max(X), 0.1)
x_grid
x_grid.shape
x_grid = x_grid.reshape(len(x_grid), 1)
x_grid.shape
plt.scatter(X, y, color='green')
plt.plot(x_grid, lin_reg_model2.predict(poly_reg.fit_transform(x_grid)), color="blue")
plt.title("Truth or Bluff (Polynomial Regression)")
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
"""
### Question 1:
"""
"""
<b><i>Predicting a new result with Linear Regression</i></b>
"""
"""
Position level: 6.5
"""
# SLR
lin_reg_model.predict([[6.5]])  # Salary
"""
### Question 2:
"""
"""
<b><i>Predicting a new result with Polynomial Regression</i></b>
"""
"""
Position level: 6.5
"""
# PLR
lin_reg_model2.predict(poly_reg.fit_transform([[6.5]]))   # Salary
plotting_xs = np.linspace(X.min(), X.max(), 1000)
for i in range(1, 11):
    # Applying Train Test Split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
    
    # Creating Polynomial object
    poly_reg = PolynomialFeatures(degree=i)
    
    # Trasforming the training and testing data to required Polynomial degree
    X_train = poly_reg.fit_transform(X_train.reshape(X_train.shape[0], 1))
    X_test = poly_reg.fit_transform(X_test.reshape(X_test.shape[0], 1))
    
    # Model
    reg = LinearRegression()
    
    # Fitting
    reg.fit(X_train,y_train)
    
    # Predicting
    y_pred = reg.predict(X_test)
    
    # Generating predictions for 1000 points which will help in plotting the curve of regression
    xs = poly_reg.fit_transform(plotting_xs.reshape(plotting_xs.shape[0], 1))
    plotting_ys = reg.predict(xs)
    
    # Plotting
    plt.title(f'Degree: {i}, R2 score: {r2_score(y_pred,y_test)}')
    
    # Plotting the training data 
    plt.scatter(X, y, label='Training Data')
    
    # Plotting the curve of regression
    plt.plot(plotting_xs, plotting_ys, label='Curve of Regression')
    plt.legend()
    plt.show()
"""
## Happy Machine Learning :)
"""