Machine Learning, Minor Project, Electricity Power Consumption

Machine Learning Project – Electricity Power Consumption

Code :

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import plotly.tools as tls
from plotly.offline import download_plotlyjs, init_notebook_mode,  plot
import plotly.graph_objs as gp
from plotly.graph_objs import *

init_notebook_mode()

dataset =pd.read_csv("C:\\ClassStudies_Python//Power.csv")
x=dataset.iloc[:,:-1].values
y=dataset.iloc[:,6].values

''' Ploting Bar '''
plt.bar(x[:,0],y)
plt.title("Power Consumption (Day)")
plt.xlabel("Appliance of each Room ")
plt.ylabel("Power Consumption ")
plt.show()
figure = tls.make_subplots(rows=2, cols=2)

#Bar plot
trace1 = gp.Bar(
    x=x[:,0],
    y=y
)

layout = Layout(title="Power Consumption (Day)",yaxis=dict(title='Power Consumption')
    ,xaxis=dict(title='Appliance of each Room '),
    showlegend=False,
    height=600,
    width=600,
)
data1 = [trace1]
fig = dict( data=data1, layout=layout )
figure.append_trace(fig['data'][0], 1, 1)

#Encoding categorical dataset
#Encoding the Independent Variable
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder= LabelEncoder()
x[:,1]=labelencoder.fit_transform(x[:,1])
onehotencoder=OneHotEncoder(categorical_features=[1])
x=onehotencoder.fit_transform(x).toarray()


#Avoiding the Dummy Variable Trap 
x=x[:,1:]

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)


#Fitting Multiple Linear Regresssion to the Training set
from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(x_train,y_train)

y_pred=regressor.predict(x_test)

from sklearn.metrics import r2_score
plt.scatter(y_pred,y_test,c='cyan')
#plt.plot(y_pred,y_test,c='c')
plt.title("Electricty Power Consumption (Prediction)")
plt.show()

#Scatter
trace1 = gp.Scatter(
    x=y_pred,
    y=y_test,
    mode='markers',
    marker=dict(
    )
)
layout = Layout(title="Electricity Power Consumption",yaxis=dict(title='Y_Pred'),xaxis=dict(title='Y_test'),
    showlegend=False,
    height=600,
    width=600,
)

data = [trace1]
fig2 = dict( data=data, layout=layout )
figure.append_trace(fig2['data'][0], 1, 2)
plot(figure)
fig=plt.figure()

ax1=fig.add_subplot(1,1,1)
ax1.plot(y_pred,c='blue')
ax2=fig.add_subplot(1,1,1)
ax2.plot(y_test,linestyle='--',c='grey')

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top