# Changes in [11401:c12e0617fe38:11402:a9e5ef96ab1d] in orange

Ignore:
Files:
5 edited

Unmodified
Removed
• ## Orange/classification/logreg.py

 r11063 from Orange.utils import deprecated_keywords, deprecated_members from Orange.data import preprocess from Orange.data.continuization import DomainContinuizer import decimal import math ##  PROBABILITY CALCULATIONS def lchisqprob(chisq,df): def lchisqprob(chisq, df): """ Return the (1-tailed) probability value associated with the provided """ 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 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 if df % 2 == 0: even = 1 else: even = 0 even = 0 if df > 1: y = ex(-a) y = ex(-a) if even: s = y s = y else: s = 2.0 * zprob(-math.sqrt(chisq)) if a > BIG: if even: e = 0.0 e = 0.0 else: e = math.log(math.sqrt(math.pi)) 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 e = math.log(z) + e s = s + ex(c * z - a - e) z = z + 1.0 return s else: c = 0.0 while (z <= chisq): e = e * (a/float(z)) e = e * (a / float(z)) c = c + e z = z + 1.0 return (c*y+s) return (c * y + s) else: return s """ Returns the area under the normal curve 'to the left of' the given z value. Thus:: Thus:: for z<0, zprob(z) = 1-tail probability Z_MAX = 6.0    # maximum meaningful z-value if z == 0.0: x = 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 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) prob = ((x + 1.0) * 0.5) else: prob = ((1.0-x)*0.5) prob = ((1.0 - x) * 0.5) return prob """ from Orange.data import preprocess class LibLinearLogRegLearner(Orange.core.LinearLearner): """A logistic regression learner from `LIBLINEAR`_. Supports L2 regularized learning. .. _`LIBLINEAR`: http://www.csie.ntu.edu.tw/~cjlin/liblinear/ """ def __init__(self, solver_type=L2R_LR, C=1.0, eps=0.01, normalization=True, bias=-1.0, **kwargs): bias=-1.0, multinomial_treatment=DomainContinuizer.NValues, **kwargs): """ :param solver_type: One of the following class constants: :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). :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) :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.normalization = normalization self.bias = bias self.multinomial_treatment = multinomial_treatment for name, value in kwargs.items(): if data.domain.has_discrete_attributes(False) or self.normalization: dc = Orange.data.continuization.DomainContinuizer() dc.multinomial_treatment = dc.NValues 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) c_domain = dc(data) data = data.translate(c_domain) return super(LibLinearLogRegLearner, self).__call__(data, weight_id)
• ## Orange/classification/svm/__init__.py

 r11377 from Orange.data import preprocess from Orange.data.preprocess import DomainContinuizer from Orange import feature as variable def __init__(self, solver_type=L2R_L2LOSS_DUAL, C=1.0, eps=0.01, bias=1.0, normalization=True, **kwargs): bias=1.0, normalization=True, multinomial_treatment=DomainContinuizer.NValues, **kwargs): """ :param solver_type: One of the following class constants: :type normalization: bool .. note:: If the training data contains discrete features they are replaced by indicator columns one for each value of the feature regardless of the value of `normalization`. This is different then in :class:`SVMLearner` where this is done only if :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` .. note:: By default if the training data contains discrete features they are replaced by indicator columns one for each value of the feature regardless of the value of `normalization`. This is different then in :class:`SVMLearner` where this is done only if `normalization` is ``True``. self.bias = bias self.normalization = normalization self.multinomial_treatment = multinomial_treatment for name, val in kwargs.items(): setattr(self, name, val) if self.solver_type not in [self.L2R_L2LOSS_DUAL, self.L2R_L2LOSS, self.L2R_L1LOSS_DUAL, self.L1R_L2LOSS]: warnings.warn("""\ Deprecated 'solver_type', use 'Orange.classification.logreg.LibLinearLogRegLearner' to build a logistic regression model using LIBLINEAR. """, DeprecationWarning) warnings.warn( " Deprecated 'solver_type', use " "'Orange.classification.logreg.LibLinearLogRegLearner'" "to build a logistic regression models using LIBLINEAR.", DeprecationWarning ) def __call__(self, data, weight_id=None): if data.domain.has_discrete_attributes(False) or self.normalization: dc = Orange.data.continuization.DomainContinuizer() dc.multinomial_treatment = dc.NValues dc = DomainContinuizer() dc.multinomial_treatment = self.multinomial_treatment dc.class_treatment = dc.Ignore dc.continuous_treatment = \ def __init__(self, C=1.0, eps=0.01, bias=1.0, normalization=True, **kwargs): normalization=True, multinomial_treatment=DomainContinuizer.NValues, **kwargs): """\ :param C: Regularization parameter (default 1.0) (default True) :type normalization: bool :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.bias = bias self.normalization = normalization self.multinomial_treatment = multinomial_treatment for name, val in kwargs.items(): setattr(self, name, val) if data.domain.has_discrete_attributes(False) or self.normalization: dc = Orange.data.continuization.DomainContinuizer() dc.multinomial_treatment = dc.NValues dc = DomainContinuizer() dc.multinomial_treatment = self.multinomial_treatment dc.class_treatment = dc.Ignore dc.continuous_treatment = \
• ## Orange/testing/unit/tests/test_linear.py

 r11017 from Orange.testing.testing import datasets_driven from Orange.classification.svm import LinearSVMLearner from Orange.data.preprocess import DomainContinuizer try: import unittest2 as unittest import numpy as np def clone(obj): return cPickle.loads(cPickle.dumps(obj)) def test_learner_with_bias_on(self, dataset): learner = self.learner learner_b = cPickle.loads(cPickle.dumps(learner)) learner_b = clone(learner) learner_b.bias = 1 try: def multinomial_test(self): data = Orange.data.Table("lenses") learner = clone(self.learner) learner.multinomial_treatment = DomainContinuizer.NValues classifier = learner(data) self.assertEqual(len(classifier.domain), 7) learner.multinomial_treatment = DomainContinuizer.FrequentIsBase classifier = learner(data) self.assertEqual(len(classifier.domain), 6) learner.multinomial_treatment = DomainContinuizer.ReportError with self.assertRaises(Orange.core.KernelException): classifier = learner(data) @datasets_driven(datasets=testing.CLASSIFICATION_DATASETS) class TestLinearSVMLearnerL2R_L2LOSS_DUAL(testing.LearnerTestCase): test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test test_learner_with_bias_on = test_learner_with_bias_on test_missing_instances = missing_instances_test test_multinomial = multinomial_test