# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# -

ccpp = pd.read_excel("~/Downloads/Folds5x2_pp.xlsx")
ccpp

#Rename Columns to make them better descriptive and unddable
rename_columns = ccpp.rename(columns = {"V": "Exhaust_Vacuum", "AP": "Ambient_Pressure", "RH": "Relative_Humidity", "AT": "Temperature", "PE": "Electricity_output"})
rename_columns

#Check for missing Values in the dataset
ccpp.isna().sum()

rename_columns.info()

rename_columns.describe()

#Check for Outliers of any sort
fig = plt.figure(figsize = (10, 7))
plt.boxplot(rename_columns)

rename_columns.shape

fig= sns.lmplot(x = 'Relative_Humidity', y='Electricity_output',data=rename_columns)
plt.title("Electricity_output VS Relative_Humidity")

fig= sns.lmplot(x = 'Ambient_Pressure', y='Electricity_output',data=rename_columns)
plt.title("Ambient_Pressure VS Electrical_output")

fig= sns.lmplot(x = 'Temperature', y='Electricity_output',data=rename_columns)
plt.title("Electricity_output VS Temperature")

fig= sns.lmplot(x = 'Exhaust_Vacuum', y='Electricity_output',data=rename_columns)
plt.title("Electricity_output VS Exhaust_Vacuum")

##HeatMap Can also be used to check the correlation between the dependent variable and the Independent variable
plt.figure(figsize=(15, 10))
sns.heatmap(rename_columns.corr(), annot=True)

y = rename_columns['Electricity_output']
X = rename_columns.drop(columns=['Electricity_output'], axis=1)
y.shape
X.shape

#Splitting the DataSet into Train and Test. The second Train and Set will be used for validation
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.20, random_state=45)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=1)

# +
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(random_state=1, max_depth=4)
model.fit(rename_columns,x)

RandomForestRegressor(max_depth=4, random_state=1)
# -

features = rename_columns.columns
importances = model.feature_importances_
indices = np.argsort(importances)[-4:]
plt.title('Feature Importances')
plt.barh(range(len(indices)), importances[indices], color='green', align='center')
plt.yticks(range(len(indices)), [features[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()

model = LinearRegression()
power_output = model.fit(X_val, y_val)
power_output

coef_deter = power_output.score(X_val, y_val)
coef_deter

1 - (1-model.score(X_val, y_val)) * (len(y_val)-1)/(len(y_val)-X_val.shape[1]-1)

intercept = model.coef_
intercept

X_predict = power_output.predict(X_test)
X_predict

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
print("MAE : ",mean_absolute_error(y_test, X_predict))
print("MSE : ", mean_squared_error(y_test, X_predict))
print("R2 Sore : ", r2_score(y_test, X_predict))

from sklearn.linear_model import Ridge

model_ = Ridge()
power_output = model_.fit(X_val, y_val)
power_output

coef_deter = power_output.score(X_val, y_val)
coef_deter

1 - (1-model_.score(X_val, y_val)) * (len(y_val)-1)/(len(y_val)-X_val.shape[1]-1)

intercept = model_.coef_
intercept

X_predict = power_output.predict(X_test)
X_predict

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
print("MAE : ",mean_absolute_error(y_test, X_predict))
print("MSE : ", mean_squared_error(y_test, X_predict))
print("R2 Sore : ", r2_score(y_test, X_predict))

# +
train_score = []
test_score = []

random_state = 10

for i in range(random_state):
    X_train1, X_test1, y_train1, y_test1 = train_test_split(X_val, y_val, test_size = 0.30, random_state = i)

    linear_model_1 = LinearRegression()
    linear_model_1.fit(X_train1, y_train1)
    
    y_train_predict_mimax = linear_model_1.predict(X_train1)
    y_test_predict_minmax = linear_model_1.predict(X_test1)
    
    train_score = np.append(train_score, r2_score(y_train1, y_train_predict_mimax))
    test_score = np.append(test_score, r2_score(y_test1, y_test_predict_minmax))

print(X_train1.shape)
print(y_train1.shape)
# -

print('R2 train: %.2f +/- %.2f' % (np.mean(train_score),np.std(train_score)))

print('R2 train: %.2f +/- %.2f' % (np.mean(train_score),np.std(train_score)))

# +
from yellowbrick.regressor import ResidualsPlot

model = LinearRegression()
visualizer = ResidualsPlot(model, hist=True, qqplot=False)

visualizer.fit(X_train, y_train)  
visualizer.score(X_test, y_test)  
visualizer.show()
# -

*Train the model with the Linear Regression model since it performs better than the Ridge Regression

model = LinearRegression()
power_output = model.fit(X_train, y_train)

coef_deter = power_output.score(X_train, y_train)
coef_deter

intercept = model.coef_
intercept

y_predict = power_output.predict(X_test)

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
print("MAE : ",mean_absolute_error(y_test, y_predict))
print("MSE : ",mean_squared_error(y_test, y_predict))
print("R2 score : ",r2_score(y_test, y_predict))

From the R2 score above it shows that the variables in our regression equation can explain ~93% of the change in our output variable.*

StarcoderdataJupyterScriptsDedupFiltered