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_two_estimators_predict1(self):
pipeline = StandardScaler() >> ( PCA() & Nystroem() & PassiveAggressiveClassifier() )>>ConcatFeatures() >> NoOp() >> PassiveAggressiveClassifier()
trained = pipeline.fit(self.X_train, self.y_train)
trained.predict(self.X_test)
def test_two_estimators_predict_proba(self):
def test_remove_last3(self):
pipeline = StandardScaler() >> ( PCA() & Nystroem() & PassiveAggressiveClassifier() )>>ConcatFeatures() >> NoOp() >> PassiveAggressiveClassifier()
pipeline.remove_last().freeze_trainable()
def test_transform_schema_Concat_irisArr(self):
from lale.datasets.data_schemas import to_schema
data_X, data_y = self._irisArr['X'], self._irisArr['y']
s_in_X, s_in_y = to_schema(data_X), to_schema(data_y)
def check(s_actual, n_expected, s_expected):
assert s_actual['items']['minItems'] == n_expected, str(s_actual)
assert s_actual['items']['maxItems'] == n_expected, str(s_actual)
assert s_actual['items']['items'] == s_expected, str(s_actual)
s_out_X = ConcatFeatures.transform_schema({'items': [s_in_X]})
check(s_out_X, 4, {'type': 'number'})
s_out_y = ConcatFeatures.transform_schema({'items': [s_in_y]})
check(s_out_y, 1, {'type': 'integer'})
s_out_XX = ConcatFeatures.transform_schema({'items': [s_in_X, s_in_X]})
check(s_out_XX, 8, {'type': 'number'})
s_out_yy = ConcatFeatures.transform_schema({'items': [s_in_y, s_in_y]})
check(s_out_yy, 2, {'type': 'integer'})
s_out_Xy = ConcatFeatures.transform_schema({'items': [s_in_X, s_in_y]})
check(s_out_Xy, 5, {'type': 'number'})
s_out_XXX = ConcatFeatures.transform_schema({
'items': [s_in_X, s_in_X, s_in_X]})
check(s_out_XXX, 12, {'type': 'number'})
def test_import_as_2(self):
from lale.lib.sklearn import MinMaxScaler as Scaler
from lale.lib.lale import NoOp
from lale.lib.sklearn import PCA
from lale.lib.sklearn import Nystroem
from lale.lib.lale import ConcatFeatures as Concat
from lale.lib.sklearn import KNeighborsClassifier as KNN
from lale.lib.sklearn import LogisticRegression as LR
pca = PCA(copy=False)
lr = LR(solver='saga', C=0.9)
pipeline = (Scaler | NoOp) >> (pca & Nystroem) >> Concat >> (KNN | lr)
expected = \
"""from lale.lib.sklearn import MinMaxScaler as Scaler
from lale.lib.lale import NoOp
from lale.lib.sklearn import PCA
from lale.lib.sklearn import Nystroem
from lale.lib.lale import ConcatFeatures as Concat
from lale.lib.sklearn import KNeighborsClassifier as KNN
from lale.lib.sklearn import LogisticRegression as LR
import lale
lale.wrap_imported_operators()
pca = PCA(copy=False)
lr = LR(solver='saga', C=0.9)
pipeline = (Scaler | NoOp) >> (pca & Nystroem) >> Concat >> (KNN | lr)"""
self._roundtrip(expected, lale.pretty_print.to_string(pipeline))
def test_planned_pipeline_3(self) :
plan = (
( MinMaxScaler() & NoOp() ) >> ConcatFeatures() >>
( StandardScaler & ( NoOp() | MinMaxScaler() ) ) >> ConcatFeatures() >>
( LogisticRegression | KNeighborsClassifier )
)
run_hyperopt_on_planned_pipeline(plan)
def test_remove_last5(self):
pipeline = StandardScaler() >> ( PCA() & Nystroem() & PassiveAggressiveClassifier() )>>ConcatFeatures() >> NoOp() >> PassiveAggressiveClassifier()
pipeline.remove_last(inplace=True).freeze_trainable()
def test_get_param_ranges(self):
for op in [ConcatFeatures, KNeighborsClassifier, LogisticRegression,
MLPClassifier, Nystroem, OneHotEncoder, PCA]:
self.validate_get_param_ranges(op)
'post': []},
'properties': {
'hyperparams': _hyperparams_schema,
'input_fit': _input_fit_schema,
'input_predict': _input_predict_schema,
'output': _output_predict_schema}}
HyperoptRegressor = lale.operators.make_operator(HyperoptRegressorImpl, _combined_schemas)
if __name__ == '__main__':
from lale.lib.lale import ConcatFeatures
from lale.lib.sklearn import Nystroem, PCA, RandomForestRegressor
from sklearn.metrics import r2_score
pca = PCA(n_components=3)
nys = Nystroem(n_components=3)
concat = ConcatFeatures()
rf = RandomForestRegressor()
trainable = (pca & nys) >> concat >> rf
#trainable = nys >>rf
import sklearn.datasets
from lale.helpers import cross_val_score
diabetes = sklearn.datasets.load_diabetes()
X, y = sklearn.utils.shuffle(diabetes.data, diabetes.target, random_state=42)
hp_n = HyperoptRegressor(estimator=trainable, max_evals=20)
hp_n_trained = hp_n.fit(X, y)
predictions = hp_n_trained.predict(X)
mse = r2_score(y, [round(pred) for pred in predictions])
print(mse)
'hyperparams': _hyperparams_schema,
'input_fit': _input_fit_schema,
'input_predict': _input_predict_schema,
'output_predict': _output_predict_schema}}
lale.docstrings.set_docstrings(HyperoptImpl, _combined_schemas)
Hyperopt = lale.operators.make_operator(HyperoptImpl, _combined_schemas)
if __name__ == '__main__':
from lale.lib.lale import ConcatFeatures
from lale.lib.sklearn import Nystroem
from lale.lib.sklearn import PCA
pca = PCA(n_components=10)
nys = Nystroem(n_components=10)
concat = ConcatFeatures()
lr = LogisticRegression(random_state=42, C=0.1)
trainable = (pca & nys) >> concat >> lr
import sklearn.datasets
from lale.helpers import cross_val_score
digits = sklearn.datasets.load_iris()
X, y = sklearn.utils.shuffle(digits.data, digits.target, random_state=42)
hp_n = Hyperopt(estimator=trainable, max_evals=2)
hp_n_trained = hp_n.fit(X, y)
predictions = hp_n_trained.predict(X)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y, [round(pred) for pred in predictions])
print(accuracy)