为什么我的 sigmoid 函数 return 值不在区间 ]0,1[ 内?
Why does my sigmoid function return values not in the interval ]0,1[?
我正在 Python 中使用 numpy 实现逻辑回归。我生成了以下数据集:
# class 0:
# covariance matrix and mean
cov0 = np.array([[5,-4],[-4,4]])
mean0 = np.array([2.,3])
# number of data points
m0 = 1000
# class 1
# covariance matrix
cov1 = np.array([[5,-3],[-3,3]])
mean1 = np.array([1.,1])
# number of data points
m1 = 1000
# generate m gaussian distributed data points with
# mean and cov.
r0 = np.random.multivariate_normal(mean0, cov0, m0)
r1 = np.random.multivariate_normal(mean1, cov1, m1)
X = np.concatenate((r0,r1))
现在我已经借助以下方法实现了sigmoid函数:
def logistic_function(x):
""" Applies the logistic function to x, element-wise. """
return 1.0 / (1 + np.exp(-x))
def logistic_hypothesis(theta):
return lambda x : logistic_function(np.dot(generateNewX(x), theta.T))
def generateNewX(x):
x = np.insert(x, 0, 1, axis=1)
return x
应用逻辑回归后,我发现最好的 thetas 是:
best_thetas = [-0.9673200946417307, -1.955812236119612, -5.060885703369424]
但是,当我对这些 thetas 应用逻辑函数时,输出是不在区间 [0,1]
内的数字
示例:
data = logistic_hypothesis(np.asarray(best_thetas))(X)
print(data
结果如下:
[2.67871968e-11 3.19858822e-09 3.77845881e-09 ... 5.61325410e-03
2.19767618e-01 6.23288747e-01]
有人可以帮助我了解我的实施出了什么问题吗?我不明白为什么我会得到这么大的价值。 sigmoid函数不是应该只给出[0,1]区间的结果吗?
有,就在scientific notation。
'e' Exponent notation. Prints the number in scientific notation using
the letter ‘e’ to indicate the exponent.
>>> a = [2.67871968e-11, 3.19858822e-09, 3.77845881e-09, 5.61325410e-03]
>>> [0 <= i <= 1 for i in a]
[True, True, True, True]
我正在 Python 中使用 numpy 实现逻辑回归。我生成了以下数据集:
# class 0:
# covariance matrix and mean
cov0 = np.array([[5,-4],[-4,4]])
mean0 = np.array([2.,3])
# number of data points
m0 = 1000
# class 1
# covariance matrix
cov1 = np.array([[5,-3],[-3,3]])
mean1 = np.array([1.,1])
# number of data points
m1 = 1000
# generate m gaussian distributed data points with
# mean and cov.
r0 = np.random.multivariate_normal(mean0, cov0, m0)
r1 = np.random.multivariate_normal(mean1, cov1, m1)
X = np.concatenate((r0,r1))
现在我已经借助以下方法实现了sigmoid函数:
def logistic_function(x):
""" Applies the logistic function to x, element-wise. """
return 1.0 / (1 + np.exp(-x))
def logistic_hypothesis(theta):
return lambda x : logistic_function(np.dot(generateNewX(x), theta.T))
def generateNewX(x):
x = np.insert(x, 0, 1, axis=1)
return x
应用逻辑回归后,我发现最好的 thetas 是:
best_thetas = [-0.9673200946417307, -1.955812236119612, -5.060885703369424]
但是,当我对这些 thetas 应用逻辑函数时,输出是不在区间 [0,1]
内的数字示例:
data = logistic_hypothesis(np.asarray(best_thetas))(X)
print(data
结果如下:
[2.67871968e-11 3.19858822e-09 3.77845881e-09 ... 5.61325410e-03
2.19767618e-01 6.23288747e-01]
有人可以帮助我了解我的实施出了什么问题吗?我不明白为什么我会得到这么大的价值。 sigmoid函数不是应该只给出[0,1]区间的结果吗?
有,就在scientific notation。
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent.
>>> a = [2.67871968e-11, 3.19858822e-09, 3.77845881e-09, 5.61325410e-03]
>>> [0 <= i <= 1 for i in a]
[True, True, True, True]