-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauss_regression.py
87 lines (83 loc) · 3.87 KB
/
gauss_regression.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
import numpy as np
import bisect
from aquire_history_data import CONTINUOUS_PARAM, DISCRETE_PARAM, CONTINUOUS_PARAM_RANGE, DISCRETE_PARAM_RANGE
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
from var_blk_size import SECT_SIZE, ZONE_SIZE
class gauss:
def __init__(self, data, param):
self.datas = data
self.params = param
self.param_data_array = np.array([data_series[param].tolist() for data_series in data])
self.target = np.array([target[-1] for target in data])
# print(self.param_data_array)
# print(self.target)
def train_model(self):
# 核函数
kernel = C(0.1, (0.001, 0.1)) * RBF(0.5, (1e-4, 10))
# 高斯模型
self.reg = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10, alpha=0.1)
# print(self.param_data_array)
# print(self.target)
# 训练模型
self.reg.fit(self.param_data_array, self.target)
def recommend(self):
# 训练模型
self.train_model()
# 在最好的参数附近进行修改预测
max_target_index = np.argmax(self.target)
max_param = self.datas[max_target_index]
recommend_param = max_param[:-1]
max_target = max_param[-1]
res = recommend_param
# 调参顺序按照选择参数的重要性进行调参
for param in self.params:
variable_range = []
# 离散参数尝试遍历值
if param in DISCRETE_PARAM:
variable_range = DISCRETE_PARAM_RANGE[param]
variable_range = [value.value for value in variable_range]
# 连续参数尝试按照比例遍历范围
elif param in CONTINUOUS_PARAM:
min_v, max_v = CONTINUOUS_PARAM_RANGE[param]["min"], CONTINUOUS_PARAM_RANGE[param]["max"]
interval = (max_v-min_v) * 0.05
now_v = recommend_param[param]
variable_range = [(now_v + i * interval) for i in range(-10, 11) if i != 0]
variable_range = [num for num in variable_range if num >= min_v and num <= max_v]
# 属于块参数 选择与预测值最接近的值
else:
sz = recommend_param[param]
if param == "sect_size":
position = bisect.bisect(SECT_SIZE, sz)
if position == len(SECT_SIZE):
position = len(SECT_SIZE)-1
elif position == -1:
position = 0
recommend_param[param] = SECT_SIZE[position]
elif param == "zone_size":
position = bisect.bisect(ZONE_SIZE, sz)
if position == len(ZONE_SIZE):
position = len(ZONE_SIZE)-1
elif position == -1:
position = 0
recommend_param[param] = ZONE_SIZE[position]
else:
print("error in block param!")
# 调整参数
for value in variable_range:
if max_param[param] == value:
continue
recommend_param[param] = value
predict_x = np.array(recommend_param[self.params].tolist()).reshape(1, -1)
output, err = self.reg.predict(predict_x, return_std=True)
if output > max_target:
print(
"now the predicted {} value is {}, between {} and {}".format(param, output,
(1 - 1.96 * err) * output, (1 + 1.96 * err) * output))
res = recommend_param
max_param = output
return res
# xset, yset = np.meshgrid(np.arange(1, 6, 0.5), np.arange(1, 6, 0.5))
# print(xset)
# print(yset)
# print(np.c_[xset.ravel(), yset.ravel()])