annotate iraps_classifier.py @ 0:2ad4c2798be7 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
author bgruening
date Tue, 14 May 2019 18:12:53 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
1 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
2 class IRAPSCore
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
3 class IRAPSClassifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
4 class BinarizeTargetClassifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
5 class BinarizeTargetRegressor
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
6 class _BinarizeTargetScorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
7 class _BinarizeTargetProbaScorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
8
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
9 binarize_auc_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
10 binarize_average_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
11
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
12 binarize_accuracy_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
13 binarize_balanced_accuracy_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
14 binarize_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
15 binarize_recall_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
16 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
17
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
18
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
19 import numpy as np
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
20 import random
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
21 import warnings
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
22
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
23 from abc import ABCMeta
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
24 from scipy.stats import ttest_ind
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
25 from sklearn import metrics
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
26 from sklearn.base import BaseEstimator, clone, RegressorMixin
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
27 from sklearn.externals import six
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
28 from sklearn.feature_selection.univariate_selection import _BaseFilter
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
29 from sklearn.metrics.scorer import _BaseScorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
30 from sklearn.pipeline import Pipeline
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
31 from sklearn.utils import as_float_array, check_X_y
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
32 from sklearn.utils._joblib import Parallel, delayed
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
33 from sklearn.utils.validation import (check_array, check_is_fitted,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
34 check_memory, column_or_1d)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
35
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
36
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
37 VERSION = '0.1.1'
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
38
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
39
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
40 class IRAPSCore(six.with_metaclass(ABCMeta, BaseEstimator)):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
41 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
42 Base class of IRAPSClassifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
43 From sklearn BaseEstimator:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
44 get_params()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
45 set_params()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
46
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
47 Parameters
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
48 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
49 n_iter : int
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
50 sample count
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
51
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
52 positive_thres : float
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
53 z_score shreshold to discretize positive target values
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
54
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
55 negative_thres : float
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
56 z_score threshold to discretize negative target values
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
57
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
58 verbose : int
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
59 0 or geater, if not 0, print progress
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
60
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
61 n_jobs : int, default=1
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
62 The number of CPUs to use to do the computation.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
63
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
64 pre_dispatch : int, or string.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
65 Controls the number of jobs that get dispatched during parallel
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
66 execution. Reducing this number can be useful to avoid an
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
67 explosion of memory consumption when more jobs get dispatched
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
68 than CPUs can process. This parameter can be:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
69 - None, in which case all the jobs are immediately
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
70 created and spawned. Use this for lightweight and
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
71 fast-running jobs, to avoid delays due to on-demand
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
72 spawning of the jobs
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
73 - An int, giving the exact number of total jobs that are
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
74 spawned
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
75 - A string, giving an expression as a function of n_jobs,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
76 as in '2*n_jobs'
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
77
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
78 random_state : int or None
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
79 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
80
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
81 def __init__(self, n_iter=1000, positive_thres=-1, negative_thres=0,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
82 verbose=0, n_jobs=1, pre_dispatch='2*n_jobs',
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
83 random_state=None):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
84 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
85 IRAPS turns towwards general Anomaly Detection
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
86 It comapares positive_thres with negative_thres,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
87 and decide which portion is the positive target.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
88 e.g.:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
89 (positive_thres=-1, negative_thres=0)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
90 => positive = Z_score of target < -1
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
91 (positive_thres=1, negative_thres=0)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
92 => positive = Z_score of target > 1
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
93
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
94 Note: The positive targets here is always the
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
95 abnormal minority group.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
96 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
97 self.n_iter = n_iter
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
98 self.positive_thres = positive_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
99 self.negative_thres = negative_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
100 self.verbose = verbose
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
101 self.n_jobs = n_jobs
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
102 self.pre_dispatch = pre_dispatch
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
103 self.random_state = random_state
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
104
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
105 def fit(self, X, y):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
106 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
107 X: array-like (n_samples x n_features)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
108 y: 1-d array-like (n_samples)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
109 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
110 X, y = check_X_y(X, y, ['csr', 'csc'], multi_output=False)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
111
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
112 def _stochastic_sampling(X, y, random_state=None, positive_thres=-1,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
113 negative_thres=0):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
114 # each iteration select a random number of random subset of
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
115 # training samples. this is somewhat different from the original
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
116 # IRAPS method, but effect is almost the same.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
117 SAMPLE_SIZE = [0.25, 0.75]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
118 n_samples = X.shape[0]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
119
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
120 if random_state is None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
121 n_select = random.randint(int(n_samples * SAMPLE_SIZE[0]),
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
122 int(n_samples * SAMPLE_SIZE[1]))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
123 index = random.sample(list(range(n_samples)), n_select)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
124 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
125 n_select = random.Random(random_state).randint(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
126 int(n_samples * SAMPLE_SIZE[0]),
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
127 int(n_samples * SAMPLE_SIZE[1]))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
128 index = random.Random(random_state).sample(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
129 list(range(n_samples)), n_select)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
130
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
131 X_selected, y_selected = X[index], y[index]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
132
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
133 # Spliting by z_scores.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
134 y_selected = (y_selected - y_selected.mean()) / y_selected.std()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
135 if positive_thres < negative_thres:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
136 X_selected_positive = X_selected[y_selected < positive_thres]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
137 X_selected_negative = X_selected[y_selected > negative_thres]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
138 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
139 X_selected_positive = X_selected[y_selected > positive_thres]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
140 X_selected_negative = X_selected[y_selected < negative_thres]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
141
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
142 # For every iteration, at least 5 responders are selected
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
143 if X_selected_positive.shape[0] < 5:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
144 warnings.warn("Warning: fewer than 5 positives were selected!")
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
145 return
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
146
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
147 # p_values
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
148 _, p = ttest_ind(X_selected_positive, X_selected_negative,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
149 axis=0, equal_var=False)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
150
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
151 # fold_change == mean change?
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
152 # TODO implement other normalization method
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
153 positive_mean = X_selected_positive.mean(axis=0)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
154 negative_mean = X_selected_negative.mean(axis=0)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
155 mean_change = positive_mean - negative_mean
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
156 # mean_change = np.select(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
157 # [positive_mean > negative_mean,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
158 # positive_mean < negative_mean],
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
159 # [positive_mean / negative_mean,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
160 # -negative_mean / positive_mean])
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
161 # mean_change could be adjusted by power of 2
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
162 # mean_change = 2**mean_change \
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
163 # if mean_change>0 else -2**abs(mean_change)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
164
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
165 return p, mean_change, negative_mean
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
166
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
167 parallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
168 pre_dispatch=self.pre_dispatch)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
169 if self.random_state is None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
170 res = parallel(delayed(_stochastic_sampling)(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
171 X, y, random_state=None,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
172 positive_thres=self.positive_thres,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
173 negative_thres=self.negative_thres)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
174 for i in range(self.n_iter))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
175 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
176 res = parallel(delayed(_stochastic_sampling)(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
177 X, y, random_state=seed,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
178 positive_thres=self.positive_thres,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
179 negative_thres=self.negative_thres)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
180 for seed in range(self.random_state,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
181 self.random_state+self.n_iter))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
182 res = [_ for _ in res if _]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
183 if len(res) < 50:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
184 raise ValueError("too few (%d) valid feature lists "
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
185 "were generated!" % len(res))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
186 pvalues = np.vstack([x[0] for x in res])
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
187 fold_changes = np.vstack([x[1] for x in res])
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
188 base_values = np.vstack([x[2] for x in res])
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
189
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
190 self.pvalues_ = np.asarray(pvalues)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
191 self.fold_changes_ = np.asarray(fold_changes)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
192 self.base_values_ = np.asarray(base_values)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
193
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
194 return self
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
195
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
196
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
197 def _iraps_core_fit(iraps_core, X, y):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
198 return iraps_core.fit(X, y)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
199
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
200
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
201 class IRAPSClassifier(six.with_metaclass(ABCMeta, _BaseFilter,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
202 BaseEstimator, RegressorMixin)):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
203 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
204 Extend the bases of both sklearn feature_selector and classifier.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
205 From sklearn BaseEstimator:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
206 get_params()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
207 set_params()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
208 From sklearn _BaseFilter:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
209 get_support()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
210 fit_transform(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
211 transform(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
212 From sklearn RegressorMixin:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
213 score(X, y): R2
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
214 New:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
215 predict(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
216 predict_label(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
217 get_signature()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
218 Properties:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
219 discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
220
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
221 Parameters
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
222 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
223 iraps_core: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
224 p_thres: float, threshold for p_values
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
225 fc_thres: float, threshold for fold change or mean difference
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
226 occurrence: float, occurrence rate selected by set of p_thres and fc_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
227 discretize: float, threshold of z_score to discretize target value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
228 memory: None, str or joblib.Memory object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
229 min_signature_features: int, the mininum number of features in a signature
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
230 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
231
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
232 def __init__(self, iraps_core, p_thres=1e-4, fc_thres=0.1,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
233 occurrence=0.8, discretize=-1, memory=None,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
234 min_signature_features=1):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
235 self.iraps_core = iraps_core
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
236 self.p_thres = p_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
237 self.fc_thres = fc_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
238 self.occurrence = occurrence
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
239 self.discretize = discretize
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
240 self.memory = memory
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
241 self.min_signature_features = min_signature_features
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
242
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
243 def fit(self, X, y):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
244 memory = check_memory(self.memory)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
245 cached_fit = memory.cache(_iraps_core_fit)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
246 iraps_core = clone(self.iraps_core)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
247 # allow pre-fitted iraps_core here
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
248 if not hasattr(iraps_core, 'pvalues_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
249 iraps_core = cached_fit(iraps_core, X, y)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
250 self.iraps_core_ = iraps_core
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
251
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
252 pvalues = as_float_array(iraps_core.pvalues_, copy=True)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
253 # why np.nan is here?
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
254 pvalues[np.isnan(pvalues)] = np.finfo(pvalues.dtype).max
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
255
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
256 fold_changes = as_float_array(iraps_core.fold_changes_, copy=True)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
257 fold_changes[np.isnan(fold_changes)] = 0.0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
258
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
259 base_values = as_float_array(iraps_core.base_values_, copy=True)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
260
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
261 p_thres = self.p_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
262 fc_thres = self.fc_thres
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
263 occurrence = self.occurrence
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
264
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
265 mask_0 = np.zeros(pvalues.shape, dtype=np.int32)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
266 # mark p_values less than the threashold
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
267 mask_0[pvalues <= p_thres] = 1
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
268 # mark fold_changes only when greater than the threashold
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
269 mask_0[abs(fold_changes) < fc_thres] = 0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
270
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
271 # count the occurrence and mask greater than the threshold
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
272 counts = mask_0.sum(axis=0)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
273 occurrence_thres = int(occurrence * iraps_core.n_iter)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
274 mask = np.zeros(counts.shape, dtype=bool)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
275 mask[counts >= occurrence_thres] = 1
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
276
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
277 # generate signature
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
278 fold_changes[mask_0 == 0] = 0.0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
279 signature = fold_changes[:, mask].sum(axis=0) / counts[mask]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
280 signature = np.vstack((signature, base_values[:, mask].mean(axis=0)))
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
281 # It's not clearn whether min_size could impact prediction
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
282 # performance
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
283 if signature is None\
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
284 or signature.shape[1] < self.min_signature_features:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
285 raise ValueError("The classifier got None signature or the number "
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
286 "of sinature feature is less than minimum!")
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
287
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
288 self.signature_ = np.asarray(signature)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
289 self.mask_ = mask
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
290 # TODO: support other discretize method: fixed value, upper
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
291 # third quater, etc.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
292 self.discretize_value = y.mean() + y.std() * self.discretize
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
293 if iraps_core.negative_thres > iraps_core.positive_thres:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
294 self.less_is_positive = True
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
295 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
296 self.less_is_positive = False
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
297
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
298 return self
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
299
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
300 def _get_support_mask(self):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
301 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
302 return mask of feature selection indices
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
303 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
304 check_is_fitted(self, 'mask_')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
305
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
306 return self.mask_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
307
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
308 def get_signature(self):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
309 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
310 return signature
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
311 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
312 check_is_fitted(self, 'signature_')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
313
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
314 return self.signature_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
315
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
316 def predict(self, X):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
317 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
318 compute the correlation coefficient with irpas signature
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
319 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
320 signature = self.get_signature()
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
321
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
322 X = as_float_array(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
323 X_transformed = self.transform(X) - signature[1]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
324 corrcoef = np.array(
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
325 [np.corrcoef(signature[0], e)[0][1] for e in X_transformed])
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
326 corrcoef[np.isnan(corrcoef)] = np.finfo(np.float32).min
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
327
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
328 return corrcoef
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
329
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
330 def predict_label(self, X, clf_cutoff=0.4):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
331 return self.predict(X) >= clf_cutoff
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
332
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
333
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
334 class BinarizeTargetClassifier(BaseEstimator, RegressorMixin):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
335 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
336 Convert continuous target to binary labels (True and False)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
337 and apply a classification estimator.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
338
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
339 Parameters
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
340 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
341 classifier: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
342 Estimator object such as derived from sklearn `ClassifierMixin`.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
343
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
344 z_score: float, default=-1.0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
345 Threshold value based on z_score. Will be ignored when
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
346 fixed_value is set
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
347
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
348 value: float, default=None
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
349 Threshold value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
350
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
351 less_is_positive: boolean, default=True
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
352 When target is less the threshold value, it will be converted
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
353 to True, False otherwise.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
354
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
355 Attributes
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
356 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
357 classifier_: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
358 Fitted classifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
359
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
360 discretize_value: float
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
361 The threshold value used to discretize True and False targets
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
362 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
363
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
364 def __init__(self, classifier, z_score=-1, value=None,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
365 less_is_positive=True):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
366 self.classifier = classifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
367 self.z_score = z_score
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
368 self.value = value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
369 self.less_is_positive = less_is_positive
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
370
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
371 def fit(self, X, y, sample_weight=None):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
372 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
373 Convert y to True and False labels and then fit the classifier
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
374 with X and new y
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
375
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
376 Returns
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
377 ------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
378 self: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
379 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
380 y = check_array(y, accept_sparse=False, force_all_finite=True,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
381 ensure_2d=False, dtype='numeric')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
382 y = column_or_1d(y)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
383
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
384 if self.value is None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
385 discretize_value = y.mean() + y.std() * self.z_score
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
386 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
387 discretize_value = self.Value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
388 self.discretize_value = discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
389
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
390 if self.less_is_positive:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
391 y_trans = y < discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
392 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
393 y_trans = y > discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
394
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
395 self.classifier_ = clone(self.classifier)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
396
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
397 if sample_weight is not None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
398 self.classifier_.fit(X, y_trans, sample_weight=sample_weight)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
399 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
400 self.classifier_.fit(X, y_trans)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
401
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
402 if hasattr(self.classifier_, 'feature_importances_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
403 self.feature_importances_ = self.classifier_.feature_importances_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
404 if hasattr(self.classifier_, 'coef_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
405 self.coef_ = self.classifier_.coef_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
406 if hasattr(self.classifier_, 'n_outputs_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
407 self.n_outputs_ = self.classifier_.n_outputs_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
408 if hasattr(self.classifier_, 'n_features_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
409 self.n_features_ = self.classifier_.n_features_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
410
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
411 return self
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
412
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
413 def predict(self, X):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
414 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
415 Predict class probabilities of X.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
416 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
417 check_is_fitted(self, 'classifier_')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
418 proba = self.classifier_.predict_proba(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
419 return proba[:, 1]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
420
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
421 def predict_label(self, X):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
422 """Predict class label of X
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
423 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
424 check_is_fitted(self, 'classifier_')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
425 return self.classifier_.predict(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
426
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
427
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
428 class _BinarizeTargetProbaScorer(_BaseScorer):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
429 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
430 base class to make binarized target specific scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
431 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
432
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
433 def __call__(self, clf, X, y, sample_weight=None):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
434 clf_name = clf.__class__.__name__
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
435 # support pipeline object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
436 if isinstance(clf, Pipeline):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
437 main_estimator = clf.steps[-1][-1]
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
438 # support stacking ensemble estimators
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
439 # TODO support nested pipeline/stacking estimators
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
440 elif clf_name in ['StackingCVClassifier', 'StackingClassifier']:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
441 main_estimator = clf.meta_clf_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
442 elif clf_name in ['StackingCVRegressor', 'StackingRegressor']:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
443 main_estimator = clf.meta_regr_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
444 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
445 main_estimator = clf
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
446
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
447 discretize_value = main_estimator.discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
448 less_is_positive = main_estimator.less_is_positive
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
449
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
450 if less_is_positive:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
451 y_trans = y < discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
452 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
453 y_trans = y > discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
454
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
455 y_pred = clf.predict(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
456 if sample_weight is not None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
457 return self._sign * self._score_func(y_trans, y_pred,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
458 sample_weight=sample_weight,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
459 **self._kwargs)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
460 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
461 return self._sign * self._score_func(y_trans, y_pred,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
462 **self._kwargs)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
463
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
464
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
465 # roc_auc
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
466 binarize_auc_scorer =\
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
467 _BinarizeTargetProbaScorer(metrics.roc_auc_score, 1, {})
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
468
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
469 # average_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
470 binarize_average_precision_scorer =\
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
471 _BinarizeTargetProbaScorer(metrics.average_precision_score, 1, {})
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
472
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
473 # roc_auc_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
474 iraps_auc_scorer = binarize_auc_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
475
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
476 # average_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
477 iraps_average_precision_scorer = binarize_average_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
478
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
479
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
480 class BinarizeTargetRegressor(BaseEstimator, RegressorMixin):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
481 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
482 Extend regression estimator to have discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
483
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
484 Parameters
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
485 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
486 regressor: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
487 Estimator object such as derived from sklearn `RegressionMixin`.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
488
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
489 z_score: float, default=-1.0
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
490 Threshold value based on z_score. Will be ignored when
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
491 fixed_value is set
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
492
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
493 value: float, default=None
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
494 Threshold value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
495
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
496 less_is_positive: boolean, default=True
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
497 When target is less the threshold value, it will be converted
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
498 to True, False otherwise.
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
499
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
500 Attributes
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
501 ----------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
502 regressor_: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
503 Fitted regressor
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
504
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
505 discretize_value: float
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
506 The threshold value used to discretize True and False targets
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
507 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
508
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
509 def __init__(self, regressor, z_score=-1, value=None,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
510 less_is_positive=True):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
511 self.regressor = regressor
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
512 self.z_score = z_score
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
513 self.value = value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
514 self.less_is_positive = less_is_positive
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
515
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
516 def fit(self, X, y, sample_weight=None):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
517 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
518 Calculate the discretize_value fit the regressor with traning data
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
519
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
520 Returns
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
521 ------
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
522 self: object
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
523 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
524 y = check_array(y, accept_sparse=False, force_all_finite=True,
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
525 ensure_2d=False, dtype='numeric')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
526 y = column_or_1d(y)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
527
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
528 if self.value is None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
529 discretize_value = y.mean() + y.std() * self.z_score
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
530 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
531 discretize_value = self.Value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
532 self.discretize_value = discretize_value
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
533
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
534 self.regressor_ = clone(self.regressor)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
535
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
536 if sample_weight is not None:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
537 self.regressor_.fit(X, y, sample_weight=sample_weight)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
538 else:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
539 self.regressor_.fit(X, y)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
540
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
541 # attach classifier attributes
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
542 if hasattr(self.regressor_, 'feature_importances_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
543 self.feature_importances_ = self.regressor_.feature_importances_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
544 if hasattr(self.regressor_, 'coef_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
545 self.coef_ = self.regressor_.coef_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
546 if hasattr(self.regressor_, 'n_outputs_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
547 self.n_outputs_ = self.regressor_.n_outputs_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
548 if hasattr(self.regressor_, 'n_features_'):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
549 self.n_features_ = self.regressor_.n_features_
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
550
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
551 return self
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
552
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
553 def predict(self, X):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
554 """Predict target value of X
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
555 """
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
556 check_is_fitted(self, 'regressor_')
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
557 y_pred = self.regressor_.predict(X)
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
558 if not np.all((y_pred >= 0) & (y_pred <= 1)):
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
559 y_pred = (y_pred - y_pred.min()) / (y_pred.max() - y_pred.min())
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
560 if self.less_is_positive:
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
561 y_pred = 1 - y_pred
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
562 return y_pred
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
563
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
564
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
565 # roc_auc_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
566 regression_auc_scorer = binarize_auc_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
567
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
568 # average_precision_scorer
2ad4c2798be7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit c0a3a186966888e5787335a7628bf0a4382637e7
bgruening
parents:
diff changeset
569 regression_average_precision_scorer = binarize_average_precision_scorer