Java Logistic回归检验。不';t使用1或0收敛,但使用logit

Java Logistic回归检验。不';t使用1或0收敛,但使用logit,java,machine-learning,regression,Java,Machine Learning,Regression,我在这里使用线性回归代码创建了一个使用梯度下降的简单逻辑回归: 现在我简单地将其应用于逻辑回归,通过添加逻辑变换来改变假设:1/(1+e^(-z)),其中z是原始θ^T*X,而不是按总体规模进行缩放 当尝试测试我的结果时,我得到了一个令人困惑的行为:我将自变量(X)设置为一个随机数*一个期望的权重,将因变量(Y)设置为它们总和的logit,因此Y=logit(w0*1+w1*x1+w2*x2) 现在在这种情况下,它收敛到正确的答案,我可以恢复预期的权重。但很明显,我应该把Y设为0或1。但当我向

我在这里使用线性回归代码创建了一个使用梯度下降的简单逻辑回归:

现在我简单地将其应用于逻辑回归,通过添加逻辑变换来改变假设:1/(1+e^(-z)),其中z是原始θ^T*X,而不是按总体规模进行缩放

当尝试测试我的结果时,我得到了一个令人困惑的行为:我将自变量(X)设置为一个随机数*一个期望的权重,将因变量(Y)设置为它们总和的logit,因此Y=logit(w0*1+w1*x1+w2*x2)

现在在这种情况下,它收敛到正确的答案,我可以恢复预期的权重。但很明显,我应该把Y设为0或1。但当我向上或向下取整时,我不再收敛

我在这里生成培训数据:

@Test
public void testLogisticDescentMultiple() {
    //...

    //initialize Independent Xi
    //Going to create test data y= 10 + .5(x1) + .33(x2) 
    for( int x=0;x<NUM_EXAMPLES;x++) {
        independent.set(x, 0, 1); //x0 We always set this to 1 for the intercept
        independent.set(x, 1, random.nextGaussian()); //x1
        independent.set(x, 2, random.nextGaussian() ); //x2
    }

    //initialize dependent Yi
    for( int x=0;x<NUM_EXAMPLES;x++) {
        double val      = w0 +  (w1*independent.get(x,1)) + (w2*independent.get(x,2));
        double logitVal = logit( val );

        //Converges without this code block
        if( logitVal < 0.5 ) {
            logitVal = 0;
        }else {
            logitVal = 1;
        }
        //

        dependent.set(x, logitVal );
    }
    //...
}

public static double logit( double val ) {
    return( 1.0 / (1.0 + Math.exp(-val)));
}

//updated Logistic Regression
public DoubleMatrix1D logisticDescent(double         alpha,
                                      DoubleMatrix1D thetas,
                                      DoubleMatrix2D independent,
                                      DoubleMatrix1D dependent ) {
    Algebra algebra     = new Algebra();

    //hypothesis is 1/( 1+ e ^ -(theta(Transposed) * X))
    //start with theata(Transposed)*X
    DoubleMatrix1D hypothesies = algebra.mult( independent, thetas );

    //h = 1/(1+ e^-h)
    hypothesies.assign(new DoubleFunction() {
        @Override
        public double apply (double val) {
            return( logit( val ) );
        }
    });

    //hypothesis - Y
    //Now we have for each Xi, the difference between predicted by the hypothesis and the actual Yi
    hypothesies.assign(dependent, Functions.minus);

    //Transpose Examples(MxN) to NxM so we can matrix multiply by hypothesis Nx1
    DoubleMatrix2D transposed = algebra.transpose(independent);

    DoubleMatrix1D deltas     = algebra.mult(transposed, hypothesies );

    // thetas = thetas - (deltas*alpha)  in one step
    thetas.assign(deltas, Functions.minusMult(alpha));

    return( thetas );
}
@测试
public void testlogisticsdescentmultiple(){
//...
初始化独立的席
//创建测试数据y=10+.5(x1)+.33(x2)

对于(Xi=0;席)使用了CurSera机器学习课程的样本数据集,这确实收敛了。所以我猜用我的方法生成测试数据是不有效的。使用WEKA对CurSera数据集进行逻辑回归,得到的答案是相同的,除了符号被所有参数翻转。斯蒂昂。