在手寫Adaboost算法時,基本分類器
G
m
(
x
)
G_{m}(x)
Gm?(x)的系數(shù)為:
α
m
=
1
2
log
?
1
?
e
m
e
m
\alpha_m=\frac{1}{2}\log\frac{1-e_m}{e_m}
αm?=21?logem?1?em??
我們用代碼實現(xiàn)為:(只展示部分代碼)
estimator.alpha=0.5*np.log((1-min_error)/(min_error))
preds=np.ones(np.shape(y))
negative_idx=(estimator.lable*X[:,estimator.feature_index]<estimator.lable*estimator.threshold)
preds[negative_idx]=-1
w*=np.exp(-estimator.alpha*y*preds)
w/=np.sum(w)
self.estimators.append(estimator)
但是在運行后報錯:
F:\anaconda\envs\sklearn-env\python.exe F:/PycharmProject/Adaboost_numpy_demo.py
Traceback (most recent call last):
File "F:\PycharmProject\Adaboost_numpy_demo.py", line 116, in <module>
clf.fit(X_train,y_train)
File "F:\PycharmProject\Adaboost_numpy_demo.py", line 80, in fit
estimator.alpha=0.5*np.log((1-min_error)/(min_error))
ZeroDivisionError: division by zero
Process finished with exit code 1
這里的錯誤很明顯,就是我們的公式計算中, e m e_m em?可能是一個非常小的數(shù)字,在進(jìn)行計算時就會變?yōu)?,出現(xiàn)錯誤,這樣一來,改正的方法也很明確:
estimator.alpha=0.5*np.log((1-min_error)/(min_error+1e-9))
這樣的運行結(jié)果就不會報錯了!文章來源:http://www.zghlxwxcb.cn/news/detail-627001.html
問題解決!文章來源地址http://www.zghlxwxcb.cn/news/detail-627001.html
到了這里,關(guān)于提示錯誤信息:ZeroDivisionError: division by zero的解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!