You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dev/_downloads/6bf99924122eedf93b886bae30f1182b/sklearn-gridsearchcv-replacement.ipynb
+39-3Lines changed: 39 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@
26
26
},
27
27
"outputs": [],
28
28
"source": [
29
-
"print(__doc__)\nimport numpy as np"
29
+
"print(__doc__)\nimport numpy as np\nnp.random.seed(123)\nimport matplotlib.pyplot as plt"
30
30
]
31
31
},
32
32
{
@@ -62,7 +62,43 @@
62
62
},
63
63
"outputs": [],
64
64
"source": [
65
-
"from skopt import BayesSearchCV\nfrom skopt.space import Real, Categorical, Integer\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.svm import LinearSVC, SVC\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import train_test_split\n\nX, y = load_digits(10, True)\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n\n# pipeline class is used as estimator to enable\n# search over different model types\npipe = Pipeline([\n ('model', SVC())\n])\n\n# single categorical value of 'model' parameter is\n# sets the model class\n# We will get ConvergenceWarnings because the problem is not well-conditioned.\n# But that's fine, this is just an example.\nlinsvc_search = {\n 'model': [LinearSVC(max_iter=1000)],\n 'model__C': (1e-6, 1e+6, 'log-uniform'),\n}\n\n# explicit dimension classes can be specified like this\nsvc_search = {\n 'model': Categorical([SVC()]),\n 'model__C': Real(1e-6, 1e+6, prior='log-uniform'),\n 'model__gamma': Real(1e-6, 1e+1, prior='log-uniform'),\n 'model__degree': Integer(1,8),\n 'model__kernel': Categorical(['linear', 'poly', 'rbf']),\n}\n\nopt = BayesSearchCV(\n pipe,\n [(svc_search, 20), (linsvc_search, 16)], # (parameter space, # of evaluations)\n cv=3\n)\n\nopt.fit(X_train, y_train)\n\nprint(\"val. score: %s\" % opt.best_score_)\nprint(\"test score: %s\" % opt.score(X_test, y_test))"
65
+
"from skopt import BayesSearchCV\nfrom skopt.space import Real, Categorical, Integer\nfrom skopt.plots import plot_objective, plot_histogram\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.svm import LinearSVC, SVC\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import train_test_split\n\nX, y = load_digits(10, True)\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n\n# pipeline class is used as estimator to enable\n# search over different model types\npipe = Pipeline([\n ('model', SVC())\n])\n\n# single categorical value of 'model' parameter is\n# sets the model class\n# We will get ConvergenceWarnings because the problem is not well-conditioned.\n# But that's fine, this is just an example.\nlinsvc_search = {\n 'model': [LinearSVC(max_iter=1000)],\n 'model__C': (1e-6, 1e+6, 'log-uniform'),\n}\n\n# explicit dimension classes can be specified like this\nsvc_search = {\n 'model': Categorical([SVC()]),\n 'model__C': Real(1e-6, 1e+6, prior='log-uniform'),\n 'model__gamma': Real(1e-6, 1e+1, prior='log-uniform'),\n 'model__degree': Integer(1,8),\n 'model__kernel': Categorical(['linear', 'poly', 'rbf']),\n}\n\nopt = BayesSearchCV(\n pipe,\n # (parameter space, # of evaluations)\n [(svc_search, 40), (linsvc_search, 16)],\n cv=3\n)\n\nopt.fit(X_train, y_train)\n\nprint(\"val. score: %s\" % opt.best_score_)\nprint(\"test score: %s\" % opt.score(X_test, y_test))\nprint(\"best params: %s\" % str(opt.best_params_))"
66
+
]
67
+
},
68
+
{
69
+
"cell_type": "markdown",
70
+
"metadata": {},
71
+
"source": [
72
+
"Partial Dependence plot of the objective function for SVC\n\n\n"
<spanclass="k">assert</span><spanclass="n">res</span><spanclass="o">.</span><spanclass="n">space</span><spanclass="o">.</span><spanclass="n">n_dims</span><spanclass="o">==</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="s2">"Space dimension must be 1"</span>
<spanclass="k">assert</span><spanclass="n">n_dims</span><spanclass="o">==</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="s2">"Space dimension must be 1"</span>
0 commit comments