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_make_choice_with_instance(self):
from lale.operators import make_union, make_choice, make_pipeline
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
tfm = PCA() | Nystroem() | NoOp()
with self.assertRaises(AttributeError):
trained = tfm.fit(X, y)
planned_pipeline1 = (OneHotEncoder | NoOp) >> tfm >> (LogisticRegression | KNeighborsClassifier)
planned_pipeline2 = (OneHotEncoder | NoOp) >> (PCA | Nystroem) >> (LogisticRegression | KNeighborsClassifier)
planned_pipeline3 = make_choice(OneHotEncoder, NoOp) >> make_choice(PCA, Nystroem) >> make_choice(LogisticRegression, KNeighborsClassifier)
def test_fit_args(self):
from sklearn.datasets import load_iris
from lale.lib.lale import TopKVotingClassifier
from lale.lib.sklearn import Nystroem
from sklearn.metrics import accuracy_score
ensemble = TopKVotingClassifier(estimator=(PCA() | Nystroem()) >> (LogisticRegression()|KNeighborsClassifier()), k=2)
trained = ensemble.fit(self.X_train, self.y_train)
trained.predict(self.X_test)
def test_export_to_sklearn_pipeline2(self):
from lale.lib.lale import ConcatFeatures
from lale.lib.sklearn import PCA
from lale.lib.sklearn import KNeighborsClassifier
from sklearn.feature_selection import SelectKBest
from lale.lib.sklearn import Nystroem
from sklearn.pipeline import FeatureUnion
lale_pipeline = (((PCA(svd_solver='randomized', random_state=42) & SelectKBest(k=3)) >> ConcatFeatures()) & Nystroem(random_state=42)) >> ConcatFeatures() >> KNeighborsClassifier()
trained_lale_pipeline = lale_pipeline.fit(self.X_train, self.y_train)
sklearn_pipeline = trained_lale_pipeline.export_to_sklearn_pipeline()
self.assertIsInstance(sklearn_pipeline.named_steps['featureunion'], FeatureUnion)
from sklearn.neighbors import KNeighborsClassifier
self.assertIsInstance(sklearn_pipeline.named_steps['kneighborsclassifier'], KNeighborsClassifier)
self.assert_equal_predictions(sklearn_pipeline, trained_lale_pipeline)
def test_with_concat_features2(self):
import warnings
warnings.filterwarnings("ignore")
from sklearn.datasets import load_iris
from lale.lib.lale import Hyperopt
from sklearn.metrics import accuracy_score
data = load_iris()
X, y = data.data, data.target
pca = PCA(n_components=3)
nys = Nystroem(n_components=10)
concat = ConcatFeatures()
lr = LogisticRegression(random_state=42, C=0.1)
from lale.operators import make_pipeline
pipeline = make_pipeline(((((SimpleImputer() | NoOp()) >> pca) & nys) >> concat >> lr) | KNeighborsClassifier())
clf = Hyperopt(estimator=pipeline, max_evals=1, handle_cv_failure=True)
trained = clf.fit(X, y)
predictions = trained.predict(X)
print(accuracy_score(y, predictions))
warnings.resetwarnings()
def test_export_to_sklearn_pipeline_with_noop_1(self):
from lale.lib.sklearn import PCA, KNeighborsClassifier
from lale.lib.lale import NoOp
from sklearn.pipeline import make_pipeline
lale_pipeline = NoOp() >> PCA(n_components=3) >> KNeighborsClassifier()
trained_lale_pipeline = lale_pipeline.fit(self.X_train, self.y_train)
sklearn_pipeline = trained_lale_pipeline.export_to_sklearn_pipeline()
self.assert_equal_predictions(sklearn_pipeline, trained_lale_pipeline)
def test_predict_proba(self):
trainable = KNeighborsClassifier()
iris = sklearn.datasets.load_iris()
trained = trainable.fit(iris.data, iris.target)
#with self.assertWarns(DeprecationWarning):
predicted = trainable.predict_proba(iris.data)
predicted = trained.predict_proba(iris.data)
def test_export_to_sklearn_pipeline_with_noop_3(self):
from lale.lib.sklearn import PCA, KNeighborsClassifier
from lale.lib.lale import NoOp
from sklearn.pipeline import make_pipeline
# This test is probably unnecessary, but doesn't harm at this point
lale_pipeline = PCA(n_components=3) >> KNeighborsClassifier() >> NoOp()
trained_lale_pipeline = lale_pipeline.fit(self.X_train, self.y_train)
sklearn_pipeline = trained_lale_pipeline.export_to_sklearn_pipeline()
def test_export_to_sklearn_pipeline1(self):
from lale.lib.sklearn import PCA
from lale.lib.sklearn import KNeighborsClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.pipeline import make_pipeline
lale_pipeline = SelectKBest(k=3) >> KNeighborsClassifier()
trained_lale_pipeline = lale_pipeline.fit(self.X_train, self.y_train)
sklearn_pipeline = trained_lale_pipeline.export_to_sklearn_pipeline()
for i, pipeline_step in enumerate(sklearn_pipeline.named_steps):
sklearn_step_params = type(sklearn_pipeline.named_steps[pipeline_step])
lale_sklearn_params = type(trained_lale_pipeline.steps()[i]._impl._wrapped_model) if hasattr(trained_lale_pipeline.steps()[i]._impl, '_wrapped_model') else type(trained_lale_pipeline.steps()[i]._impl)
self.assertEqual(sklearn_step_params, lale_sklearn_params)
self.assert_equal_predictions(sklearn_pipeline, trained_lale_pipeline)
def test_planned_pipeline_2(self) :
plan = (
( MinMaxScaler() & NoOp() ) >> ConcatFeatures() >>
( NoOp() & MinMaxScaler() ) >> ConcatFeatures() >>
( LogisticRegression | KNeighborsClassifier )
)
run_hyperopt_on_planned_pipeline(plan)
def test_with_gridsearch(self):
from lale.lib.sklearn import VotingClassifier
from lale.lib.lale import GridSearchCV
from sklearn.metrics import accuracy_score, make_scorer
clf = VotingClassifier(estimators=[('knn', KNeighborsClassifier()), ('rc', RidgeClassifier())], voting='hard')
trained = clf.auto_configure(self.X_train, self.y_train, GridSearchCV, lale_num_samples=1, lale_num_grids=1, cv=2, scoring=make_scorer(accuracy_score))