Orange Forum • View topic - How do you add weights to examples

How do you add weights to examples

A place to ask questions about methods in Orange and how they are used and other general support.

How do you add weights to examples

Postby bgbg » Tue Feb 07, 2012 9:50

Hi,
in the example below I generate data that consists of two parts. The first part corresponds to the model y = 2*x, while the second part is generated using y = 3*x + 1. I than use linear regression to regress find the slope and the intercept.
I then want to "shut down" the second part of the data and do regression again. I expect that the slope in the resulting model will be 2 and the intercept will be 0.
Here is the code:

Code: Select all
import orange
import re
from Orange.regression import linear
linearRegression = linear.LinearRegressionLearner
y1 = [[1, 2],
       [2, 4.0],
       [3., 6.0]] #y = 2x
xy2 = [[4., 13.],
       [5., 16.],
       [6., 19.]] #y = 3x + 1
xy = list(xy1)
xy.extend(xy2)
domain = orange.Domain(map(orange.FloatVariable, ['x', 'y']))
domain.add_meta(-100, orange.FloatVariable('weight'))
data = orange.ExampleTable(domain, xy)
modelAllTheData = linearRegression(data)
weights = [1, 1, 1, 0, 0, 0] #turn on the first section
for w, e in zip(weights, data):
    e.set_weight(-100, w)
modelFirstSection = linearRegression(data) #expect y = 2x
print 'all the data'
print modelAllTheData
print 'only the first section'
print modelFirstSection


This is the output of the code:
Code: Select all
all the data
Variable  Coeff Est  Std Error    t-value          p
 Intercept     -2.800      1.312     -2.134      0.100     .
         x      3.657      0.337     10.857      0.000   ***
Signif. codes:  0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1
only the first section
Variable  Coeff Est  Std Error    t-value          p
 Intercept     -2.800      1.312     -2.134      0.100     .
         x      3.657      0.337     10.857      0.000   ***
Signif. codes:  0 *** 0.001 ** 0.01 * 0.05 . 0.1 empty 1

As you can see, the weights had no effect on the regression. What am I doing wrong?

Re: How do you add weights to examples

Postby Ales » Fri Feb 10, 2012 12:08

You need to pass the weight id to linearRegression:
Code: Select all
modelFirstSection = linearRegression(data, -100)


Return to Questions & Support



cron