【问题标题】:Simple logistic regression简单逻辑回归
【发布时间】:2018-04-10 11:49:05
【问题描述】:

我试图解决二维分类的简单线性回归问题。 这里我有一个特征矩阵 X_expanded,其形状为 [826,6]。为了对物体进行分类,我们将获得物体属于“1”类的概率。为了预测概率,我们将使用线性模型和逻辑函数的输出。 这是计算概率的函数。

def probability(X, w):
    """
    Given input features and weights
    return predicted probabilities of y==1 given x, P(y=1|x), see description above

    Don't forget to use expand(X) function (where necessary) in this and subsequent functions.

    :param X: feature matrix X of shape [n_samples,6] (expanded)
    :param w: weight vector w of shape [6] for each of the expanded features
    :returns: an array of predicted probabilities in [0,1] interval.
    """

    # TODO:<your code here>
    X = X_expanded
    m = X.shape[0]
    w = np.zeros((m,1))
    Z = np.dot(w.T,X)
    P = 1./(1+np.exp(-Z))

    return P

对于一个简单的测试:

dummy_weights = np.linspace(-1, 1, 6)
ans_part1 = probability(X_expanded[:1, :], dummy_weights)[0]

但它总是返回array([ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])

有什么建议吗?

【问题讨论】:

  • w = np.zeros((m,1)) 返回零数组,Z = np.dot(wT,X) 将始终返回 0。所以 P = 1./(1+np .exp(0)) 将始终为 0.5。
  • 那么我应该如何初始化权重向量呢?
  • np.random.rand(m, 1)

标签: python machine-learning linear-regression


【解决方案1】:

由于您已将权重初始化为零,因此 Z = np.dot(w.T,X) 将为 0,而 sigmoid 函数将始终返回 0.5。您需要随机初始化权重。可以通过以下方式完成:

dummy_weights = np.random.rand(m, 1)

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2014-04-24
    • 2018-05-06
    • 2019-11-07
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多