Source

orange / Orange / classification / logreg.py

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
import Orange
from Orange.utils import deprecated_keywords, deprecated_members
from Orange.data import preprocess
from Orange.data.continuization import DomainContinuizer
import decimal
import math


from numpy import dot, array, identity, reshape, diagonal, \
    transpose, concatenate, sqrt, sign
from numpy.linalg import inv
from Orange.core import LogRegClassifier, LogRegFitter, LogRegFitter_Cholesky

def format_decimal(x, prec=2):
    """Allows arbitrary precision with scientific notation"""
    tup = x.as_tuple()
    digits = list(tup.digits[:prec + 1])
    sign = '-' if tup.sign else ''
    dec = ''.join(str(i) for i in digits[1:])
    exp = x.adjusted()
    return '{sign}{int}.{dec}e{exp}'.format(sign=sign, int=digits[0], dec=dec, exp=exp)

def dump(classifier):
    """ Return a formatted string describing the logistic regression model

    :param classifier: logistic regression classifier.
    """

    # print out class values
    out = ['']
    out.append("class attribute = " + classifier.domain.class_var.name)
    out.append("class values = " + str(classifier.domain.class_var.values))
    out.append('')
    
    # get the longest attribute name
    longest=0
    for at in classifier.continuized_domain.features:
        if len(at.name)>longest:
            longest=len(at.name)

    # print out the head
    formatstr = "%"+str(longest)+"s %10s %10s %10s %10s %10s"
    out.append(formatstr % ("Feature", "beta", "st. error", "wald Z", "P", "OR=exp(beta)"))
    out.append('')
    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f"
    out.append(formatstr % ("Intercept", classifier.beta[0], classifier.beta_se[0], classifier.wald_Z[0], classifier.P[0]))
    formatstr = "%"+str(longest)+"s %10.2f %10.2f %10.2f %10.2f %s"
    for i in range(len(classifier.continuized_domain.features)):
        try:
            exp = decimal.Decimal(math.e) ** decimal.Decimal(classifier.beta[i+1])
        except TypeError:
            # Python 2.6 does not support creating Decimals from float
            exp = decimal.Decimal(str(math.e)) ** decimal.Decimal(str(classifier.beta[i+1]))
        out.append(formatstr % (classifier.continuized_domain.features[i].name,
            classifier.beta[i+1],
            classifier.beta_se[i+1],
            classifier.wald_Z[i+1],
            abs(classifier.P[i+1]),
            format_decimal(exp)))

    return '\n'.join(out)


def has_discrete_values(domain):
    """
    Return 1 if the given domain contains any discrete features, else 0.

    :param domain: domain.
    :type domain: :class:`Orange.data.Domain`
    """
    return any(at.var_type == Orange.feature.Type.Discrete
               for at in domain.features)


class LogRegLearner(Orange.classification.Learner):
    """ Logistic regression learner.

    Returns either a learning algorithm (instance of
    :obj:`LogRegLearner`) or, if data is provided, a fitted model
    (instance of :obj:`LogRegClassifier`).

    :param data: data table; it may contain discrete and continuous features
    :type data: Orange.data.Table
    :param weight_id: the ID of the weight meta attribute
    :type weight_id: int
    :param remove_singular: automated removal of constant
        features and singularities (default: `False`)
    :type remove_singular: bool
    :param fitter: the fitting algorithm (default: :obj:`LogRegFitter_Cholesky`)
    :param stepwise_lr: enables stepwise feature selection (default: `False`)
    :type stepwise_lr: bool
    :param add_crit: threshold for adding a feature in stepwise
        selection (default: 0.2)
    :type add_crit: float
    :param delete_crit: threshold for removing a feature in stepwise
        selection (default: 0.3)
    :type delete_crit: float
    :param num_features: number of features in stepwise selection
        (default: -1, no limit)
    :type num_features: int
    :rtype: :obj:`LogRegLearner` or :obj:`LogRegClassifier`

    """

    @deprecated_keywords({"weightID": "weight_id"})
    def __new__(cls, data=None, weight_id=0, **argkw):
        self = Orange.classification.Learner.__new__(cls, **argkw)
        if data is not None:
            self.__init__(**argkw)
            return self.__call__(data, weight_id)
        else:
            return self

    @deprecated_keywords({"removeSingular": "remove_singular"})
    def __init__(self, remove_singular=0, fitter = None, **kwds):
        self.__dict__.update(kwds)
        self.remove_singular = remove_singular
        self.fitter = None

    @deprecated_keywords({"examples": "data"})
    def __call__(self, data, weight=0):
        """Fit a model to the given data.

        :param data: Data instances.
        :type data: :class:`~Orange.data.Table`
        :param weight: Id of meta attribute with instance weights
        :type weight: int
        :rtype: :class:`~Orange.classification.logreg.LogRegClassifier`
        """
        imputer = getattr(self, "imputer", None) or None
        if getattr(self, "remove_missing", 0):
            data = Orange.core.Preprocessor_dropMissing(data)
