Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions openml/flows/sklearn_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ def check(param_grid, restricted_parameter_name, legal_values):
for param, value in param_grid.items():
# n_jobs is scikitlearn parameter for paralizing jobs
if param.split('__')[-1] == restricted_parameter_name:
# 0 = illegal value (?), 1 = use one core, n = use n cores
# 0 = illegal value (?), 1 / None = use one core,
# n = use n cores,
# -1 = use all available cores -> this makes it hard to
# measure runtime in a fair way
if legal_values is None or value not in legal_values:
Expand All @@ -650,7 +651,8 @@ def check(param_grid, restricted_parameter_name, legal_values):
isinstance(model, sklearn.model_selection._search.BaseSearchCV)):
raise ValueError('model should be BaseEstimator or BaseSearchCV')

# make sure that n_jobs is not in the parameter grid of optimization procedure
# make sure that n_jobs is not in the parameter grid of optimization
# procedure
if isinstance(model, sklearn.model_selection._search.BaseSearchCV):
if isinstance(model, sklearn.model_selection.GridSearchCV):
param_distributions = model.param_grid
Expand All @@ -663,13 +665,13 @@ def check(param_grid, restricted_parameter_name, legal_values):
raise AttributeError('Using subclass BaseSearchCV other than {GridSearchCV, RandomizedSearchCV}. Could not find attribute param_distributions. ')
print('Warning! Using subclass BaseSearchCV other than ' \
'{GridSearchCV, RandomizedSearchCV}. Should implement param check. ')

if not check(param_distributions, 'n_jobs', None):
raise PyOpenMLError('openml-python should not be used to '
'optimize the n_jobs parameter.')

# check the parameters for n_jobs
return check(model.get_params(), 'n_jobs', [1])
return check(model.get_params(), 'n_jobs', [1, None])


def _deserialize_cross_validator(value):
Expand Down
36 changes: 25 additions & 11 deletions tests/test_flows/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import sys
import time
from distutils.version import LooseVersion

if sys.version_info[0] >= 3:
from unittest import mock
Expand All @@ -22,6 +23,12 @@
import sklearn.preprocessing
import sklearn.naive_bayes
import sklearn.tree

if LooseVersion(sklearn.__version__) < "0.20":
from sklearn.preprocessing import Imputer
else:
from sklearn.impute import SimpleImputer as Imputer

import xmltodict

from openml.testing import TestBase
Expand Down Expand Up @@ -230,8 +237,8 @@ def test_publish_error(self, api_call_mock, get_flow_mock):

def test_illegal_flow(self):
# should throw error as it contains two imputers
illegal = sklearn.pipeline.Pipeline(steps=[('imputer1', sklearn.preprocessing.Imputer()),
('imputer2', sklearn.preprocessing.Imputer()),
illegal = sklearn.pipeline.Pipeline(steps=[('imputer1', Imputer()),
('imputer2', Imputer()),
('classif', sklearn.tree.DecisionTreeClassifier())])
self.assertRaises(ValueError, openml.flows.sklearn_to_flow, illegal)

Expand All @@ -256,9 +263,11 @@ def test_existing_flow_exists(self):
# create a flow
nb = sklearn.naive_bayes.GaussianNB()

steps = [('imputation', sklearn.preprocessing.Imputer(strategy='median')),
('hotencoding', sklearn.preprocessing.OneHotEncoder(sparse=False,
handle_unknown='ignore')),
ohe_params = {'sparse': False, 'handle_unknown': 'ignore'}
if LooseVersion(sklearn.__version__) >= '0.20':
ohe_params['categories'] = 'auto'
steps = [('imputation', Imputer(strategy='median')),
('hotencoding', sklearn.preprocessing.OneHotEncoder(**ohe_params)),
('variencethreshold', sklearn.feature_selection.VarianceThreshold()),
('classifier', sklearn.tree.DecisionTreeClassifier())]
complicated = sklearn.pipeline.Pipeline(steps=steps)
Expand All @@ -274,16 +283,18 @@ def test_existing_flow_exists(self):
# check if flow exists can find it
flow = openml.flows.get_flow(flow.flow_id)
downloaded_flow_id = openml.flows.flow_exists(flow.name, flow.external_version)
self.assertEquals(downloaded_flow_id, flow.flow_id)
self.assertEqual(downloaded_flow_id, flow.flow_id)

def test_sklearn_to_upload_to_flow(self):
iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target

# Test a more complicated flow
ohe = sklearn.preprocessing.OneHotEncoder(categorical_features=[1],
handle_unknown='ignore')
ohe_params = {'handle_unknown': 'ignore'}
Comment thread
mfeurer marked this conversation as resolved.
Resolved
if LooseVersion(sklearn.__version__) >= "0.20":
ohe_params['categories'] = 'auto'
ohe = sklearn.preprocessing.OneHotEncoder(**ohe_params)
scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
pca = sklearn.decomposition.TruncatedSVD()
fs = sklearn.feature_selection.SelectPercentile(
Expand Down Expand Up @@ -338,17 +349,20 @@ def test_sklearn_to_upload_to_flow(self):
openml.flows.functions.assert_flows_equal(new_flow, flow)
self.assertIsNot(new_flow, flow)

# OneHotEncoder was moved to _encoders module in 0.20
module_name_encoder = ('_encoders'
if LooseVersion(sklearn.__version__) >= "0.20"
else 'data')
fixture_name = '%ssklearn.model_selection._search.RandomizedSearchCV(' \
'estimator=sklearn.pipeline.Pipeline(' \
'ohe=sklearn.preprocessing.data.OneHotEncoder,' \
'ohe=sklearn.preprocessing.%s.OneHotEncoder,' \
'scaler=sklearn.preprocessing.data.StandardScaler,' \
'fu=sklearn.pipeline.FeatureUnion(' \
'pca=sklearn.decomposition.truncated_svd.TruncatedSVD,' \
'fs=sklearn.feature_selection.univariate_selection.SelectPercentile),' \
'boosting=sklearn.ensemble.weight_boosting.AdaBoostClassifier(' \
'base_estimator=sklearn.tree.tree.DecisionTreeClassifier)))' \
% sentinel

% (sentinel, module_name_encoder)
self.assertEqual(new_flow.name, fixture_name)
new_flow.model.fit(X, y)

Expand Down
Loading