forked from dlzams/SVR-powerconsumption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvr_powerconsumption.py
159 lines (117 loc) · 4.81 KB
/
svr_powerconsumption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""SVR-powerconsumption.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/115mxaDALVG0U-iJQyCJOeXqjQl6m2eW3
## SVM for Regression
This Support Vector Machine (SVM) method for regression is applied to analyze the "Power consumption of Tetouan City" dataset. This dataset includes various attributes such as DateTime, Temperature, Humidity, Wind Speed, general diffuse flows, diffuse flows, and power consumption data in three different distribution zones in Tetouan, Northern Morocco.
"""
# Data Preprocessing
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.linear_model import LinearRegression
# Load the dataset
data = pd.read_csv("dataset.csv")
data = data.sample(500).reset_index(drop=True) # display random samples based on filters
data = data.drop('DateTime', axis=1)
# define the target column
target_column = 'Zone 1 Power Consumption'
# separate features (X) and target (y)
X = data.drop(target_column, axis=1)
y = data[target_column]
# split the data into a training set (80%) and a testing set (20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
data.describe()
data.isnull().sum() # check to see the number of null data
# visualize the data
plt.figure(figsize=(12, 6))
# plot the training data
plt.scatter(X_train['Temperature'], y_train, color='blue', label='Training Data')
# plot the testing data
plt.scatter(X_test['Temperature'], y_test, color='red', label='Testing Data')
plt.xlabel('Temperature')
plt.ylabel('Zone 1 Power Consumption')
plt.title('Scatter Plot of Temperature vs. Zone 1 Power Consumption')
plt.legend()
plt.show()
# visualize the data
plt.figure(figsize=(12, 6))
for feature in data.columns[:-3]: # exclude the last column (target)
plt.scatter(data[feature], data['Zone 1 Power Consumption'], label=feature)
plt.xlabel('Feature')
plt.ylabel('Zone 1 Power Consumption')
plt.title('Perbandingan Fitur dengan Zone 1 Power Consumption')
plt.legend()
plt.show()
# Baseline Model dan Model Exploration (at least 3 scheme)
#Model 1 - Linear
model_linear = LinearRegression()
model_linear.fit(X_train, y_train)
y_pred_linear = model_linear.predict(X_test) #checking predict dari model
#Model 2 - RBF
model_rbf = SVR(kernel='rbf') #baseline
param_grid = [
{'C': [0.1, 0.5, 1, 10, 100],
'gamma': ['scale', 1, 0.1, 0.01]}
]
grid_search = GridSearchCV(model_rbf, param_grid, cv=5)
grid_search.fit(X_train_scaled, y_train)
model_rbf = grid_search.best_estimator_
model_rbf.fit(X_train_scaled, y_train) # Training the model
y_pred_rbf = model_rbf.predict(X_test_scaled) # Make predictions
#Model 3 - Polynom
model_poly = SVR(kernel='poly',degree=3) #baseline
param_grid = [
{'C': [0.1, 1, 10],
'gamma': ['scale', 0.1, 1, 10]}
]
grid_search = GridSearchCV(model_poly, param_grid, cv=2)
grid_search.fit(X_train_scaled, y_train)
model_poly = grid_search.best_estimator_
model_poly.fit(X_train_scaled, y_train) # Training the model
y_pred_poly = model_poly.predict(X_test_scaled) # Make predictions
# Evaluation
mse_linear = mean_squared_error(y_test, y_pred_linear)
mse_linear_dec = mse_linear / 10**8
r2_linear = r2_score(y_test, y_pred_linear)
mse_rbf = mean_squared_error(y_test, y_pred_rbf)
mse_rbf_dec = mse_rbf / 10**8
r2_rbf = r2_score(y_test, y_pred_rbf)
mse_poly = mean_squared_error(y_test, y_pred_poly)
mse_poly_dec = mse_poly / 10**8
r2_poly = r2_score(y_test, y_pred_poly)
print("Model dengan Kernel Linear:")
print(f"Mean Squared Error (MSE) :{mse_linear_dec:.9f}")
print("R-squared (R2) Score:", r2_linear)
print("\nModel dengan Kernel RBF:")
print(f"Mean Squared Error (MSE) :{mse_rbf_dec:.9f}")
print("R-squared (R2) Score:", r2_rbf)
print("\nModel dengan Kernel Polinom:")
print(f"Mean Squared Error (MSE) :{mse_poly_dec:.9f}")
print("R-squared (R2) Score:", r2_poly)
# obtain MSE (Mean Squared Error) and R2 (R-squared) results for the tested models
mse_values = [mse_linear_dec, mse_rbf_dec, mse_poly_dec]
r2_values = [r2_linear, r2_rbf, r2_poly]
model_names = ['Linear', 'RBF', 'Poly']
# Plot MSE
plt.figure(figsize=(10, 6))
plt.bar(model_names, mse_values, color='b')
plt.xlabel('Model')
plt.ylabel('Mean Squared Error (MSE)')
plt.title('Evaluasi MSE untuk Model Linear, RBF, dan Poly')
plt.show()
# Plot R2
plt.figure(figsize=(10, 6))
plt.bar(model_names, r2_values, color='g')
plt.xlabel('Model')
plt.ylabel('R-squared (R2)')
plt.title('Evaluasi R2 untuk Model Linear, RBF, dan Poly')
plt.show()