<jupyter_start><jupyter_text>Lale: Type-Driven Auto-ML with Scikit-Learn https://github.com/ibm/lale Example Dataset<jupyter_code>!pip install 'liac-arff>=2.4.0'
import lale.datasets.openml
import pandas as pd
(train_X, train_y), (test_X, test_y) = lale.datasets.openml.fetch(
    'credit-g', 'classification', preprocess=False)
print(f'train_X.shape {train_X.shape}')
pd.concat([train_y.tail(), train_X.tail()], axis=1)<jupyter_output>train_X.shape (670, 20)<jupyter_text>Algorithm Selection and Hyperparameter Tuning<jupyter_code>from sklearn.preprocessing import Normalizer as Norm
from sklearn.preprocessing import OneHotEncoder as OneHot
from sklearn.linear_model import LogisticRegression as LR
from xgboost import XGBClassifier as XGBoost
from sklearn.svm import LinearSVC
from lale.operators import make_pipeline, make_union
from lale.lib.lale import Project, ConcatFeatures, NoOp
lale.wrap_imported_operators()
project_nums = Project(columns={'type': 'number'})
project_cats = Project(columns={'type': 'string'})
planned_pipeline = (
       (project_nums >> (Norm | NoOp) & project_cats >> OneHot)
    >> ConcatFeatures
    >> (LR | LinearSVC(dual=False)| XGBoost))
planned_pipeline.visualize()
import sklearn.metrics
from lale.lib.lale import Hyperopt
auto_optimizer = Hyperopt(estimator=planned_pipeline, cv=3, max_evals=5)
auto_trained = auto_optimizer.fit(train_X, train_y)
auto_y = auto_trained.predict(test_X)
print(f'accuracy {sklearn.metrics.accuracy_score(test_y, auto_y):.1%}')<jupyter_output>100%|█████████| 5/5 [01:08<00:00, 13.74s/trial, best loss: -0.7507273649370062]
accuracy 72.1%<jupyter_text>Displaying Automation Results<jupyter_code>best_pipeline = auto_trained.get_pipeline()
best_pipeline.visualize()
from lale.pretty_print import ipython_display
ipython_display(best_pipeline, show_imports=False)<jupyter_output><empty_output><jupyter_text>JSON Schemashttps://json-schema.org/<jupyter_code>ipython_display(XGBoost.hyperparam_schema('n_estimators'))
ipython_display(XGBoost.hyperparam_schema('booster'))
import jsonschema
import sys
try:
    XGBoost(n_estimators=0.5, booster='gbtree')
except jsonschema.ValidationError as e:
    print(e.message, file=sys.stderr)<jupyter_output>Invalid configuration for XGBoost(n_estimators=0.5, booster='gbtree') due to invalid value n_estimators=0.5.
Schema of argument n_estimators: {
    "description": "Number of trees to fit.",
    "type": "integer",
    "default": 1000,
    "minimumForOptimizer": 500,
    "maximumForOptimizer": 1500,
}
Value: 0.5<jupyter_text>Customizing Schemas<jupyter_code>import lale.schemas as schemas
Grove = XGBoost.customize_schema(
    n_estimators=schemas.Int(minimum=2, maximum=10),
    booster=schemas.Enum(['gbtree'], default='gbtree'))
grove_planned = ( Project(columns={'type': 'number'}) >> Norm
                & Project(columns={'type': 'string'}) >> OneHot
                ) >> ConcatFeatures >> Grove
grove_optimizer = Hyperopt(estimator=grove_planned, cv=3, max_evals=10)
grove_trained = grove_optimizer.fit(train_X, train_y)
grove_y = grove_trained.predict(test_X)
print(f'accuracy {sklearn.metrics.accuracy_score(test_y, grove_y):.1%}')
grove_best = grove_trained.get_pipeline()
ipython_display(grove_best, show_imports=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup