#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 07:44:07 2019

@author: PRIYANKA
"""
##################### IMPORTING PAKAGES ############################
import pandas as pd
import numpy as np



df=pd.read_excel("Case_3_LR_manufacturing.xls")# READING EXCEL FILE USING PD
df.dtypes   # DATATYPES OF FETURE
df.columns
df.columns=['Cement','Blast Furnace Slag','Fly Ash','Water','Superplasticizer',
            'Coarse Aggregate','Fine Aggregate','Age (day)','Concrete compressive strength'
        ]
df.head()
df2=df.describe()
df.shape
df2.to_csv('manufacturing.csv')
df.median()
df.mean()
df3=df.mode()
df.skew()
df.corr()
df.hist()
df['Age (day)'].hist()
df['Age (day)']
df.boxplot(column=['Concrete compressive strength'])
df.boxplot(column=['Age (days)'])






############################  OUTLIERS #############################

Q1 = df.quantile(0.05)
Q3=df.quantile(0.95)
IQR = Q3 - Q1

index_outliers= df.loc[((df< (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)].index.tolist() #Index of the outliers 
outlier_values=df.loc[index_outliers]    #List of values of outliers
outlier_values
remove_outliers=df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]   # Removing outliers from dataframe and get the dataframe without outliers


df=remove_outliers.reset_index()   # Resetting the index 
df_final=df.drop('index',axis=1)













############################# ML MODEL ####################################

from sklearn.model_selection import train_test_split
train_set, test_set = train_test_split(df_final, test_size=0.2, random_state=42)

X=train_set.drop('Concrete compressive strength',axis=1)
y=train_set['Concrete compressive strength']

X_test=test_set.drop('Concrete compressive strength',axis=1)
y_test=test_set['Concrete compressive strength']




from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X,y)

from sklearn.metrics import mean_squared_error
import numpy as np
y_pred = lin_reg.predict(X)
lin_mse = mean_squared_error(y, y_pred)
lin_rmse = np.sqrt(lin_mse)
lin_rmse

from sklearn.metrics import r2_score
r2_score(y, y_pred)   # 0.61045935279395924






y_lin_test = lin_reg.predict(X_test)
lin_mse_lin_test = mean_squared_error(y_test, y_lin_test)
lin_rmse_lin_test = np.sqrt(lin_mse_lin_test)
lin_rmse_lin_test

from sklearn.metrics import r2_score
r2_score(y_test,y_lin_test) # 0.62754160554290184