##        if hasDiscreteValues(examples.domain):
##            examples = createNoDiscTable(examples)
        if not len(data):
            return None
        if getattr(self, "stepwise_lr", 0):
            add_crit = getattr(self, "add_crit", 0.2)
            delete_crit = getattr(self, "delete_crit", 0.3)
            num_features = getattr(self, "num_features", -1)
            attributes = StepWiseFSS(data, add_crit= add_crit,
                delete_crit=delete_crit, imputer = imputer, num_features= num_features)
            tmp_domain = Orange.data.Domain(attributes,
                data.domain.class_var)
            tmp_domain.addmetas(data.domain.getmetas())
            data = data.select(tmp_domain)
        learner = Orange.core.LogRegLearner() # Yes, it has to be from core.
        learner.imputer_constructor = imputer
        if imputer:
            data = self.imputer(data)(data)
        data = Orange.core.Preprocessor_dropMissing(data)
        if self.fitter:
            learner.fitter = self.fitter
        if self.remove_singular:
            lr = learner.fit_model(data, weight)
        else:
            lr = learner(data, weight)
        while isinstance(lr, Orange.feature.Descriptor):
            if isinstance(lr.getValueFrom, Orange.core.ClassifierFromVar) and isinstance(lr.getValueFrom.transformer, Orange.core.Discrete2Continuous):
                lr = lr.getValueFrom.variable
            attributes = data.domain.features[:]
            if lr in attributes:
                attributes.remove(lr)
            else:
                attributes.remove(lr.getValueFrom.variable)
            new_domain = Orange.data.Domain(attributes, 
                data.domain.class_var)
            new_domain.addmetas(data.domain.getmetas())
            data = data.select(new_domain)
            lr = learner.fit_model(data, weight)
        return lr

LogRegLearner = deprecated_members({"removeSingular": "remove_singular",
                                    "weightID": "weight_id",
                                    "stepwiseLR": "stepwise_lr",
                                    "addCrit": "add_crit",
                                    "deleteCrit": "delete_crit",
                                    "numFeatures": "num_features",
                                    "removeMissing": "remove_missing"
                                    })(LogRegLearner)

class UnivariateLogRegLearner(Orange.classification.Learner):
    def __new__(cls, data=None, **argkw):
        self = Orange.classification.Learner.__new__(cls, **argkw)
        if data is not None:
            self.__init__(**argkw)
            return self.__call__(data)
        else:
            return self

    def __init__(self, **kwds):
        self.__dict__.update(kwds)

    @deprecated_keywords({"examples": "data"})
    def __call__(self, data):
        data = createFullNoDiscTable(data)
        classifiers = map(lambda x: LogRegLearner(Orange.core.Preprocessor_dropMissing(
            data.select(Orange.data.Domain(x, 
                data.domain.class_var)))), data.domain.features)
        maj_classifier = LogRegLearner(Orange.core.Preprocessor_dropMissing
            (data.select(Orange.data.Domain(data.domain.class_var))))
        beta = [maj_classifier.beta[0]] + [x.beta[1] for x in classifiers]
        beta_se = [maj_classifier.beta_se[0]] + [x.beta_se[1] for x in classifiers]
        P = [maj_classifier.P[0]] + [x.P[1] for x in classifiers]
        wald_Z = [maj_classifier.wald_Z[0]] + [x.wald_Z[1] for x in classifiers]
        domain = data.domain

        return Univariate_LogRegClassifier(beta = beta, beta_se = beta_se, P = P, wald_Z = wald_Z, domain = domain)

class UnivariateLogRegClassifier(Orange.classification.Classifier):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

    def __call__(self, instance, result_type = Orange.classification.Classifier.GetValue):
        # classification not implemented yet. For now its use is only to
        # provide regression coefficients and its statistics
        raise NotImplemented
    

class LogRegLearnerGetPriors(object):
    def __new__(cls, data=None, weight_id=0, **argkw):
        self = object.__new__(cls)
        if data is not None:
            self.__init__(**argkw)
            return self.__call__(data, weight_id)
        else:
            return self

    @deprecated_keywords({"removeSingular": "remove_singular"})
    def __init__(self, remove_singular=0, **kwds):
        self.__dict__.update(kwds)
        self.remove_singular = remove_singular

    @deprecated_keywords({"examples": "data"})
    def __call__(self, data, weight=0):
        # next function changes data set to a extended with unknown values 
        def createLogRegExampleTable(data, weight_id):
            sets_of_data = []
            for at in data.domain.features:
                # za vsak atribut kreiraj nov newExampleTable new_data
                # v dataOrig, dataFinal in new_data dodaj nov atribut -- continuous variable
                if at.var_type == Orange.feature.Type.Continuous:
                    at_disc = Orange.feature.Continuous(at.name+ "Disc")
                    new_domain = Orange.data.Domain(data.domain.features+[at_disc,data.domain.class_var])
                    new_domain.addmetas(data.domain.getmetas())
                    new_data = Orange.data.Table(new_domain,data)
                    alt_data = Orange.data.Table(new_domain,data)
                    for i,d in enumerate(new_data):
                        d[at_disc] = 0
                        d[weight_id] = 1*data[i][weight_id]
                    for i,d in enumerate(alt_data):
                        d[at_disc] = 1
                        d[at] = 0
                        d[weight_id] = 0.000001*data[i][weight_id]
                elif at.var_type == Orange.feature.Type.Discrete:
                # v dataOrig, dataFinal in new_data atributu "at" dodaj ee  eno  vreednost, ki ima vrednost kar  ime atributa +  "X"
                    at_new = Orange.feature.Discrete(at.name, values = at.values + [at.name+"X"])
                    new_domain = Orange.data.Domain(filter(lambda x: x!=at, data.domain.features)+[at_new,data.domain.class_var])
                    new_domain.addmetas(data.domain.getmetas())
                    new_data = Orange.data.Table(new_domain,data)
                    alt_data = Orange.data.Table(new_domain,data)
                    for i,d in enumerate(new_data):
                        d[at_new] = data[i][at]
                        d[weight_id] = 1*data[i][weight_id]
                    for i,d in enumerate(alt_data):
                        d[at_new] = at.name+"X"
                        d[weight_id] = 0.000001*data[i][weight_id]
                new_data.extend(alt_data)
                sets_of_data.append(new_data)
            return sets_of_data
                  
        learner = LogRegLearner(imputer=Orange.feature.imputation.ImputerConstructor_average(),
            remove_singular = self.remove_singular)
        # get Original Model
        orig_model = learner(data, weight)
        if orig_model.fit_status:
            print "Warning: model did not converge"

        # get extended Model (you should not change data)
        if weight == 0:
            weight = Orange.feature.Descriptor.new_meta_id()
            data.addMetaAttribute(weight, 1.0)
        extended_set_of_examples = createLogRegExampleTable(data, weight)
        extended_models = [learner(extended_examples, weight) \
                           for extended_examples in extended_set_of_examples]

