"""
# Table of Content
"""
"""
# 1. WorkFlow
"""
"""
# 2. What is Linear Regression
"""
"""
# 3. Importing Library
"""
"""
# 4. Loading Dataset
"""
"""

# 5. Data Preprocessing
"""
"""
# 6. Model Building 
"""
"""
# 7. Model Evaluation

"""
"""
# 8. Conclusion
"""
"""
# 1. Work Flow
"""
"""
# Axix Bank Stock Data --->  Data Preprocessing ---> Train Test Split ---> Model Fiting ---> Prediction
"""
"""
# 2. What is Linear Regression?
"""
"""
**Linear regression is one of the easiest and most popular Machine Learning algorithms. Linear Regression is a machine learning algorithm based on supervised learning. It is a statistical method that is used for predictive analysis. Linear regression is useful for finding relationship between two continuous variables. One is independent variable and other is dependent variable. Linear regression makes predictions for continuous or real or numeric variables such as sales, salary, age, etc.**
"""
#Import library which need for prediciton

import numpy as np                     #Pandas is used to analyze data
import pandas as pd                    #This library contains a large number of mathematical, algebraic, and transformation functions
from sklearn import metrics
#matplotlib inline sets the backend of matplotlib to the 'inline' backend
%matplotlib inline                     
import matplotlib.pyplot as plt        # Used for Visualization of data
from sklearn.model_selection import train_test_split         # Used for splitting data into train data and test data

# Loading the Dataset using Pandas Library using pd.read_csv(filename with extenstion)

stock = pd.read_csv('../input/axisbank/AXISBANK.csv')
stock    #print the all the row of the dataset
# Printing number of rows and column using shape

stock.shape
#change the date format 

stock['Date'] = pd.to_datetime(stock.Date)
#remove unnecessary columns from the stock dataset

list_drop = ['%Deliverble', 'Deliverable Volume']
stock.drop(list_drop,axis = 1, inplace = True)
# Printing First Five Rows of dataset using head()

stock.head()
# Printing Last Five Row of Dataset using tail()

stock.tail()
#Visualize the Open price data

plt.figure(figsize=(16,8))
plt.title('AXISBANK')
plt.xlabel('Days')
plt.ylabel('Close price IND (₹)')
plt.plot(stock['Open'])
plt.show()
# info(): This method prints information about a DataFrame including the index dtype and column dtypes, non-null values 

stock.info()
# describe() : is used to view some basic statistical details like percentile,mean, std, etc

stock.describe()
# Checking for Null Values using isnull() it return whole dataset with boolean value (False- Notnull value and True- Null Value)

stock.isnull()
# This is second way to check null value and here we use isnull() with sum() 

stock.isnull().sum()
#calculate the length of dataset using len() command
print(len(stock))
# We are just plotting  of Open value

stock['Open'].plot(figsize=(12,8))
# We are just plotting  of High value

stock['High'].plot(figsize=(12,8))
# We are just plotting  of Low value

stock['Low'].plot(figsize=(12,9))
# We are just plotting  of Last value

stock['Last'].plot(figsize=(12,9))
# We are just plotting  of Close value

stock['Close'].plot(figsize=(12,9))
# We are just plotting  of VWAP value

stock['VWAP'].plot(figsize=(12,9))
# We are just plotting  of Volume value

stock['Volume'].plot(figsize=(12,9))

# Instead of Writing all the histogram seprately we can use

stock.hist(figsize=(12,8),bins=50)
#here we created a dependent and independent variables

X = stock[['Open','High','Low','Volume']]
y = stock['Close']
# Spliting data into train data and test data

X_train,X_test,y_train,y_test=train=train_test_split(X,y,random_state=0)
# Printing the size of X training dataset

X_train.shape
# Printing the size of X testing dataset

X_test.shape
# Printing the size of Y training dataset

y_train.shape
# Printing the size of Y testing dataset

y_test.shape
from sklearn.linear_model import LinearRegression
from sklearn.metrics import confusion_matrix, accuracy_score
# Creating instance of LinearRegression

model=LinearRegression()
# Fitting the data in the LinearRegression 

model.fit(X_train,y_train)
#calculate the Regression coefficients values
print(model.coef_)
print(model.intercept_)
predicted = model.predict(X_test)
print(X_test)
predicted.shape
dframe = pd.DataFrame(y_test,predicted)
dfr = pd.DataFrame({'Actual':y_test,'predicted':predicted})
print(dfr)
dfr.head()
dfr.tail()
model.score(X_test,y_test)
import math
print('Mean Absolute Error:',metrics.mean_absolute_error(y_test,predicted))
print('Mean Squared Error:',metrics.mean_squared_error(y_test,predicted))
print('Root Mean Squared Error:',math.sqrt(metrics.mean_squared_error(y_test,predicted)))
graph=dfr.head(25)
graph.plot(kind = 'bar')