Skip to content

Commit 5852eeb

Browse files
committed
add helper for sample weighting
1 parent aeda4cd commit 5852eeb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

dataikuapi/dss/ml.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ def use_feature(self, feature_name):
167167
"""
168168
self.get_feature_preprocessing(feature_name)["role"] = "INPUT"
169169

170+
def use_sample_weighting(self, feature_name):
171+
"""
172+
Uses a feature as sample weight
173+
:param str feature_name: Name of the feature to use
174+
"""
175+
self.remove_sample_weighting()
176+
if not feature_name in self.mltask_settings["preprocessing"]["per_feature"]:
177+
raise ValueError("Feature %s doesn't exist in this ML task, can't use as weight" % feature_name)
178+
self.mltask_settings['weight']['weightMethod'] = 'SAMPLE_WEIGHT'
179+
self.mltask_settings['weight']['sampleWeightVariable'] = feature_name
180+
self.mltask_settings['preprocessing']['per_feature'][feature_name]['role'] = 'WEIGHT'
181+
182+
183+
def remove_sample_weighting(self):
184+
"""
185+
Remove sample weighting. If a feature was used as weight, it's set back to being an input feature
186+
"""
187+
self.mltask_settings['weight']['weightMethod'] = 'NO_WEIGHTING'
188+
for feature_name in self.mltask_settings['preprocessing']['per_feature']:
189+
if self.mltask_settings['preprocessing']['per_feature'][feature_name]['role'] == 'WEIGHT':
190+
self.mltask_settings['preprocessing']['per_feature'][feature_name]['role'] = 'INPUT'
191+
170192
def get_algorithm_settings(self, algorithm_name):
171193
"""
172194
Gets the training settings for a particular algorithm. This returns a reference to the

0 commit comments

Comments
 (0)