##        print examples[0]
##        printOUT(orig_model)
##        print orig_model.domain
##        print orig_model.beta
##        print orig_model.beta[orig_model.continuized_domain.features[-1]]
##        for i,m in enumerate(extended_models):
##            print examples.domain.features[i]
##            printOUT(m)
            
        
        # izracunas odstopanja
        # get sum of all betas
        beta = 0
        betas_ap = []
        for m in extended_models:
            beta_add = m.beta[m.continuized_domain.features[-1]]
            betas_ap.append(beta_add)
            beta = beta + beta_add
        
        # substract it from intercept
        #print "beta", beta
        logistic_prior = orig_model.beta[0]+beta
        
        # compare it to bayes prior
        bayes = Orange.classification.bayes.NaiveLearner(data)
        bayes_prior = math.log(bayes.distribution[1]/bayes.distribution[0])

        # normalize errors
##        print "bayes", bayes_prior
##        print "lr", orig_model.beta[0]
##        print "lr2", logistic_prior
##        print "dist", Orange.statistics.distribution.Distribution(examples.domain.class_var,examples)
##        print "prej", betas_ap

        # error normalization - to avoid errors due to assumption of independence of unknown values
        dif = bayes_prior - logistic_prior
        positives = sum(filter(lambda x: x>=0, betas_ap))
        negatives = -sum(filter(lambda x: x<0, betas_ap))
        if not negatives == 0:
            kPN = positives/negatives
            diffNegatives = dif/(1+kPN)
            diffPositives = kPN*diffNegatives
            kNegatives = (negatives-diffNegatives)/negatives
            kPositives = positives/(positives-diffPositives)
    ##        print kNegatives
    ##        print kPositives

            for i,b in enumerate(betas_ap):
                if b<0: betas_ap[i]*=kNegatives
                else: betas_ap[i]*=kPositives
        #print "potem", betas_ap

        # vrni originalni model in pripadajoce apriorne niclele
        return (orig_model, betas_ap)
        #return (bayes_prior,orig_model.beta[examples.domain.class_var],logistic_prior)

LogRegLearnerGetPriors = deprecated_members({"removeSingular":
                                                 "remove_singular"}
)(LogRegLearnerGetPriors)

class LogRegLearnerGetPriorsOneTable:
    @deprecated_keywords({"removeSingular": "remove_singular"})
    def __init__(self, remove_singular=0, **kwds):
        self.__dict__.update(kwds)
        self.remove_singular = remove_singular

    @deprecated_keywords({"examples": "data"})
    def __call__(self, data, weight=0):
        # next function changes data set to a extended with unknown values 
        def createLogRegExampleTable(data, weightID):
            finalData = Orange.data.Table(data)
            orig_data = Orange.data.Table(data)
            for at in data.domain.features:
                # za vsak atribut kreiraj nov newExampleTable newData
                # v dataOrig, dataFinal in newData dodaj nov atribut -- continuous variable
                if at.var_type == Orange.feature.Type.Continuous:
                    atDisc = Orange.feature.Continuous(at.name + "Disc")
                    newDomain = Orange.data.Domain(orig_data.domain.features+[atDisc,data.domain.class_var])
                    newDomain.addmetas(newData.domain.getmetas())
                    finalData = Orange.data.Table(newDomain,finalData)
                    newData = Orange.data.Table(newDomain,orig_data)
                    orig_data = Orange.data.Table(newDomain,orig_data)
                    for d in orig_data:
                        d[atDisc] = 0
                    for d in finalData:
                        d[atDisc] = 0
                    for i,d in enumerate(newData):
                        d[atDisc] = 1
                        d[at] = 0
                        d[weightID] = 100*data[i][weightID]
                        
                elif at.var_type == Orange.feature.Type.Discrete:
                # v dataOrig, dataFinal in newData atributu "at" dodaj ee  eno  vreednost, ki ima vrednost kar  ime atributa +  "X"
                    at_new = Orange.feature.Discrete(at.name, values = at.values + [at.name+"X"])
                    newDomain = Orange.data.Domain(filter(lambda x: x!=at, orig_data.domain.features)+[at_new,orig_data.domain.class_var])
                    newDomain.addmetas(orig_data.domain.getmetas())
                    temp_finalData = Orange.data.Table(finalData)
                    finalData = Orange.data.Table(newDomain,finalData)
                    newData = Orange.data.Table(newDomain,orig_data)
                    temp_origData = Orange.data.Table(orig_data)
                    orig_data = Orange.data.Table(newDomain,orig_data)
                    for i,d in enumerate(orig_data):
                        d[at_new] = temp_origData[i][at]
                    for i,d in enumerate(finalData):
                        d[at_new] = temp_finalData[i][at]
                    for i,d in enumerate(newData):
                        d[at_new] = at.name+"X"
                        d[weightID] = 10*data[i][weightID]
                finalData.extend(newData)
            return finalData
                  
        learner = LogRegLearner(imputer = Orange.feature.imputation.ImputerConstructor_average(), removeSingular = self.remove_singular)
        # get Original Model
        orig_model = learner(data,weight)

        # get extended Model (you should not change data)
        if weight == 0:
            weight = Orange.feature.Descriptor.new_meta_id()
            data.addMetaAttribute(weight, 1.0)
        extended_examples = createLogRegExampleTable(data, weight)
        extended_model = learner(extended_examples, weight)

