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

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 orangeimport refrom Orange.regression import linearlinearRegression = linear.LinearRegressionLearnery1 = [[1, 2],        [2, 4.0],       [3., 6.0]] #y = 2xxy2 = [[4., 13.],       [5., 16.],       [6., 19.]] #y = 3x + 1xy = 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 sectionfor w, e in zip(weights, data):    e.set_weight(-100, w)modelFirstSection = linearRegression(data) #expect y = 2xprint 'all the data'print modelAllTheDataprint 'only the first section'print modelFirstSection`

This is the output of the code:
Code: Select all
`all the dataVariable  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 1only the first sectionVariable  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

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

Return to Questions & Support