Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_decision_function(self):
from lale.lib.imblearn import SMOTE
from lale.operators import make_pipeline
from lale.lib.sklearn import RandomForestClassifier
smote = SMOTE(operator=make_pipeline(RandomForestClassifier()))
trained = smote.fit(self.X_train, self.y_train)
trained.predict(self.X_test)
with self.assertRaises(AttributeError):
trained.decision_function(self.X_test)
def test_concat_with_hyperopt2(self):
from lale.operators import make_pipeline, make_union
from lale.lib.lale import Hyperopt
pca = PCA(n_components=3)
nys = Nystroem(n_components=10)
concat = ConcatFeatures()
lr = LogisticRegression(random_state=42, C=0.1)
trainable = make_pipeline(make_union(pca, nys), lr)
clf = Hyperopt(estimator=trainable, max_evals=2)
from sklearn.datasets import load_iris
iris_data = load_iris()
clf.fit(iris_data.data, iris_data.target)
clf.predict(iris_data.data)
def test_export_to_sklearn_pipeline4(self):
from lale.lib.sklearn import LogisticRegression
from lale.operators import make_pipeline
lale_pipeline = make_pipeline(LogisticRegression())
trained_lale_pipeline = lale_pipeline.fit(self.X_train, self.y_train)
sklearn_pipeline = trained_lale_pipeline.export_to_sklearn_pipeline()
from sklearn.linear_model import LogisticRegression
self.assertIsInstance(sklearn_pipeline.named_steps['logisticregression'], LogisticRegression)
self.assert_equal_predictions(sklearn_pipeline, trained_lale_pipeline)
def test_make_pipeline(self):
from lale.operators import make_pipeline
tfm = PCA(n_components=10)
clf = LogisticRegression(random_state=42)
trainable = make_pipeline(tfm, clf)
digits = sklearn.datasets.load_digits()
trained = trainable.fit(digits.data, digits.target)
predicted = trained.predict(digits.data)
def test_compose2(self):
def test_pipeline_2(self):
from lale.lib.lale import NoOp
from lale.lib.sklearn import Nystroem
from lale.lib.sklearn import PCA
from lale.lib.sklearn import LogisticRegression
from lale.lib.sklearn import KNeighborsClassifier
from lale.operators import make_choice, make_pipeline
from lale.json_operator import to_json, from_json
kernel_tfm_or_not = make_choice(NoOp, Nystroem)
tfm = PCA
clf = make_choice(LogisticRegression, KNeighborsClassifier)
operator = make_pipeline(kernel_tfm_or_not, tfm, clf)
json = to_json(operator)
operator_2 = from_json(json)
json_2 = to_json(operator_2)
self.assertEqual(json, json_2)
def test_autoai_libs_tam_2(self):
from lale.lib.autoai_libs import TAM
import numpy as np
from lightgbm import LGBMClassifier
from sklearn.decomposition import PCA
from lale.operators import make_pipeline
pca = PCA(copy=False)
tam = TAM(tans_class=pca, name='pca', col_names=['a', 'b', 'c'], col_dtypes=[np.dtype('float32'), np.dtype('float32'), np.dtype('float32')])
lgbm_classifier = LGBMClassifier(class_weight='balanced', learning_rate=0.18)
pipeline = make_pipeline(tam, lgbm_classifier)
expected = \
"""from lale.lib.autoai_libs import TAM
import sklearn.decomposition.pca
import numpy as np
from lightgbm import LGBMClassifier
from lale.operators import make_pipeline
tam = TAM(tans_class=sklearn.decomposition.pca.PCA(copy=False, iterated_power='auto', n_components=None, random_state=None, svd_solver='auto', tol=0.0, whiten=False), name='pca', col_names=['a', 'b', 'c'], col_dtypes=[np.dtype('float32'), np.dtype('float32'), np.dtype('float32')])
lgbm_classifier = LGBMClassifier(class_weight='balanced', learning_rate=0.18)
pipeline = make_pipeline(tam, lgbm_classifier)"""
self._roundtrip(expected, lale.pretty_print.to_string(pipeline, combinators=False))
class_ = lale_op(**hyperparams)
if lale_wrapper_found:
wrapped_model = copy.deepcopy(sklearn_obj)
class_._impl_instance()._wrapped_model = wrapped_model
else:# If there is no lale wrapper, there is no _wrapped_model
class_._impl = copy.deepcopy(sklearn_obj)
return class_
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.base import BaseEstimator
from lale.operators import make_pipeline, make_union
if isinstance(sklearn_pipeline, Pipeline):
nested_pipeline_steps = sklearn_pipeline.named_steps.values()
nested_pipeline_lale_objects = [import_from_sklearn_pipeline(nested_pipeline_step, fitted=fitted) for nested_pipeline_step in nested_pipeline_steps]
lale_op_obj = make_pipeline(*nested_pipeline_lale_objects)
elif isinstance(sklearn_pipeline, FeatureUnion):
transformer_list = sklearn_pipeline.transformer_list
concat_predecessors = [import_from_sklearn_pipeline(transformer[1], fitted=fitted) for transformer in transformer_list]
lale_op_obj = make_union(*concat_predecessors)
else:
lale_op_obj = get_equivalent_lale_op(sklearn_pipeline, fitted=fitted)
return lale_op_obj
Explore the grammar `g` starting from `g.start` and generate all possible choices after `n` derivations.
Parameters
----------
g : Grammar
input grammar
n : int
number of derivations
Returns
-------
PlannedOperator
"""
assert hasattr(self, 'start'), "Rule start must be defined"
op = self._unfold(self.start, n)
return make_pipeline(op) if op else NoOp
def sample(self, n: int) -> PlannedOperator:
"""
Sample the grammar `g` starting from `g.start`, that is, choose one element at random for each possible choices.
Parameters
----------
n : int
number of derivations
Returns
-------
PlannedOperator
"""
assert hasattr(self, 'start'), "Rule start must be defined"
op = self._sample(self.start, n)
return make_pipeline(op) if op else NoOp