##        print examples[0]
##        printOUT(orig_model)
##        print orig_model.domain
##        print orig_model.beta

##        printOUT(extended_model)        
        # izracunas odstopanja
        # get sum of all betas
        beta = 0
        betas_ap = []
        for m in extended_models:
            beta_add = m.beta[m.continuized_domain.features[-1]]
            betas_ap.append(beta_add)
            beta = beta + beta_add
        
        # substract it from intercept
        #print "beta", beta
        logistic_prior = orig_model.beta[0]+beta
        
        # compare it to bayes prior
        bayes = Orange.classification.bayes.NaiveLearner(data)
        bayes_prior = math.log(bayes.distribution[1]/bayes.distribution[0])

        # normalize errors
        #print "bayes", bayes_prior
        #print "lr", orig_model.beta[0]
        #print "lr2", logistic_prior
        #print "dist", Orange.statistics.distribution.Distribution(examples.domain.class_var,examples)
        k = (bayes_prior-orig_model.beta[0])/(logistic_prior-orig_model.beta[0])
        #print "prej", betas_ap
        betas_ap = [k*x for x in betas_ap]                
        #print "potem", betas_ap

        # vrni originalni model in pripadajoce apriorne niclele
        return (orig_model, betas_ap)
        #return (bayes_prior,orig_model.beta[data.domain.class_var],logistic_prior)

LogRegLearnerGetPriorsOneTable = deprecated_members({"removeSingular":
                                                         "remove_singular"}
)(LogRegLearnerGetPriorsOneTable)


######################################
#### Fitters for logistic regression (logreg) learner ####
######################################

def pr(x, betas):
    k = math.exp(dot(x, betas))
    return k / (1+k)

def lh(x,y,betas):
    llh = 0.0
    for i,x_i in enumerate(x):
        pr = pr(x_i,betas)
        llh += y[i]*math.log(max(pr,1e-6)) + (1-y[i])*log(max(1-pr,1e-6))
    return llh


def diag(vector):
    mat = identity(len(vector))
    for i,v in enumerate(vector):
        mat[i][i] = v
    return mat
    
class SimpleFitter(LogRegFitter):
    def __init__(self, penalty=0, se_penalty = False):
        self.penalty = penalty
        self.se_penalty = se_penalty

    def __call__(self, data, weight=0):
        ml = data.native(0)
        for i in range(len(data.domain.features)):
          a = data.domain.features[i]
          if a.var_type == Orange.feature.Type.Discrete:
            for m in ml:
              m[i] = a.values.index(m[i])
        for m in ml:
          m[-1] = data.domain.class_var.values.index(m[-1])
        Xtmp = array(ml)
        y = Xtmp[:,-1]   # true probabilities (1's or 0's)
        one = reshape(array([1]*len(data)), (len(data),1)) # intercept column
        X=concatenate((one, Xtmp[:,:-1]),1)  # intercept first, then data

        betas = array([0.0] * (len(data.domain.features)+1))
        oldBetas = array([1.0] * (len(data.domain.features)+1))
        N = len(data)

        pen_matrix = array([self.penalty] * (len(data.domain.features)+1))
        if self.se_penalty:
            p = array([pr(X[i], betas) for i in range(len(data))])
            W = identity(len(data))
            pp = p * (1.0-p)
            for i in range(N):
                W[i,i] = pp[i]
            se = sqrt(diagonal(inv(dot(transpose(X), dot(W, X)))))
            for i,p in enumerate(pen_matrix):
                pen_matrix[i] *= se[i]
        # predict the probability for an instance, x and betas are vectors
        # start the computation
        likelihood = 0.
        likelihood_new = 1.
        while abs(likelihood - likelihood_new)>1e-5:
            likelihood = likelihood_new
            oldBetas = betas
            p = array([pr(X[i], betas) for i in range(len(data))])

            W = identity(len(data))
            pp = p * (1.0-p)
            for i in range(N):
                W[i,i] = pp[i]

            WI = inv(W)
            z = dot(X, betas) + dot(WI, y - p)

            tmpA = inv(dot(transpose(X), dot(W, X))+diag(pen_matrix))
            tmpB = dot(transpose(X), y-p)
            betas = oldBetas + dot(tmpA,tmpB)
#            betaTemp = dot(dot(dot(dot(tmpA,transpose(X)),W),X),oldBetas)
#            print betaTemp
#            tmpB = dot(transpose(X), dot(W, z))
#            betas = dot(tmpA, tmpB)
            likelihood_new = lh(X,y,betas)-self.penalty*sum([b*b for b in betas])
            print likelihood_new

            
            
