SVM - Práctica de regresión con SVR
Índice de contenido
python
import pandas as pd
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV, train_test_split
python
df = pd.read_csv("C:/Users/user/Desktop/energydata_complete.csv", parse_dates = ["date"], infer_datetime_format =True)
df
| date | Appliances | lights | T1 | RH_1 | T2 | RH_2 | T3 | RH_3 | T4 | ... | T9 | RH_9 | T_out | Press_mm_hg | RH_out | Windspeed | Visibility | Tdewpoint | rv1 | rv2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2016-01-11 17:00:00 | 60 | 30 | 19.890000 | 47.596667 | 19.200000 | 44.790000 | 19.790000 | 44.730000 | 19.000000 | ... | 17.033333 | 45.5300 | 6.600000 | 733.5 | 92.000000 | 7.000000 | 63.000000 | 5.300000 | 13.275433 | 13.275433 |
| 1 | 2016-01-11 17:10:00 | 60 | 30 | 19.890000 | 46.693333 | 19.200000 | 44.722500 | 19.790000 | 44.790000 | 19.000000 | ... | 17.066667 | 45.5600 | 6.483333 | 733.6 | 92.000000 | 6.666667 | 59.166667 | 5.200000 | 18.606195 | 18.606195 |
| 2 | 2016-01-11 17:20:00 | 50 | 30 | 19.890000 | 46.300000 | 19.200000 | 44.626667 | 19.790000 | 44.933333 | 18.926667 | ... | 17.000000 | 45.5000 | 6.366667 | 733.7 | 92.000000 | 6.333333 | 55.333333 | 5.100000 | 28.642668 | 28.642668 |
| 3 | 2016-01-11 17:30:00 | 50 | 40 | 19.890000 | 46.066667 | 19.200000 | 44.590000 | 19.790000 | 45.000000 | 18.890000 | ... | 17.000000 | 45.4000 | 6.250000 | 733.8 | 92.000000 | 6.000000 | 51.500000 | 5.000000 | 45.410389 | 45.410389 |
| 4 | 2016-01-11 17:40:00 | 60 | 40 | 19.890000 | 46.333333 | 19.200000 | 44.530000 | 19.790000 | 45.000000 | 18.890000 | ... | 17.000000 | 45.4000 | 6.133333 | 733.9 | 92.000000 | 5.666667 | 47.666667 | 4.900000 | 10.084097 | 10.084097 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 19730 | 2016-05-27 17:20:00 | 100 | 0 | 25.566667 | 46.560000 | 25.890000 | 42.025714 | 27.200000 | 41.163333 | 24.700000 | ... | 23.200000 | 46.7900 | 22.733333 | 755.2 | 55.666667 | 3.333333 | 23.666667 | 13.333333 | 43.096812 | 43.096812 |
| 19731 | 2016-05-27 17:30:00 | 90 | 0 | 25.500000 | 46.500000 | 25.754000 | 42.080000 | 27.133333 | 41.223333 | 24.700000 | ... | 23.200000 | 46.7900 | 22.600000 | 755.2 | 56.000000 | 3.500000 | 24.500000 | 13.300000 | 49.282940 | 49.282940 |
| 19732 | 2016-05-27 17:40:00 | 270 | 10 | 25.500000 | 46.596667 | 25.628571 | 42.768571 | 27.050000 | 41.690000 | 24.700000 | ... | 23.200000 | 46.7900 | 22.466667 | 755.2 | 56.333333 | 3.666667 | 25.333333 | 13.266667 | 29.199117 | 29.199117 |
| 19733 | 2016-05-27 17:50:00 | 420 | 10 | 25.500000 | 46.990000 | 25.414000 | 43.036000 | 26.890000 | 41.290000 | 24.700000 | ... | 23.200000 | 46.8175 | 22.333333 | 755.2 | 56.666667 | 3.833333 | 26.166667 | 13.233333 | 6.322784 | 6.322784 |
| 19734 | 2016-05-27 18:00:00 | 430 | 10 | 25.500000 | 46.600000 | 25.264286 | 42.971429 | 26.823333 | 41.156667 | 24.700000 | ... | 23.200000 | 46.8450 | 22.200000 | 755.2 | 57.000000 | 4.000000 | 27.000000 | 13.200000 | 34.118851 | 34.118851 |
19735 rows × 29 columns
python
df.set_index("date", inplace=True)
python
df
| Appliances | lights | T1 | RH_1 | T2 | RH_2 | T3 | RH_3 | T4 | RH_4 | ... | T9 | RH_9 | T_out | Press_mm_hg | RH_out | Windspeed | Visibility | Tdewpoint | rv1 | rv2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||||||||||||
| 2016-01-11 17:00:00 | 60 | 30 | 19.890000 | 47.596667 | 19.200000 | 44.790000 | 19.790000 | 44.730000 | 19.000000 | 45.566667 | ... | 17.033333 | 45.5300 | 6.600000 | 733.5 | 92.000000 | 7.000000 | 63.000000 | 5.300000 | 13.275433 | 13.275433 |
| 2016-01-11 17:10:00 | 60 | 30 | 19.890000 | 46.693333 | 19.200000 | 44.722500 | 19.790000 | 44.790000 | 19.000000 | 45.992500 | ... | 17.066667 | 45.5600 | 6.483333 | 733.6 | 92.000000 | 6.666667 | 59.166667 | 5.200000 | 18.606195 | 18.606195 |
| 2016-01-11 17:20:00 | 50 | 30 | 19.890000 | 46.300000 | 19.200000 | 44.626667 | 19.790000 | 44.933333 | 18.926667 | 45.890000 | ... | 17.000000 | 45.5000 | 6.366667 | 733.7 | 92.000000 | 6.333333 | 55.333333 | 5.100000 | 28.642668 | 28.642668 |
| 2016-01-11 17:30:00 | 50 | 40 | 19.890000 | 46.066667 | 19.200000 | 44.590000 | 19.790000 | 45.000000 | 18.890000 | 45.723333 | ... | 17.000000 | 45.4000 | 6.250000 | 733.8 | 92.000000 | 6.000000 | 51.500000 | 5.000000 | 45.410389 | 45.410389 |
| 2016-01-11 17:40:00 | 60 | 40 | 19.890000 | 46.333333 | 19.200000 | 44.530000 | 19.790000 | 45.000000 | 18.890000 | 45.530000 | ... | 17.000000 | 45.4000 | 6.133333 | 733.9 | 92.000000 | 5.666667 | 47.666667 | 4.900000 | 10.084097 | 10.084097 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2016-05-27 17:20:00 | 100 | 0 | 25.566667 | 46.560000 | 25.890000 | 42.025714 | 27.200000 | 41.163333 | 24.700000 | 45.590000 | ... | 23.200000 | 46.7900 | 22.733333 | 755.2 | 55.666667 | 3.333333 | 23.666667 | 13.333333 | 43.096812 | 43.096812 |
| 2016-05-27 17:30:00 | 90 | 0 | 25.500000 | 46.500000 | 25.754000 | 42.080000 | 27.133333 | 41.223333 | 24.700000 | 45.590000 | ... | 23.200000 | 46.7900 | 22.600000 | 755.2 | 56.000000 | 3.500000 | 24.500000 | 13.300000 | 49.282940 | 49.282940 |
| 2016-05-27 17:40:00 | 270 | 10 | 25.500000 | 46.596667 | 25.628571 | 42.768571 | 27.050000 | 41.690000 | 24.700000 | 45.730000 | ... | 23.200000 | 46.7900 | 22.466667 | 755.2 | 56.333333 | 3.666667 | 25.333333 | 13.266667 | 29.199117 | 29.199117 |
| 2016-05-27 17:50:00 | 420 | 10 | 25.500000 | 46.990000 | 25.414000 | 43.036000 | 26.890000 | 41.290000 | 24.700000 | 45.790000 | ... | 23.200000 | 46.8175 | 22.333333 | 755.2 | 56.666667 | 3.833333 | 26.166667 | 13.233333 | 6.322784 | 6.322784 |
| 2016-05-27 18:00:00 | 430 | 10 | 25.500000 | 46.600000 | 25.264286 | 42.971429 | 26.823333 | 41.156667 | 24.700000 | 45.963333 | ... | 23.200000 | 46.8450 | 22.200000 | 755.2 | 57.000000 | 4.000000 | 27.000000 | 13.200000 | 34.118851 | 34.118851 |
19735 rows × 28 columns
python
df.dtypes
date datetime64[ns]
Appliances int64
lights int64
T1 float64
RH_1 float64
T2 float64
RH_2 float64
T3 float64
RH_3 float64
T4 float64
RH_4 float64
T5 float64
RH_5 float64
T6 float64
RH_6 float64
T7 float64
RH_7 float64
T8 float64
RH_8 float64
T9 float64
RH_9 float64
T_out float64
Press_mm_hg float64
RH_out float64
Windspeed float64
Visibility float64
Tdewpoint float64
rv1 float64
rv2 float64
dtype: object
python
regressor = SVR()
python
y = df.Appliances
python
X = df.drop("Appliances", axis=1)
python
X.columns
Index(['lights', 'T1', 'RH_1', 'T2', 'RH_2', 'T3', 'RH_3', 'T4', 'RH_4', 'T5',
'RH_5', 'T6', 'RH_6', 'T7', 'RH_7', 'T8', 'RH_8', 'T9', 'RH_9', 'T_out',
'Press_mm_hg', 'RH_out', 'Windspeed', 'Visibility', 'Tdewpoint', 'rv1',
'rv2'],
dtype='object')
python
X_train, X_test, y_train, y_test = train_test_split(X, y,
random_state = 42,
test_size = 0.15)
python
paramgrid = {"kernel": ["linear", "poly", "rbf", "sigmoid"],
"C": [0.2, 0.4, 0.6, 0.8, 1]}
python
gs = GridSearchCV(estimator = regressor,
param_grid = paramgrid,
scoring = None,
cv = None,
refit = True)
python
gs.fit(X_train, y_train)
GridSearchCV(estimator=SVR(),
param_grid={'C': [0.2, 0.4, 0.6, 0.8, 1],
'kernel': ['linear', 'poly', 'rbf', 'sigmoid']})
python
pd.DataFrame(gs.cv_results_)
| mean_fit_time | std_fit_time | mean_score_time | std_score_time | param_C | param_kernel | params | split0_test_score | split1_test_score | split2_test_score | split3_test_score | split4_test_score | mean_test_score | std_test_score | rank_test_score | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 40.752531 | 1.352108 | 2.373936 | 0.140572 | 0.2 | linear | {'C': 0.2, 'kernel': 'linear'} | 0.019653 | 0.010238 | 0.033227 | 0.043215 | 0.034625 | 0.028192 | 0.011728 | 5 |
| 1 | 18.326448 | 0.866918 | 2.413738 | 0.129136 | 0.2 | poly | {'C': 0.2, 'kernel': 'poly'} | -0.118780 | -0.115594 | -0.115704 | -0.108767 | -0.113247 | -0.114419 | 0.003328 | 13 |
| 2 | 19.781931 | 0.312770 | 2.794960 | 0.019261 | 0.2 | rbf | {'C': 0.2, 'kernel': 'rbf'} | -0.129083 | -0.125228 | -0.126925 | -0.118871 | -0.123578 | -0.124737 | 0.003455 | 15 |
| 3 | 21.603636 | 0.072545 | 3.587005 | 0.017782 | 0.2 | sigmoid | {'C': 0.2, 'kernel': 'sigmoid'} | -0.137131 | -0.132830 | -0.135788 | -0.127274 | -0.131937 | -0.132992 | 0.003429 | 20 |
| 4 | 41.414769 | 0.552810 | 1.692897 | 0.127614 | 0.4 | linear | {'C': 0.4, 'kernel': 'linear'} | 0.020779 | 0.011267 | 0.034262 | 0.044283 | 0.036474 | 0.029413 | 0.011818 | 4 |
| 5 | 18.166039 | 1.024226 | 2.379536 | 0.071229 | 0.4 | poly | {'C': 0.4, 'kernel': 'poly'} | -0.111933 | -0.109167 | -0.108241 | -0.102607 | -0.106765 | -0.107743 | 0.003071 | 10 |
| 6 | 23.986372 | 0.514323 | 3.469398 | 0.044869 | 0.4 | rbf | {'C': 0.4, 'kernel': 'rbf'} | -0.119906 | -0.116396 | -0.116779 | -0.109390 | -0.113987 | -0.115292 | 0.003500 | 14 |
| 7 | 24.208985 | 1.328853 | 3.954226 | 0.327962 | 0.4 | sigmoid | {'C': 0.4, 'kernel': 'sigmoid'} | -0.135667 | -0.131464 | -0.134193 | -0.125775 | -0.130426 | -0.131505 | 0.003422 | 19 |
| 8 | 55.259561 | 0.989018 | 1.674096 | 0.034429 | 0.6 | linear | {'C': 0.6, 'kernel': 'linear'} | 0.021643 | 0.011705 | 0.035039 | 0.045299 | 0.036830 | 0.030103 | 0.011921 | 3 |
| 9 | 16.357136 | 0.987940 | 2.138522 | 0.334483 | 0.6 | poly | {'C': 0.6, 'kernel': 'poly'} | -0.107240 | -0.104847 | -0.102930 | -0.098082 | -0.102331 | -0.103086 | 0.003033 | 8 |
| 10 | 19.806733 | 0.146611 | 2.820161 | 0.009880 | 0.6 | rbf | {'C': 0.6, 'kernel': 'rbf'} | -0.115550 | -0.112441 | -0.111675 | -0.105857 | -0.109917 | -0.111088 | 0.003188 | 12 |
| 11 | 22.374480 | 0.094818 | 3.743414 | 0.030038 | 0.6 | sigmoid | {'C': 0.6, 'kernel': 'sigmoid'} | -0.134220 | -0.130056 | -0.132545 | -0.124174 | -0.128895 | -0.129978 | 0.003448 | 18 |
| 12 | 64.584294 | 0.953269 | 1.651294 | 0.014635 | 0.8 | linear | {'C': 0.8, 'kernel': 'linear'} | 0.021894 | 0.011550 | 0.035023 | 0.046248 | 0.038090 | 0.030561 | 0.012323 | 2 |
| 13 | 15.359278 | 0.216740 | 1.953712 | 0.013808 | 0.8 | poly | {'C': 0.8, 'kernel': 'poly'} | -0.103437 | -0.101485 | -0.098904 | -0.094132 | -0.098330 | -0.099258 | 0.003152 | 7 |
| 14 | 19.679126 | 0.099504 | 2.811561 | 0.012094 | 0.8 | rbf | {'C': 0.8, 'kernel': 'rbf'} | -0.112886 | -0.109904 | -0.109041 | -0.103642 | -0.107450 | -0.108585 | 0.003039 | 11 |
| 15 | 22.378080 | 0.070287 | 3.765815 | 0.018436 | 0.8 | sigmoid | {'C': 0.8, 'kernel': 'sigmoid'} | -0.132739 | -0.128607 | -0.130842 | -0.122580 | -0.127290 | -0.128411 | 0.003462 | 17 |
| 16 | 75.441115 | 2.213122 | 1.738899 | 0.191793 | 1 | linear | {'C': 1, 'kernel': 'linear'} | 0.021846 | 0.011744 | 0.035227 | 0.046698 | 0.038177 | 0.030739 | 0.012410 | 1 |
| 17 | 15.253072 | 0.142732 | 1.980913 | 0.102427 | 1 | poly | {'C': 1, 'kernel': 'poly'} | -0.100448 | -0.098625 | -0.095828 | -0.090579 | -0.095173 | -0.096130 | 0.003367 | 6 |
| 18 | 19.578120 | 0.075016 | 2.799760 | 0.024994 | 1 | rbf | {'C': 1, 'kernel': 'rbf'} | -0.110324 | -0.107222 | -0.106138 | -0.100981 | -0.104646 | -0.105862 | 0.003070 | 9 |
| 19 | 22.398681 | 0.108951 | 3.797617 | 0.055737 | 1 | sigmoid | {'C': 1, 'kernel': 'sigmoid'} | -0.131244 | -0.127154 | -0.129186 | -0.121045 | -0.125743 | -0.126875 | 0.003458 | 16 |
python
gs.best_params_
{'C': 1, 'kernel': 'linear'}
python
gs.best_score_
0.0307386556178449
python
best_model = gs.best_estimator_
python
y_pred = best_model.predict(X_test)
python
pd.DataFrame({"y_test": y_test, "y_pred": y_pred})
| y_test | y_pred | |
|---|---|---|
| date | ||
| 2016-03-14 01:40:00 | 40 | 47.425845 |
| 2016-01-30 20:00:00 | 90 | 144.780909 |
| 2016-03-15 03:00:00 | 50 | 43.357670 |
| 2016-04-20 10:10:00 | 50 | 72.324751 |
| 2016-03-13 08:10:00 | 70 | 40.150215 |
| ... | ... | ... |
| 2016-04-25 13:30:00 | 50 | 82.308629 |
| 2016-04-09 07:20:00 | 30 | 52.122389 |
| 2016-02-12 05:40:00 | 60 | 48.252534 |
| 2016-05-01 06:10:00 | 60 | 58.874192 |
| 2016-04-07 16:00:00 | 50 | 79.774316 |
2961 rows × 2 columns
python
best_model.score(X_test, y_test)
0.05339127348678119
python
best_model.get_params()
{'C': 1,
'cache_size': 200,
'coef0': 0.0,
'degree': 3,
'epsilon': 0.1,
'gamma': 'scale',
'kernel': 'linear',
'max_iter': -1,
'shrinking': True,
'tol': 0.001,
'verbose': False}
