在使用逻辑斯蒂回归时,报了栈溢出错误。
def logistic(x):
return 1.0 / (1 + np.exp(-x))
RuntimeWarning: overflow encountered in exp return 1.0 / (1 + np.exp(-x))
参照网上的做法,对x做判断,
def logistic(x):
if x>=0: #对sigmoid函数优化,避免出现极大的数据溢出
return 1.0 / (1 + np.exp(-x))
else:
return np.exp(x)/(1+np.exp(x))
此时又报了另一个错误
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
x的值不止一个,需要对所有的x都进行判断,按照报错后给的提示修改即可。
def logistic(x):
if np.all(x>=0): #对sigmoid函数优化,避免出现极大的数据溢出
return 1.0 / (1 + np.exp(-x))
else:
return np.exp(x)/(1+np.exp(x))
all,any用法
>>> help(all)
all(...)
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
>>> help(any)
any(...)
any(iterable) -> bool
Return True if bool(x) is True for any x in the iterable.