##        XX = sqrt(diagonal(inv(dot(transpose(X),X))))
##        yhat = array([pr(X[i], betas) for i in range(len(data))])
##        ss = sum((y - yhat) ** 2) / (N - len(data.domain.features) - 1)
##        sigma = math.sqrt(ss)
        p = array([pr(X[i], betas) for i in range(len(data))])
        W = identity(len(data))
        pp = p * (1.0-p)
        for i in range(N):
            W[i,i] = pp[i]
        diXWX = sqrt(diagonal(inv(dot(transpose(X), dot(W, X)))))
        xTemp = dot(dot(inv(dot(transpose(X), dot(W, X))),transpose(X)),y)
        beta = []
        beta_se = []
        print "likelihood ridge", likelihood
        for i in range(len(betas)):
            beta.append(betas[i])
            beta_se.append(diXWX[i])
        return (self.OK, beta, beta_se, 0)

def pr_bx(bx):
    if bx > 35:
        return 1
    if bx < -35:
        return 0
    return exp(bx)/(1+exp(bx))

class BayesianFitter(LogRegFitter):
    def __init__(self, penalty=0, anch_examples=[], tau = 0):
        self.penalty = penalty
        self.anch_examples = anch_examples
        self.tau = tau

    def create_array_data(self,data):
        if not len(data):
            return (array([]),array([]))
        # convert data to numeric
        ml = data.native(0)
        for i,a in enumerate(data.domain.features):
          if a.var_type == Orange.feature.Type.Discrete:
            for m in ml:
              m[i] = a.values.index(m[i])
        for m in ml:
          m[-1] = data.domain.class_var.values.index(m[-1])
        Xtmp = array(ml)
        y = Xtmp[:,-1]   # true probabilities (1's or 0's)
        one = reshape(array([1]*len(data)), (len(data),1)) # intercept column
        X=concatenate((one, Xtmp[:,:-1]),1)  # intercept first, then data
        return (X,y)
    
    def __call__(self, data, weight=0):
        (X,y)=self.create_array_data(data)

        exTable = Orange.data.Table(data.domain)
        for id,ex in self.anch_examples:
            exTable.extend(Orange.data.Table(ex,data.domain))
        (X_anch,y_anch)=self.create_array_data(exTable)

        betas = array([0.0] * (len(data.domain.features)+1))

        likelihood,betas = self.estimate_beta(X,y,betas,[0]*(len(betas)),X_anch,y_anch)

        # get attribute groups atGroup = [(startIndex, number of values), ...)
        ats = data.domain.features
        atVec=reduce(lambda x,y: x+[(y,not y==x[-1][0])], [a.getValueFrom and a.getValueFrom.whichVar or a for a in ats],[(ats[0].getValueFrom and ats[0].getValueFrom.whichVar or ats[0],0)])[1:]
        atGroup=[[0,0]]
        for v_i,v in enumerate(atVec):
            if v[1]==0: atGroup[-1][1]+=1
            else:       atGroup.append([v_i,1])
        
        # compute zero values for attributes
        sumB = 0.
        for ag in atGroup:
            X_temp = concatenate((X[:,:ag[0]+1],X[:,ag[0]+1+ag[1]:]),1)
            if X_anch:
                X_anch_temp = concatenate((X_anch[:,:ag[0]+1],X_anch[:,ag[0]+1+ag[1]:]),1)
            else: X_anch_temp = X_anch
##            print "1", concatenate((betas[:i+1],betas[i+2:]))
##            print "2", betas
            likelihood_temp,betas_temp=self.estimate_beta(X_temp,y,concatenate((betas[:ag[0]+1],betas[ag[0]+ag[1]+1:])),[0]+[1]*(len(betas)-1-ag[1]),X_anch_temp,y_anch)
            print "finBetas", betas, betas_temp
            print "betas", betas[0], betas_temp[0]
            sumB += betas[0]-betas_temp[0]
        apriori = Orange.statistics.distribution.Distribution(data.domain.class_var, data)
        aprioriProb = apriori[0]/apriori.abs
        
        print "koncni rezultat", sumB, math.log((1-aprioriProb)/aprioriProb), betas[0]
            
        beta = []
        beta_se = []
        print "likelihood2", likelihood
        for i in range(len(betas)):
            beta.append(betas[i])
            beta_se.append(0.0)
        return (self.OK, beta, beta_se, 0)

     
        
    def estimate_beta(self,X,y,betas,const_betas,X_anch,y_anch):
        N,N_anch = len(y),len(y_anch)
        r,r_anch = array([dot(X[i], betas) for i in range(N)]),\
                   array([dot(X_anch[i], betas) for i in range(N_anch)])
        p    = array([pr_bx(ri) for ri in r])
        X_sq = X*X

        max_delta      = [1.]*len(const_betas)
        likelihood     = -1.e+10
        likelihood_new = -1.e+9
        while abs(likelihood - likelihood_new)>0.01 and max(max_delta)>0.01:
            likelihood = likelihood_new
            print likelihood
            betas_temp = [b for b in betas]
            for j in range(len(betas)):
                if const_betas[j]: continue
                dl = dot(X[:,j], transpose(y-p))
                for xi,x in enumerate(X_anch):
                    dl += self.penalty*x[j]*(y_anch[xi] - pr_bx(r_anch[xi]*self.penalty))

                ddl = dot(X_sq[:,j], transpose(p*(1-p)))
                for xi,x in enumerate(X_anch):
                    ddl += self.penalty*x[j]*pr_bx(r[xi]*self.penalty)*(1-pr_bx(r[xi]*self.penalty))

                if j==0:
                    dv = dl/max(ddl,1e-6)
                elif betas[j] == 0: # special handling due to non-defined first and second derivatives
                    dv = (dl-self.tau)/max(ddl,1e-6)
                    if dv < 0:
                        dv = (dl+self.tau)/max(ddl,1e-6)
                        if dv > 0:
                            dv = 0
                else:
                    dl -= sign(betas[j])*self.tau
                    dv = dl/max(ddl,1e-6)
                    if not sign(betas[j] + dv) == sign(betas[j]):
                        dv = -betas[j]
                dv = min(max(dv,-max_delta[j]),max_delta[j])
                r+= X[:,j]*dv
                p = array([pr_bx(ri) for ri in r])
                if N_anch:
                    r_anch+=X_anch[:,j]*dv
                betas[j] += dv
                max_delta[j] = max(2*abs(dv),max_delta[j]/2)
            likelihood_new = lh(X,y,betas)
            for xi,x in enumerate(X_anch):
                try:
                    likelihood_new += y_anch[xi]*r_anch[xi]*self.penalty-log(1+exp(r_anch[xi]*self.penalty))
                except:
                    likelihood_new += r_anch[xi]*self.penalty*(y_anch[xi]-1)
            likelihood_new -= sum([abs(b) for b in betas[1:]])*self.tau
            if likelihood_new < likelihood:
                max_delta = [md/4 for md in max_delta]
                likelihood_new = likelihood
                likelihood = likelihood_new + 1.
                betas = [b for b in betas_temp]
        print "betas", betas
        print "init_like", likelihood_new
        print "pure_like", lh(X,y,betas)
        return (likelihood,betas)
    
