简体   繁体   English

为什么我的 sigmoid 函数返回的值不在区间 ]0,1[ 中?

[英]Why does my sigmoid function return values not in the interval ]0,1[?

I am implementing logistic regression in Python with numpy.我正在用 numpy 在 Python 中实现逻辑回归。 I have generated the following data set:我生成了以下数据集:

# 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))

Now I have implemented the sigmoid function with the aid of the following methods:现在我已经借助以下方法实现了 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 

After applying logistic regression, I found out that the best thetas are:应用逻辑回归后,我发现最好的 theta 是:

best_thetas = [-0.9673200946417307, -1.955812236119612, -5.060885703369424]

However, when I apply the logistic function with these thetas, then the output is numbers that are not inside the interval [0,1]但是,当我对这些 theta 应用逻辑函数时,输出是不在区间 [0,1] 内的数字

Example:例子:

data = logistic_hypothesis(np.asarray(best_thetas))(X)
print(data

This gives the following result:这给出了以下结果:

[2.67871968e-11 3.19858822e-09 3.77845881e-09 ... 5.61325410e-03
 2.19767618e-01 6.23288747e-01]

Can someone help me understand what has gone wrong with my implementation?有人可以帮助我了解我的实施出了什么问题吗? I cannot understand why I am getting such big values.我不明白为什么我会得到这么大的价值。 Isnt the sigmoid function supposed to only give results in the [0,1] interval? sigmoid 函数不应该只给出 [0,1] 区间的结果吗?

It does, it's just in scientific notation .确实如此,它只是采用科学记数法

'e' Exponent notation. 'e' 指数符号。 Prints the number in scientific notation using the letter 'e' to indicate the exponent.使用字母“e”表示指数以科学记数法打印数字。

>>> a = [2.67871968e-11, 3.19858822e-09, 3.77845881e-09, 5.61325410e-03]
>>> [0 <= i <= 1 for i in a]
[True, True, True, True]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 output 层的 sigmoid function 获得值 0 或 1 {0,1} 而不是在 [0,1] 中获得值 - Why output layer of sigmoid function gets value 0 or 1 {0,1} instead of getting value in [0,1] 为什么我的递归函数返回“None”? - Why does my recursive function return “None”? 为什么我的 function 不返回列表? - Why my function does not return the list? 为什么我的递归 function 返回 None? - Why does my recursive function return None? Python Numpy:numpy.exp(x)如何将此Sigmoid函数的返回值强制转换为ndarray? - Python Numpy: How does numpy.exp(x) coerce the return value of this sigmoid function into an ndarray? 为什么这个 python 递归 function 会按照它的顺序返回值? - Why does this python recursive function return the values in the order that it does? 为什么Sigmoid函数未在散点图上显示该线? - Why is the Sigmoid function not displaying the line on the scatter plot? 为什么skimage.imread()不为我的bmp返回RGB值? - Why does skimage.imread() not return RGB values for my bmp? 为什么此函数在重复调用时返回不同的值? - Why does this function return different values on repeated calls? 为什么这个Pandas系列的功能没有返回任何值? - Why does this Pandas series where function not return any values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM