Ridge Regression: function beta() does not return a valid value

30 views
Skip to first unread message

Paul

unread,
Jul 9, 2012, 4:07:02 PM7/9/12
to mlpy-g...@googlegroups.com
I just tested the implementation of ridge regression in mlpy. To my suprise it seems as if the slope vector beta is not returned correctly. Instead it returns a [.0]. Anyway prediction of linear data works fine. Here is some sample code (you can copy and paste it):

import numpy as np

import mlpy

from numpy.random import *


def test_ridge_regression():

    

    beta1 = 3

    beta0 = 2

    num_datapoints = 1000

    

    X = np.arange(num_datapoints).reshape(num_datapoints,1)

    X_max = max(X)

    X = X/X_max

    Y = beta1*X + beta0 #+ np.random.randn( X.shape[0], 1 )

    Y = np.asarray(Y).reshape(-1) #transforms the matrix Y.shape = (1000,1) into an array object that is Y.shape = (1000,) 

    

    Xtrain = X[0:num_datapoints/2]

    Ytrain = Y[0:num_datapoints/2]

    Xtest = X[num_datapoints/2:]

    Ytest = Y[num_datapoints/2:]

    

    lmb = 1

    result = learn_ridge_regression( Xtrain, Ytrain, Xtest, Ytest, lmb )

    

    error_rr = result[0]

    beta1_rr = result[2]

    beta0_rr = result[3]

    

    print "Original beta1 = " + str(beta1) + " RR beta1 = " + str(beta1_rr)

    print "Original beta0 = " + str(beta0) + " RR beta0 = " + str(beta0_rr)

    print "Error = " + str( error_rr )



def learn_ridge_regression( Xtrain, Ytrain, Xtest, Ytest, lmb ):


    #Ridge Regression

    ridge = mlpy.Ridge( lmb )

    ridge.learn( Xtrain, Ytrain );

    YPred = ridge.pred( Xtest )

    

    beta = ridge.beta()

    print str(ridge.beta())

    beta0 = ridge.beta0()

    

    return [mlpy.mse( YPred, Ytest ), YPred, beta, beta0]



This is the output on the console:


[ 0.]

Original beta1 = 3 RR beta1 = [ 0.]

Original beta0 = 2 RR beta0 = 2.0

Error = 0.018


The value [ 0.] comes from the ridge regression object of mlpy. How is this possible? Or did I something wrong here?

Paul

unread,
Jul 10, 2012, 4:12:58 AM7/10/12
to mlpy-g...@googlegroups.com
Sorry, it was my mistake. The X data is not created as float values. Therefore the numbers were rounded wrong. Thanks for reading anyway.
Reply all
Reply to author
Forward
0 new messages