############################################################
#  Feature subset selection for logistic regression

@deprecated_keywords({"examples": "data"})
def get_likelihood(fitter, data):
    res = fitter(data)
    if res[0] in [fitter.OK]: #, fitter.Infinity, fitter.Divergence]:
       status, beta, beta_se, likelihood = res
       if sum([abs(b) for b in beta])<sum([abs(b) for b in beta_se]):
           return -100*len(data)
       return likelihood
    else:
       return -100*len(data)
        


class StepWiseFSS(object):
  """
  A learning algorithm for logistic regression that implements a
  stepwise feature subset selection as described in Applied Logistic
  Regression (Hosmer and Lemeshow, 2000).

  Each step of the algorithm is composed of two parts. The first is
  backward elimination in which the least significant variable in the
  model is removed if its p-value is above the prescribed threshold
  :obj:`delete_crit`. The second step is forward selection in which
  all variables are tested for addition to the model, and the one with
  the most significant contribution is added if the corresponding
  p-value is smaller than the prescribed :obj:d`add_crit`. The
  algorithm stops when no more variables can be added or removed.

  The model can be additionaly constrained by setting
  :obj:`num_features` to a non-negative value. The algorithm will then
  stop when the number of variables exceeds the given limit.

  Significances are assesed by the likelihood ratio chi-square
  test. Normal F test is not appropriate since the errors are assumed
  to follow a binomial distribution.

  The class constructor returns an instance of learning algorithm or,
  if given training data, a list of selected variables.

  :param table: training data.
  :type table: Orange.data.Table

  :param add_crit: threshold for adding a variable (default: 0.2)
  :type add_crit: float

  :param delete_crit: threshold for removing a variable
      (default: 0.3); should be higher than :obj:`add_crit`.
  :type delete_crit: float

  :param num_features: maximum number of selected features,
      use -1 for infinity.
  :type num_features: int
  :rtype: :obj:`StepWiseFSS` or list of features

  """

  def __new__(cls, data=None, **argkw):
      self = object.__new__(cls)
      if data is not None:
          self.__init__(**argkw)
          return self.__call__(data)
      else:
          return self

  @deprecated_keywords({"addCrit": "add_crit", "deleteCrit": "delete_crit",
                        "numFeatures": "num_features"})
  def __init__(self, add_crit=0.2, delete_crit=0.3, num_features=-1, **kwds):
    self.__dict__.update(kwds)
    self.add_crit = add_crit
    self.delete_crit = delete_crit
    self.num_features = num_features

  def __call__(self, examples):
    if getattr(self, "imputer", 0):
        examples = self.imputer(examples)(examples)
    if getattr(self, "removeMissing", 0):
        examples = preprocess.DropMissing(examples)
    continuizer = preprocess.DomainContinuizer(zeroBased=1,
        continuousTreatment=preprocess.DomainContinuizer.Leave,
                                           multinomialTreatment = preprocess.DomainContinuizer.FrequentIsBase,
                                           classTreatment = preprocess.DomainContinuizer.Ignore)
    attr = []
    remain_attr = examples.domain.features[:]

    # get LL for Majority Learner 
    tempDomain = Orange.data.Domain(attr,examples.domain.class_var)
    #tempData  = Orange.core.Preprocessor_dropMissing(examples.select(tempDomain))
    tempData  = Orange.core.Preprocessor_dropMissing(examples.select(tempDomain))

    ll_Old = get_likelihood(LogRegFitter_Cholesky(), tempData)
    ll_Best = -1000000
    length_Old = float(len(tempData))

    stop = 0
    while not stop:
        # LOOP until all variables are added or no further deletion nor addition of attribute is possible
        worstAt = None
        # if there are more than 1 attribute then perform backward elimination
        if len(attr) >= 2:
            minG = 1000
            worstAt = attr[0]
            ll_Best = ll_Old
            length_Best = length_Old
            for at in attr:
                # check all attribute whether its presence enough increases LL?

                tempAttr = filter(lambda x: x!=at, attr)
                tempDomain = Orange.data.Domain(tempAttr,examples.domain.class_var)
                tempDomain.addmetas(examples.domain.getmetas())
                # domain, calculate P for LL improvement.
                tempDomain  = continuizer(Orange.core.Preprocessor_dropMissing(examples.select(tempDomain)))
                tempData = Orange.core.Preprocessor_dropMissing(examples.select(tempDomain))

                ll_Delete = get_likelihood(LogRegFitter_Cholesky(), tempData)
                length_Delete = float(len(tempData))
                length_Avg = (length_Delete + length_Old)/2.0

                G=-2*length_Avg*(ll_Delete/length_Delete-ll_Old/length_Old)

                # set new worst attribute
                if G<minG:
                    worstAt = at
                    minG=G
                    ll_Best = ll_Delete
                    length_Best = length_Delete
            # deletion of attribute

            if worstAt.var_type==Orange.feature.Type.Continuous:
                P=lchisqprob(minG,1);
            else:
                P=lchisqprob(minG,len(worstAt.values)-1);
            if P>=self.delete_crit:
                attr.remove(worstAt)
                remain_attr.append(worstAt)
                nodeletion=0
                ll_Old = ll_Best
                length_Old = length_Best
            else:
                nodeletion=1
        else:
            nodeletion = 1
            # END OF DELETION PART

        # if enough attributes has been chosen, stop the procedure
        if self.num_features>-1 and len(attr)>=self.num_features:
            remain_attr=[]

        # for each attribute in the remaining
        maxG=-1
        ll_Best = ll_Old
        length_Best = length_Old
        bestAt = None
        for at in remain_attr:
            tempAttr = attr + [at]
            tempDomain = Orange.data.Domain(tempAttr,examples.domain.class_var)
            tempDomain.addmetas(examples.domain.getmetas())
            # domain, calculate P for LL improvement.
            tempDomain  = continuizer(Orange.core.Preprocessor_dropMissing(examples.select(tempDomain)))
            tempData = Orange.core.Preprocessor_dropMissing(examples.select(tempDomain))
            ll_New = get_likelihood(LogRegFitter_Cholesky(), tempData)

            length_New = float(len(tempData)) # get number of examples in tempData to normalize likelihood

            # P=PR(CHI^2>G), G=-2(L(0)-L(1))=2(E(0)-E(1))
            length_avg = (length_New + length_Old)/2
            G=-2*length_avg*(ll_Old/length_Old-ll_New/length_New);
            if G>maxG:
                bestAt = at
                maxG=G
                ll_Best = ll_New
                length_Best = length_New
        if not bestAt:
            stop = 1
            continue

        if bestAt.var_type==Orange.feature.Type.Continuous:
            P=lchisqprob(maxG,1);
        else:
            P=lchisqprob(maxG,len(bestAt.values)-1);
        # Add attribute with smallest P to attributes(attr)
        if P<=self.add_crit:
            attr.append(bestAt)
            remain_attr.remove(bestAt)
            ll_Old = ll_Best
            length_Old = length_Best

        if (P>self.add_crit and nodeletion) or (bestAt == worstAt):
            stop = 1

    return attr

