我刚刚开始学习梯度增强中的损失函数。我想定义自己的自定义损失函数。我从python中的XML文件解析了它,然后使用if-else来检查用户提供的损失函数输入,但它显示传递给该函数的不受支持的字符串。请提出建议。
代码
x = iv_train
y = dv_train
dy = dv_train.shape[0]
def huber_loss(m, b, x, y, dy, c=2):
y_fit = m * x + b
t = abs((y - y_fit) / dy)
flag = t > c
return np.sum((~flag) * (0.5 * t ** 2) - (flag) * c * (0.5 * c - t), -1)
def ls_loss(m, b, x, y):
y_fit = m * x + b
return np.sum(((y - y_fit)) ** 2, -1)
from xml.dom import minidom
xmldoc = minidom.parse('config.xml')
lossfn = xmldoc.getElementsByTagName('lossfn')[0].childNodes[0].nodeValue.strip()
if (lossfn == 'huber'):
lossfn = huber_loss
else :
lossfn = ls_loss
params = {'n_estimators': n_estimators, 'max_depth': max_depth, 'min_samples_split': min_samples_split, 'subsample' : subsample,
'learning_rate': learning_rate , 'loss': lossfn ,'min_samples_leaf': 1, 'max_features' : max_features}
model = ensemble.GradientBoostingRegressor(**params)
model.fit(iv_train, dv_train)
错误
This is the error I'm getting while running model.fit
....model.fit(iv_train, dv_train)
C:\Users\dell\Anaconda3\lib\site-packages\sklearn\utils\validation.py:761: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
Traceback (most recent call last):
File "<ipython-input-9-addb50fde228>", line 1, in <module>
model.fit(iv_train, dv_train)
File "C:\Users\dell\Anaconda3\lib\site-packages\sklearn\ensemble\gradient_boosting.py", line 1414, in fit
self._check_params()
File "C:\Users\dell\Anaconda3\lib\site-packages\sklearn\ensemble\gradient_boosting.py", line 1218, in _check_params
raise ValueError("Loss '{0:s}' not supported. ".format(self.loss))
TypeError: unsupported format string passed to function.__format__