File: linear.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (34 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download
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
import numpy as np

from Orange.classification.sgd import SGDClassificationLearner
from Orange.data import Variable
from Orange.modelling import SklFitter
from Orange.preprocess.score import LearnerScorer
from Orange.regression import SGDRegressionLearner

__all__ = ['SGDLearner']


class _FeatureScorerMixin(LearnerScorer):
    feature_type = Variable
    class_type = Variable

    def score(self, data):
        model = self.get_learner(data)(data)
        return (np.atleast_2d(np.abs(model.skl_model.coef_)).mean(0),
                model.domain.attributes)


class SGDLearner(SklFitter, _FeatureScorerMixin):
    name = 'sgd'

    __fits__ = {'classification': SGDClassificationLearner,
                'regression': SGDRegressionLearner}

    def _change_kwargs(self, kwargs, problem_type):
        pref = "classification" if problem_type is self.CLASSIFICATION else "regression"
        return kwargs | {
            attr: kwargs[pattr]
            for attr, pattr in ((attr, f"{pref}_{attr}")
                                for attr in ('loss', 'epsilon'))
            if pattr in kwargs}