-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossvalidation_LCOE.py
114 lines (84 loc) · 3.57 KB
/
Crossvalidation_LCOE.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 22 19:03:33 2019
@author: balderrama
"""
import pandas as pd
from sklearn.utils import shuffle
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score, cross_validate, cross_val_predict
from sklearn.model_selection import GridSearchCV
from sklearn.tree import export_graphviz
import matplotlib as mpl
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
import numpy as np
from sklearn import linear_model
from math import sqrt as sq
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
#%%
# Data manipulation
data = pd.read_excel('Databases/Data_Base.xls', index_col=0, Header=None)
#data = data.loc[data['Gap']< 1]
y = pd.DataFrame()
target='LCOE'
y[target] = data[target]
y=y.astype('float')
X = pd.DataFrame()
X['Renewable Invesment Cost'] = data['Renewable Unitary Invesment Cost']
X['Battery Unitary Invesment Cost'] = data['Battery Unitary Invesment Cost']
X['Deep of Discharge'] = data['Deep of Discharge']
X['Battery Cycles'] = data['Battery Cycles']
X['GenSet Unitary Invesment Cost'] = data['GenSet Unitary Invesment Cost']
X['Generator Efficiency'] = data['Generator Efficiency']
X['Low Heating Value'] = data['Low Heating Value']
X['Fuel Cost'] = data['Fuel Cost']
X['HouseHolds'] = data['HouseHolds']
X['Renewable Energy Unit Total'] = data['Renewable Energy Unit Total']
feature_list = list(X.columns)
y, X = shuffle(y, X, random_state=10)
#%%
# Linear regression
# Linear Cross validation
scoring = ['r2', 'neg_mean_absolute_error', 'neg_mean_squared_error'] #'r2' 'neg_mean_absolute_error' # 'neg_mean_squared_error'
for i in scoring:
lm = linear_model.LinearRegression(fit_intercept=True)
Results = cross_validate(lm, X, y, cv=5,return_train_score=True,n_jobs=-1
, scoring = i )
scores = Results['test_score']
score = scores.mean()
if i == 'neg_mean_squared_error':
score = sq(-score)
score = round(score,3)
print(i + ' for the linear regression in cross validatiion is ' + str(score))
else:
score = round(score,3)
print(i + ' for the linear regression in cross validatiion is ' + str(score))
Results = pd.DataFrame(Results)
path = 'Results_Regressions/Kcross_valiadation_LR_LCOE' + '_' + i + '.csv'
Results.to_csv(path)
#%%
# Gaussian Process
# Cross Validation results
scoring = ['r2', 'neg_mean_absolute_error', 'neg_mean_squared_error'] #'r2' 'neg_mean_absolute_error' # 'neg_mean_squared_error'
for i in scoring:
l = [1,1,1,1,1,1,1,1,1,1]
#kernel = (C()**2)*RBF(l)
kernel = RBF(l)
gp = GaussianProcessRegressor(kernel=kernel,optimizer = 'fmin_l_bfgs_b',
n_restarts_optimizer=3000)
Results = cross_validate(gp, X, y, cv=5,return_train_score=True,n_jobs=-1
, scoring = i )
scores = Results['test_score']
score = round(scores.mean(),4)
if i == 'neg_mean_squared_error':
score = sq(-score)
print(i + ' for the gaussian process with the test data set is ' + str(score))
else:
print(i + ' for the gaussian process with the test data set is ' + str(score))
Results = pd.DataFrame(Results)
path = 'Results_Regressions/Kcross_valiadation_GP_LCOE' + '_' + i + '.csv'
Results.to_csv(path)