comparison ensemble.xml @ 0:569eefee7ed8 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tools/sklearn commit 0e582cf1f3134c777cce3aa57d71b80ed95e6ba9
author bgruening
date Fri, 16 Feb 2018 09:18:19 -0500
parents
children 6e6726be0728
comparison
equal deleted inserted replaced
-1:000000000000 0:569eefee7ed8
1 <tool id="sklearn_ensemble" name="Ensemble methods" version="@VERSION@">
2 <description>for classification and regression</description>
3 <macros>
4 <import>main_macros.xml</import>
5 </macros>
6 <expand macro="python_requirements"/>
7 <expand macro="macro_stdio"/>
8 <version_command>echo "@VERSION@"</version_command>
9 <command><![CDATA[
10 python "$ensemble_script" '$inputs'
11 ]]>
12 </command>
13 <configfiles>
14 <inputs name="inputs"/>
15 <configfile name="ensemble_script">
16 <![CDATA[
17 import sys
18 import json
19 import numpy as np
20 import sklearn.ensemble
21 import pandas
22 import pickle
23 from scipy.io import mmread
24
25 input_json_path = sys.argv[1]
26 params = json.load(open(input_json_path, "r"))
27
28 #if $selected_tasks.selected_task == "train":
29
30 algorithm = params["selected_tasks"]["selected_algorithms"]["selected_algorithm"]
31 options = params["selected_tasks"]["selected_algorithms"]["options"]
32 input_type = params["selected_tasks"]["selected_algorithms"]["input_options"]["selected_input"]
33 if input_type=="tabular":
34 col1 = params["selected_tasks"]["selected_algorithms"]["input_options"]["col1"]
35 col1 = list(map(lambda x: x - 1, col1))
36 f1 = pandas.read_csv("$selected_tasks.selected_algorithms.input_options.infile1", sep='\t', header=None, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
37 X = f1.iloc[:,col1].values
38 else:
39 X = mmread(open("$selected_tasks.selected_algorithms.input_options.infile1", 'r'))
40
41 col2 = params["selected_tasks"]["selected_algorithms"]["input_options"]["col2"]
42 col2 = list(map(lambda x: x - 1, col2))
43 f2 = pandas.read_csv("$selected_tasks.selected_algorithms.input_options.infile2", sep='\t', header=None, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
44 y = f2.iloc[:,col2].values
45
46 my_class = getattr(sklearn.ensemble, algorithm)
47 estimator = my_class(**options)
48 estimator.fit(X,y)
49 pickle.dump(estimator,open("$outfile_fit", 'w+'), pickle.HIGHEST_PROTOCOL)
50
51 #else:
52 classifier_object = pickle.load(open("$selected_tasks.infile_model", 'r'))
53 data = pandas.read_csv("$selected_tasks.infile_data", sep='\t', header=0, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
54 prediction = classifier_object.predict(data)
55 prediction_df = pandas.DataFrame(prediction)
56 res = pandas.concat([data, prediction_df], axis=1)
57 res.to_csv(path_or_buf = "$outfile_predict", sep="\t", index=False)
58 #end if
59
60 ]]>
61 </configfile>
62 </configfiles>
63 <inputs>
64 <expand macro="sl_Conditional" model="zip">
65 <param name="selected_algorithm" type="select" label="Select an ensemble method:">
66 <option value="RandomForestClassifier" selected="true">Random forest classifier</option>
67 <option value="AdaBoostClassifier">Ada boost classifier</option>
68 <option value="RandomForestRegressor">Random forest regressor</option>
69 <option value="AdaBoostRegressor">Ada boost regressor</option>
70 </param>
71 <when value="RandomForestClassifier">
72 <expand macro="sl_mixed_input"/>
73 <section name="options" title="Advanced Options" expanded="False">
74 <expand macro="n_estimators"/>
75 <expand macro="criterion"/>
76 <expand macro="max_features"/>
77 <expand macro="max_depth"/>
78 <expand macro="min_samples_split"/>
79 <expand macro="min_samples_leaf"/>
80 <expand macro="min_weight_fraction_leaf"/>
81 <expand macro="max_leaf_nodes"/>
82 <expand macro="bootstrap"/>
83 <expand macro="warm_start" checked="false"/>
84 <expand macro="random_state"/>
85 <expand macro="oob_score"/>
86 <!--class_weight=None-->
87 </section>
88 </when>
89 <when value="AdaBoostClassifier">
90 <expand macro="sl_mixed_input"/>
91 <section name="options" title="Advanced Options" expanded="False">
92 <!--base_estimator=None-->
93 <expand macro="n_estimators" default_value="50"/>
94 <expand macro="learning_rate"/>
95 <param argument="algorithm" type="select" label="Boosting algorithm" help=" ">
96 <option value="SAMME.R" selected="true">SAMME.R</option>
97 <option value="SAMME">SAMME</option>
98 </param>
99 <expand macro="random_state"/>
100 </section>
101 </when>
102 <when value="RandomForestRegressor">
103 <expand macro="sl_mixed_input"/>
104 <section name="options" title="Advanced Options" expanded="False">
105 <expand macro="n_estimators"/>
106 <expand macro="max_features"/>
107 <expand macro="max_depth"/>
108 <expand macro="min_samples_split"/>
109 <expand macro="min_samples_leaf"/>
110 <expand macro="min_weight_fraction_leaf"/>
111 <expand macro="max_leaf_nodes"/>
112 <expand macro="bootstrap"/>
113 <expand macro="warm_start" checked="false"/>
114 <expand macro="random_state"/>
115 <expand macro="oob_score"/>
116 </section>
117 </when>
118 <when value="AdaBoostRegressor">
119 <expand macro="sl_mixed_input"/>
120 <section name="options" title="Advanced Options" expanded="False">
121 <!--base_estimator=None-->
122 <expand macro="n_estimators" default_value="50"/>
123 <expand macro="learning_rate"/>
124 <param argument="loss" type="select" label="Loss function" optional="true" help="Used when updating the weights after each boosting iteration. ">
125 <option value="linear" selected="true">linear</option>
126 <option value="square">square</option>
127 <option value="exponential">exponential</option>
128 </param>
129 <expand macro="random_state"/>
130 </section>
131 </when>
132 </expand>
133 </inputs>
134
135 <expand macro="output"/>
136
137 <tests>
138 <test>
139 <param name="infile1" value="train.tabular" ftype="tabular"/>
140 <param name="infile2" value="train.tabular" ftype="tabular"/>
141 <param name="col1" value="1,2,3,4"/>
142 <param name="col2" value="5"/>
143 <param name="selected_task" value="train"/>
144 <param name="selected_algorithm" value="RandomForestClassifier"/>
145 <param name="random_state" value="10"/>
146 <output name="outfile_fit" file="rfc_model01" compare="sim_size" delta="500"/>
147 </test>
148 <test>
149 <param name="infile_model" value="rfc_model01" ftype="zip"/>
150 <param name="infile_data" value="test.tabular" ftype="tabular"/>
151 <param name="selected_task" value="load"/>
152 <output name="outfile_predict" file="rfc_result01" compare="sim_size" delta="500"/>
153 </test>
154
155 <test>
156 <param name="infile1" value="regression_train.tabular" ftype="tabular"/>
157 <param name="infile2" value="regression_train.tabular" ftype="tabular"/>
158 <param name="col1" value="1,2,3,4,5"/>
159 <param name="col2" value="6"/>
160 <param name="selected_task" value="train"/>
161 <param name="selected_algorithm" value="RandomForestRegressor"/>
162 <param name="random_state" value="10"/>
163 <output name="outfile_fit" file="rfr_model01" compare="sim_size" delta="500"/>
164 </test>
165 <test>
166 <param name="infile_model" value="rfr_model01" ftype="zip"/>
167 <param name="infile_data" value="regression_test.tabular" ftype="tabular"/>
168 <param name="selected_task" value="load"/>
169 <output name="outfile_predict" file="rfr_result01" compare="sim_size" delta="500"/>
170 </test>
171 </tests>
172 <help><![CDATA[
173 ***What it does***
174 The goal of ensemble methods is to combine the predictions of several base estimators built with a given learning algorithm in order to improve generalizability / robustness over a single estimator. This tool offers two sets of ensemble algorithms for classification and regression: random forests and ADA boosting which are based on sklearn.ensemble library from Scikit-learn. Here you can find out about the input, output and methods presented in the tools. For information about ensemble methods and parameters settings please refer to `Scikit-learn ensemble`_.
175
176 .. _`Scikit-learn ensemble`: http://scikit-learn.org/stable/modules/ensemble.html
177
178 **1 - Methods**
179 There are two groups of operations available:
180
181 1 - Train a model : A training set containing samples and their respective labels (or predicted values) are input. Based on the selected algorithm and options, an estimator object is fit to the data and is returned.
182
183 2 - Load a model and predict : An existing model predicts the class labels (or regression values) for a new dataset.
184
185 **2 - Trainig input**
186 When you choose to train a model, you need a features dataset X and a labels set y. This tool expects tabular or sparse data for X and a single column for y (tabular). You can select a subset of columns in a tabular dataset as your features dataset or labels column. Below you find some examples:
187
188 **Sample tabular features dataset**
189 The following training dataset contains 3 feature columns and a column containing class labels. You can simply select the first 3 columns as features and the last column as labels:
190
191 ::
192
193 4.01163365529 -6.10797684314 8.29829894763 1
194 10.0788438916 1.59539821454 10.0684278289 0
195 -5.17607775503 -0.878286135332 6.92941850665 2
196 4.00975406235 -7.11847496542 9.3802423585 1
197 4.61204065139 -5.71217537352 9.12509610964 1
198
199
200 **Sample sparse features dataset**
201 In this case you cannot specifiy a column range.
202
203 ::
204
205 4 1048577 8738
206 1 271 0.02083333333333341
207 1 1038 0.02461995616119806
208 2 829017 0.01629088031127686
209 2 829437 0.01209127083516686
210 2 830752 0.02535100632816968
211 3 1047487 0.01485722929945572
212 3 1047980 0.02640566620767753
213 3 1048475 0.01665869913262564
214 4 608 0.01662975263094352
215 4 1651 0.02519674277562741
216 4 4053 0.04223659971350601
217
218
219 **2 - Trainig output**
220 The trained model is generated and output in the form of a binary file.
221
222
223 **3 - Prediction input**
224
225 When you choose to load a model and do prediction, the tool expects an already trained estimator and a tabular dataset as input. The dataset contains new samples which you want to classify or predict regression values for.
226
227
228 .. class:: warningmark
229
230 The number of feature columns must be the same in training and prediction datasets!
231
232
233 **3 - Prediction output**
234 The tool predicts the class labels for new samples and adds them as the last column to the prediction dataset. The new dataset then is output as a tabular file. The prediction output format should look like the training dataset.
235
236 ]]></help>
237 <expand macro="sklearn_citation"/>
238 </tool>