{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"from lale.lib.lale import ConcatFeatures\n",
"from lale.lib.sklearn import LogisticRegression\n",
"from lale.lib.sklearn import Nystroem\n",
"from lale.lib.sklearn import PCA"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"pca = PCA(n_components=3)\n",
"nys = Nystroem(n_components=10)\n",
"concat = ConcatFeatures()\n",
"lr = LogisticRegression(random_state=42, C=0.1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"```python\n",
"from lale.lib.sklearn import PCA\n",
"from lale.lib.sklearn import Nystroem\n",
"from lale.lib.lale import ConcatFeatures\n",
"from lale.lib.sklearn import LogisticRegression\n",
"pca = PCA(n_components=3)\n",
"nystroem = Nystroem(n_components=10)\n",
"logistic_regression = LogisticRegression(random_state=42, C=0.1)\n",
"pipeline = (pca & nystroem) >> ConcatFeatures() >> logistic_regression\n",
"```"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"trainable = (pca & nys) >> concat >> lr\n",
"# or equivalently: trainable = make_pipeline(make_union(pca, nys), lr)\n",
"trainable.visualize()\n",
"trainable.pretty_print(ipython_display=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy scores during cross validation: ['67.0%', '64.1%', '65.7%', '65.5%', '63.4%']\n"
]
}
],
"source": [
"import sklearn.datasets\n",
"from lale.helpers import cross_val_score\n",
"digits = sklearn.datasets.load_digits()\n",
"X, y = sklearn.utils.shuffle(digits.data, digits.target, random_state=42)\n",
"cv_results = cross_val_score(trainable, X, y)\n",
"cv_results = ['{0:.1%}'.format(score) for score in cv_results]\n",
"print(\"Accuracy scores during cross validation: {}\".format(cv_results))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from lale.operators import make_union\n",
"from lale.operators import make_pipeline\n",
"trainable = make_pipeline(make_union(pca, nys), lr)\n",
"trainable.visualize()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}