StepWiseFSS = deprecated_members({"addCrit": "add_crit",
                                   "deleteCrit": "delete_crit",
                                   "numFeatures": "num_features"})(StepWiseFSS)


class StepWiseFSSFilter(object):
    def __new__(cls, data=None, **argkw):
        self = object.__new__(cls)
        if data is not None:
            self.__init__(**argkw)
            return self.__call__(data)
        else:
            return self

    @deprecated_keywords({"addCrit": "add_crit", "deleteCrit": "delete_crit",
                          "numFeatures": "num_features"})
    def __init__(self, add_crit=0.2, delete_crit=0.3, num_features = -1):
        self.add_crit = add_crit
        self.delete_crit = delete_crit
        self.num_features = num_features

    @deprecated_keywords({"examples": "data"})
    def __call__(self, data):
        attr = StepWiseFSS(data, add_crit=self.add_crit,
            delete_crit= self.delete_crit, num_features= self.num_features)
        return data.select(Orange.data.Domain(attr, data.domain.class_var))

StepWiseFSSFilter = deprecated_members({"addCrit": "add_crit",
                                        "deleteCrit": "delete_crit",
                                        "numFeatures": "num_features"})\
    (StepWiseFSSFilter)


####################################
##  PROBABILITY CALCULATIONS

def lchisqprob(chisq, df):
    """
    Return the (1-tailed) probability value associated with the provided
    chi-square value and df.  Adapted from chisq.c in Gary Perlman's |Stat.
    """
    BIG = 20.0

    def ex(x):
        BIG = 20.0
        if x < -BIG:
            return 0.0
        else:
            return math.exp(x)
    if chisq <= 0 or df < 1:
        return 1.0
    a = 0.5 * chisq
    if df % 2 == 0:
        even = 1
    else:
        even = 0
    if df > 1:
        y = ex(-a)
    if even:
        s = y
    else:
        s = 2.0 * zprob(-math.sqrt(chisq))
    if (df > 2):
        chisq = 0.5 * (df - 1.0)
        if even:
            z = 1.0
        else:
            z = 0.5
        if a > BIG:
            if even:
                e = 0.0
            else:
                e = math.log(math.sqrt(math.pi))
            c = math.log(a)
            while (z <= chisq):
                e = math.log(z) + e
                s = s + ex(c * z - a - e)
                z = z + 1.0
            return s
        else:
            if even:
                e = 1.0
            else:
                e = 1.0 / math.sqrt(math.pi) / math.sqrt(a)
            c = 0.0
            while (z <= chisq):
                e = e * (a / float(z))
                c = c + e
                z = z + 1.0
            return (c * y + s)
    else:
        return s


def zprob(z):
    """
    Returns the area under the normal curve 'to the left of' the given z value.
    Thus::

    for z<0, zprob(z) = 1-tail probability
    for z>0, 1.0-zprob(z) = 1-tail probability
    for any z, 2.0*(1.0-zprob(abs(z))) = 2-tail probability

    Adapted from z.c in Gary Perlman's |Stat.
    """
    Z_MAX = 6.0    # maximum meaningful z-value
    if z == 0.0:
        x = 0.0
    else:
        y = 0.5 * math.fabs(z)
    if y >= (Z_MAX * 0.5):
        x = 1.0
    elif (y < 1.0):
        w = y * y
        x = ((((((((0.000124818987 * w
            - 0.001075204047) * w + 0.005198775019) * w
              - 0.019198292004) * w + 0.059054035642) * w
            - 0.151968751364) * w + 0.319152932694) * w
          - 0.531923007300) * w + 0.797884560593) * y * 2.0
    else:
        y = y - 2.0
        x = (((((((((((((-0.000045255659 * y
                 + 0.000152529290) * y - 0.000019538132) * y
               - 0.000676904986) * y + 0.001390604284) * y
             - 0.000794620820) * y - 0.002034254874) * y
               + 0.006549791214) * y - 0.010557625006) * y
             + 0.011630447319) * y - 0.009279453341) * y
           + 0.005353579108) * y - 0.002141268741) * y
         + 0.000535310849) * y + 0.999936657524
    if z > 0.0:
        prob = ((x + 1.0) * 0.5)
    else:
        prob = ((1.0 - x) * 0.5)
    return prob


"""
Logistic regression learner from LIBLINEAR
"""


class LibLinearLogRegLearner(Orange.core.LinearLearner):
    """
    A logistic regression learner from `LIBLINEAR`_.

    Supports L2 regularized learning.

    .. _`LIBLINEAR`: http://www.csie.ntu.edu.tw/~cjlin/liblinear/

    .. note::
        Unlike :class:`LogRegLearner` this one supports multi-class
        classification using one vs. rest strategy.

    """

    L2R_LR = Orange.core.LinearLearner.L2R_LR
    L2R_LR_DUAL = Orange.core.LinearLearner.L2R_LR_Dual
    L1R_LR = Orange.core.LinearLearner.L1R_LR

    __new__ = Orange.utils._orange__new__(base=Orange.core.LinearLearner)

    def __init__(self, solver_type=L2R_LR, C=1.0, eps=0.01, normalization=True,
            bias=-1.0, multinomial_treatment=DomainContinuizer.NValues,
            **kwargs):
        """
        :param solver_type: One of the following class constants:
            ``L2_LR``, ``L2_LR_DUAL``, ``L1R_LR``.

        :param C: Regularization parameter (default 1.0). Higher values of
            C mean less regularization (C is a coefficient for the loss
            function).
        :type C: float

        :param eps: Stopping criteria (default 0.01)
        :type eps: float

        :param normalization: Normalize the input data prior to learning
            (default True)
        :type normalization: bool

        :param bias: If positive, use it as a bias (default -1).
        :type bias: float

        :param multinomial_treatment: Defines how to handle multinomial
            features for learning. It can be one of the
            :class:`~.DomainContinuizer` `multinomial_treatment`
            constants (default: `DomainContinuizer.NValues`).

        :type multinomial_treatment: int

        .. versionadded:: 2.6.1
            Added `multinomial_treatment`

        """
        self.solver_type = solver_type
        self.C = C
        self.eps = eps
        self.normalization = normalization
        self.bias = bias
        self.multinomial_treatment = multinomial_treatment

        for name, value in kwargs.items():
            setattr(self, name, value)

    def __call__(self, data, weight_id=None):
        """
        Return a classifier trained on the `data` (`weight_id` is ignored).

        :param Orange.data.Table data:
            Training data set.
        :param int weight_id:
            Ignored.
        :rval: Orange.core.LinearClassifier

        .. note::
            The :class:`Orange.core.LinearClassifier` is same class as
            :class:`Orange.classification.svm.LinearClassifier`.

        """
        if not isinstance(data.domain.class_var, Orange.feature.Discrete):
            raise TypeError("Can only learn a discrete class.")

        if data.domain.has_discrete_attributes(False) or self.normalization:
            dc = DomainContinuizer()
            dc.multinomial_treatment = self.multinomial_treatment
            dc.class_treatment = dc.Ignore
            dc.continuous_treatment = \
                    dc.NormalizeByVariance if self.normalization else dc.Leave
            c_domain = dc(data)
            data = data.translate(c_domain)
        return super(LibLinearLogRegLearner, self).__call__(data, weight_id)