We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
KerasClassifier does not return the same thing as scikit learn in the case of binary classification
For binary classification using a stock classifier from scikit, running predict_proba will return a matrix of shape (n_samples,n_classes) however KerasClassifier returns (nsamples,noutputs).

This results in the inability to use the ootb cross_val_score with 'roc_auc' due to the error:

```
site-packages\sklearn\metrics\scorer.pyc in __call__(self, clf, X, y, sample_weight)
    173 
    174                 if y_type == "binary":
--> 175                     y_pred = y_pred[:, 1]
    176                 elif isinstance(y_pred, list):
    177                     y_pred = np.vstack([p[:, -1] for p in y_pred]).T
IndexError: index 1 is out of bounds for axis 1 with size 1
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `keras/wrappers/scikit_learn.py`
Content:
```
1 from __future__ import absolute_import
2 import copy
3 import inspect
4 import types
5 
6 from ..utils.np_utils import to_categorical
7 from ..models import Sequential
8 
9 
10 class BaseWrapper(object):
11     '''Base class for the Keras scikit-learn wrapper.
12 
13     Warning: This class should not be used directly.
14     Use descendant classes instead.
15 
16     # Arguments
17         build_fn: callable function or class instance
18         sk_params: model parameters & fitting parameters
19 
20     The build_fn should construct, compile and return a Keras model, which
21     will then be used to fit/predict. One of the following
22     three values could be passed to build_fn:
23     1. A function
24     2. An instance of a class that implements the __call__ method
25     3. None. This means you implement a class that inherits from either
26     `KerasClassifier` or `KerasRegressor`. The __call__ method of the
27     present class will then be treated as the default build_fn.
28 
29     `sk_params` takes both model parameters and fitting parameters. Legal model
30     parameters are the arguments of `build_fn`. Note that like all other
31     estimators in scikit-learn, 'build_fn' should provide default values for
32     its arguments, so that you could create the estimator without passing any
33     values to `sk_params`.
34 
35     `sk_params` could also accept parameters for calling `fit`, `predict`,
36     `predict_proba`, and `score` methods (e.g., `nb_epoch`, `batch_size`).
37     fitting (predicting) parameters are selected in the following order:
38 
39     1. Values passed to the dictionary arguments of
40     `fit`, `predict`, `predict_proba`, and `score` methods
41     2. Values passed to `sk_params`
42     3. The default values of the `keras.models.Sequential`
43     `fit`, `predict`, `predict_proba` and `score` methods
44 
45     When using scikit-learn's `grid_search` API, legal tunable parameters are
46     those you could pass to `sk_params`, including fitting parameters.
47     In other words, you could use `grid_search` to search for the best
48     `batch_size` or `nb_epoch` as well as the model parameters.
49     '''
50 
51     def __init__(self, build_fn=None, **sk_params):
52         self.build_fn = build_fn
53         self.sk_params = sk_params
54         self.check_params(sk_params)
55 
56     def check_params(self, params):
57         '''Check for user typos in "params" keys to avoid
58         unwanted usage of default values
59 
60         # Arguments
61             params: dictionary
62                 The parameters to be checked
63         '''
64         legal_params_fns = [Sequential.fit, Sequential.predict,
65                             Sequential.predict_classes, Sequential.evaluate]
66         if self.build_fn is None:
67             legal_params_fns.append(self.__call__)
68         elif not isinstance(self.build_fn, types.FunctionType):
69             legal_params_fns.append(self.build_fn.__call__)
70         else:
71             legal_params_fns.append(self.build_fn)
72 
73         legal_params = []
74         for fn in legal_params_fns:
75             legal_params += inspect.getargspec(fn)[0]
76         legal_params = set(legal_params)
77 
78         for params_name in params:
79             if params_name not in legal_params:
80                 assert False, '{} is not a legal parameter'.format(params_name)
81 
82     def get_params(self, deep=True):
83         '''Get parameters for this estimator.
84 
85         # Arguments
86             deep: boolean, optional
87                 If True, will return the parameters for this estimator and
88                 contained sub-objects that are estimators.
89 
90         # Returns
91             params : dict
92                 Dictionary of parameter names mapped to their values.
93         '''
94         res = copy.deepcopy(self.sk_params)
95         res.update({'build_fn': self.build_fn})
96         return res
97 
98     def set_params(self, **params):
99         '''Set the parameters of this estimator.
100 
101         # Arguments
102         params: dict
103             Dictionary of parameter names mapped to their values.
104 
105         # Returns
106             self
107         '''
108         self.check_params(params)
109         self.sk_params.update(params)
110         return self
111 
112     def fit(self, X, y, **kwargs):
113         '''Construct a new model with build_fn and fit the model according
114         to the given training data.
115 
116         # Arguments
117             X : array-like, shape `(n_samples, n_features)`
118                 Training samples where n_samples in the number of samples
119                 and n_features is the number of features.
120             y : array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`
121                 True labels for X.
122             kwargs: dictionary arguments
123                 Legal arguments are the arguments of `Sequential.fit`
124 
125         # Returns
126             history : object
127                 details about the training history at each epoch.
128         '''
129 
130         if self.build_fn is None:
131             self.model = self.__call__(**self.filter_sk_params(self.__call__))
132         elif not isinstance(self.build_fn, types.FunctionType):
133             self.model = self.build_fn(
134                 **self.filter_sk_params(self.build_fn.__call__))
135         else:
136             self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
137 
138         loss_name = self.model.loss
139         if hasattr(loss_name, '__name__'):
140             loss_name = loss_name.__name__
141         if loss_name == 'categorical_crossentropy' and len(y.shape) != 2:
142             y = to_categorical(y)
143 
144         fit_args = copy.deepcopy(self.filter_sk_params(Sequential.fit))
145         fit_args.update(kwargs)
146 
147         history = self.model.fit(X, y, **fit_args)
148 
149         return history
150 
151     def filter_sk_params(self, fn, override={}):
152         '''Filter sk_params and return those in fn's arguments
153 
154         # Arguments
155             fn : arbitrary function
156             override: dictionary, values to override sk_params
157 
158         # Returns
159             res : dictionary dictionary containing variables
160                 in both sk_params and fn's arguments.
161         '''
162         res = {}
163         fn_args = inspect.getargspec(fn)[0]
164         for name, value in self.sk_params.items():
165             if name in fn_args:
166                 res.update({name: value})
167         res.update(override)
168         return res
169 
170 
171 class KerasClassifier(BaseWrapper):
172     '''Implementation of the scikit-learn classifier API for Keras.
173     '''
174 
175     def predict(self, X, **kwargs):
176         '''Returns the class predictions for the given test data.
177 
178         # Arguments
179             X: array-like, shape `(n_samples, n_features)`
180                 Test samples where n_samples in the number of samples
181                 and n_features is the number of features.
182             kwargs: dictionary arguments
183                 Legal arguments are the arguments of `Sequential.predict_classes`.
184 
185         # Returns
186             preds: array-like, shape `(n_samples,)`
187                 Class predictions.
188         '''
189         kwargs = self.filter_sk_params(Sequential.predict_classes, kwargs)
190         return self.model.predict_classes(X, **kwargs)
191 
192     def predict_proba(self, X, **kwargs):
193         '''Returns class probability estimates for the given test data.
194 
195         # Arguments
196             X: array-like, shape `(n_samples, n_features)`
197                 Test samples where n_samples in the number of samples
198                 and n_features is the number of features.
199             kwargs: dictionary arguments
200                 Legal arguments are the arguments of `Sequential.predict_classes`.
201 
202         # Returns
203             proba: array-like, shape `(n_samples, n_outputs)`
204                 Class probability estimates.
205         '''
206         kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)
207         return self.model.predict_proba(X, **kwargs)
208 
209     def score(self, X, y, **kwargs):
210         '''Returns the mean accuracy on the given test data and labels.
211 
212         # Arguments
213             X: array-like, shape `(n_samples, n_features)`
214                 Test samples where n_samples in the number of samples
215                 and n_features is the number of features.
216             y: array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`
217                 True labels for X.
218             kwargs: dictionary arguments
219                 Legal arguments are the arguments of `Sequential.evaluate`.
220 
221         # Returns
222             score: float
223                 Mean accuracy of predictions on X wrt. y.
224         '''
225         kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)
226         outputs = self.model.evaluate(X, y, **kwargs)
227         if type(outputs) is not list:
228             outputs = [outputs]
229         for name, output in zip(self.model.metrics_names, outputs):
230             if name == 'acc':
231                 return output
232         raise Exception('The model is not configured to compute accuracy. '
233                         'You should pass `metrics=["accuracy"]` to '
234                         'the `model.compile()` method.')
235 
236 
237 class KerasRegressor(BaseWrapper):
238     '''Implementation of the scikit-learn regressor API for Keras.
239     '''
240 
241     def predict(self, X, **kwargs):
242         '''Returns predictions for the given test data.
243 
244         # Arguments
245             X: array-like, shape `(n_samples, n_features)`
246                 Test samples where n_samples in the number of samples
247                 and n_features is the number of features.
248             kwargs: dictionary arguments
249                 Legal arguments are the arguments of `Sequential.predict`.
250         # Returns
251             preds: array-like, shape `(n_samples,)`
252                 Predictions.
253         '''
254         kwargs = self.filter_sk_params(Sequential.predict, kwargs)
255         return self.model.predict(X, **kwargs)
256 
257     def score(self, X, y, **kwargs):
258         '''Returns the mean loss on the given test data and labels.
259 
260         # Arguments
261             X: array-like, shape `(n_samples, n_features)`
262                 Test samples where n_samples in the number of samples
263                 and n_features is the number of features.
264             y: array-like, shape `(n_samples,)`
265                 True labels for X.
266             kwargs: dictionary arguments
267                 Legal arguments are the arguments of `Sequential.evaluate`.
268 
269         # Returns
270             score: float
271                 Mean accuracy of predictions on X wrt. y.
272         '''
273         kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)
274         loss = self.model.evaluate(X, y, **kwargs)
275         if type(loss) is list:
276             return loss[0]
277         return loss
278
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/keras/wrappers/scikit_learn.py b/keras/wrappers/scikit_learn.py
--- a/keras/wrappers/scikit_learn.py
+++ b/keras/wrappers/scikit_learn.py
@@ -2,6 +2,7 @@
 import copy
 import inspect
 import types
+import numpy
 
 from ..utils.np_utils import to_categorical
 from ..models import Sequential
@@ -202,9 +203,17 @@
         # Returns
             proba: array-like, shape `(n_samples, n_outputs)`
                 Class probability estimates.
+                In the case of binary classification (i.e. 1 output of 0 or 1)
+                 will return '(n_samples, 2)'
         '''
         kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)
-        return self.model.predict_proba(X, **kwargs)
+        probs = self.model.predict_proba(X, **kwargs)
+
+        # check if binary classification
+        if probs.shape[1] == 1:
+            # first column is probability of class 0 and second is of class 1
+            probs = numpy.hstack([1 - probs, probs])
+        return probs
 
     def score(self, X, y, **kwargs):
         '''Returns the mean accuracy on the given test data and labels.

{"golden_diff": "diff --git a/keras/wrappers/scikit_learn.py b/keras/wrappers/scikit_learn.py\n--- a/keras/wrappers/scikit_learn.py\n+++ b/keras/wrappers/scikit_learn.py\n@@ -2,6 +2,7 @@\n import copy\n import inspect\n import types\n+import numpy\n \n from ..utils.np_utils import to_categorical\n from ..models import Sequential\n@@ -202,9 +203,17 @@\n         # Returns\n             proba: array-like, shape `(n_samples, n_outputs)`\n                 Class probability estimates.\n+                In the case of binary classification (i.e. 1 output of 0 or 1)\n+                 will return '(n_samples, 2)'\n         '''\n         kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)\n-        return self.model.predict_proba(X, **kwargs)\n+        probs = self.model.predict_proba(X, **kwargs)\n+\n+        # check if binary classification\n+        if probs.shape[1] == 1:\n+            # first column is probability of class 0 and second is of class 1\n+            probs = numpy.hstack([1 - probs, probs])\n+        return probs\n \n     def score(self, X, y, **kwargs):\n         '''Returns the mean accuracy on the given test data and labels.\n", "issue": "KerasClassifier does not return the same thing as scikit learn in the case of binary classification\nFor binary classification using a stock classifier from scikit, running predict_proba will return a matrix of shape (n_samples,n_classes) however KerasClassifier returns (nsamples,noutputs).\n\nThis results in the inability to use the ootb cross_val_score with 'roc_auc' due to the error:\n\n```\nsite-packages\\sklearn\\metrics\\scorer.pyc in __call__(self, clf, X, y, sample_weight)\n    173 \n    174                 if y_type == \"binary\":\n--> 175                     y_pred = y_pred[:, 1]\n    176                 elif isinstance(y_pred, list):\n    177                     y_pred = np.vstack([p[:, -1] for p in y_pred]).T\nIndexError: index 1 is out of bounds for axis 1 with size 1\n```\n\n", "before_files": [{"content": "from __future__ import absolute_import\nimport copy\nimport inspect\nimport types\n\nfrom ..utils.np_utils import to_categorical\nfrom ..models import Sequential\n\n\nclass BaseWrapper(object):\n    '''Base class for the Keras scikit-learn wrapper.\n\n    Warning: This class should not be used directly.\n    Use descendant classes instead.\n\n    # Arguments\n        build_fn: callable function or class instance\n        sk_params: model parameters & fitting parameters\n\n    The build_fn should construct, compile and return a Keras model, which\n    will then be used to fit/predict. One of the following\n    three values could be passed to build_fn:\n    1. A function\n    2. An instance of a class that implements the __call__ method\n    3. None. This means you implement a class that inherits from either\n    `KerasClassifier` or `KerasRegressor`. The __call__ method of the\n    present class will then be treated as the default build_fn.\n\n    `sk_params` takes both model parameters and fitting parameters. Legal model\n    parameters are the arguments of `build_fn`. Note that like all other\n    estimators in scikit-learn, 'build_fn' should provide default values for\n    its arguments, so that you could create the estimator without passing any\n    values to `sk_params`.\n\n    `sk_params` could also accept parameters for calling `fit`, `predict`,\n    `predict_proba`, and `score` methods (e.g., `nb_epoch`, `batch_size`).\n    fitting (predicting) parameters are selected in the following order:\n\n    1. Values passed to the dictionary arguments of\n    `fit`, `predict`, `predict_proba`, and `score` methods\n    2. Values passed to `sk_params`\n    3. The default values of the `keras.models.Sequential`\n    `fit`, `predict`, `predict_proba` and `score` methods\n\n    When using scikit-learn's `grid_search` API, legal tunable parameters are\n    those you could pass to `sk_params`, including fitting parameters.\n    In other words, you could use `grid_search` to search for the best\n    `batch_size` or `nb_epoch` as well as the model parameters.\n    '''\n\n    def __init__(self, build_fn=None, **sk_params):\n        self.build_fn = build_fn\n        self.sk_params = sk_params\n        self.check_params(sk_params)\n\n    def check_params(self, params):\n        '''Check for user typos in \"params\" keys to avoid\n        unwanted usage of default values\n\n        # Arguments\n            params: dictionary\n                The parameters to be checked\n        '''\n        legal_params_fns = [Sequential.fit, Sequential.predict,\n                            Sequential.predict_classes, Sequential.evaluate]\n        if self.build_fn is None:\n            legal_params_fns.append(self.__call__)\n        elif not isinstance(self.build_fn, types.FunctionType):\n            legal_params_fns.append(self.build_fn.__call__)\n        else:\n            legal_params_fns.append(self.build_fn)\n\n        legal_params = []\n        for fn in legal_params_fns:\n            legal_params += inspect.getargspec(fn)[0]\n        legal_params = set(legal_params)\n\n        for params_name in params:\n            if params_name not in legal_params:\n                assert False, '{} is not a legal parameter'.format(params_name)\n\n    def get_params(self, deep=True):\n        '''Get parameters for this estimator.\n\n        # Arguments\n            deep: boolean, optional\n                If True, will return the parameters for this estimator and\n                contained sub-objects that are estimators.\n\n        # Returns\n            params : dict\n                Dictionary of parameter names mapped to their values.\n        '''\n        res = copy.deepcopy(self.sk_params)\n        res.update({'build_fn': self.build_fn})\n        return res\n\n    def set_params(self, **params):\n        '''Set the parameters of this estimator.\n\n        # Arguments\n        params: dict\n            Dictionary of parameter names mapped to their values.\n\n        # Returns\n            self\n        '''\n        self.check_params(params)\n        self.sk_params.update(params)\n        return self\n\n    def fit(self, X, y, **kwargs):\n        '''Construct a new model with build_fn and fit the model according\n        to the given training data.\n\n        # Arguments\n            X : array-like, shape `(n_samples, n_features)`\n                Training samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y : array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.fit`\n\n        # Returns\n            history : object\n                details about the training history at each epoch.\n        '''\n\n        if self.build_fn is None:\n            self.model = self.__call__(**self.filter_sk_params(self.__call__))\n        elif not isinstance(self.build_fn, types.FunctionType):\n            self.model = self.build_fn(\n                **self.filter_sk_params(self.build_fn.__call__))\n        else:\n            self.model = self.build_fn(**self.filter_sk_params(self.build_fn))\n\n        loss_name = self.model.loss\n        if hasattr(loss_name, '__name__'):\n            loss_name = loss_name.__name__\n        if loss_name == 'categorical_crossentropy' and len(y.shape) != 2:\n            y = to_categorical(y)\n\n        fit_args = copy.deepcopy(self.filter_sk_params(Sequential.fit))\n        fit_args.update(kwargs)\n\n        history = self.model.fit(X, y, **fit_args)\n\n        return history\n\n    def filter_sk_params(self, fn, override={}):\n        '''Filter sk_params and return those in fn's arguments\n\n        # Arguments\n            fn : arbitrary function\n            override: dictionary, values to override sk_params\n\n        # Returns\n            res : dictionary dictionary containing variables\n                in both sk_params and fn's arguments.\n        '''\n        res = {}\n        fn_args = inspect.getargspec(fn)[0]\n        for name, value in self.sk_params.items():\n            if name in fn_args:\n                res.update({name: value})\n        res.update(override)\n        return res\n\n\nclass KerasClassifier(BaseWrapper):\n    '''Implementation of the scikit-learn classifier API for Keras.\n    '''\n\n    def predict(self, X, **kwargs):\n        '''Returns the class predictions for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict_classes`.\n\n        # Returns\n            preds: array-like, shape `(n_samples,)`\n                Class predictions.\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict_classes, kwargs)\n        return self.model.predict_classes(X, **kwargs)\n\n    def predict_proba(self, X, **kwargs):\n        '''Returns class probability estimates for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict_classes`.\n\n        # Returns\n            proba: array-like, shape `(n_samples, n_outputs)`\n                Class probability estimates.\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)\n        return self.model.predict_proba(X, **kwargs)\n\n    def score(self, X, y, **kwargs):\n        '''Returns the mean accuracy on the given test data and labels.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y: array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.evaluate`.\n\n        # Returns\n            score: float\n                Mean accuracy of predictions on X wrt. y.\n        '''\n        kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)\n        outputs = self.model.evaluate(X, y, **kwargs)\n        if type(outputs) is not list:\n            outputs = [outputs]\n        for name, output in zip(self.model.metrics_names, outputs):\n            if name == 'acc':\n                return output\n        raise Exception('The model is not configured to compute accuracy. '\n                        'You should pass `metrics=[\"accuracy\"]` to '\n                        'the `model.compile()` method.')\n\n\nclass KerasRegressor(BaseWrapper):\n    '''Implementation of the scikit-learn regressor API for Keras.\n    '''\n\n    def predict(self, X, **kwargs):\n        '''Returns predictions for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict`.\n        # Returns\n            preds: array-like, shape `(n_samples,)`\n                Predictions.\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict, kwargs)\n        return self.model.predict(X, **kwargs)\n\n    def score(self, X, y, **kwargs):\n        '''Returns the mean loss on the given test data and labels.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y: array-like, shape `(n_samples,)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.evaluate`.\n\n        # Returns\n            score: float\n                Mean accuracy of predictions on X wrt. y.\n        '''\n        kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)\n        loss = self.model.evaluate(X, y, **kwargs)\n        if type(loss) is list:\n            return loss[0]\n        return loss\n", "path": "keras/wrappers/scikit_learn.py"}], "after_files": [{"content": "from __future__ import absolute_import\nimport copy\nimport inspect\nimport types\nimport numpy\n\nfrom ..utils.np_utils import to_categorical\nfrom ..models import Sequential\n\n\nclass BaseWrapper(object):\n    '''Base class for the Keras scikit-learn wrapper.\n\n    Warning: This class should not be used directly.\n    Use descendant classes instead.\n\n    # Arguments\n        build_fn: callable function or class instance\n        sk_params: model parameters & fitting parameters\n\n    The build_fn should construct, compile and return a Keras model, which\n    will then be used to fit/predict. One of the following\n    three values could be passed to build_fn:\n    1. A function\n    2. An instance of a class that implements the __call__ method\n    3. None. This means you implement a class that inherits from either\n    `KerasClassifier` or `KerasRegressor`. The __call__ method of the\n    present class will then be treated as the default build_fn.\n\n    `sk_params` takes both model parameters and fitting parameters. Legal model\n    parameters are the arguments of `build_fn`. Note that like all other\n    estimators in scikit-learn, 'build_fn' should provide default values for\n    its arguments, so that you could create the estimator without passing any\n    values to `sk_params`.\n\n    `sk_params` could also accept parameters for calling `fit`, `predict`,\n    `predict_proba`, and `score` methods (e.g., `nb_epoch`, `batch_size`).\n    fitting (predicting) parameters are selected in the following order:\n\n    1. Values passed to the dictionary arguments of\n    `fit`, `predict`, `predict_proba`, and `score` methods\n    2. Values passed to `sk_params`\n    3. The default values of the `keras.models.Sequential`\n    `fit`, `predict`, `predict_proba` and `score` methods\n\n    When using scikit-learn's `grid_search` API, legal tunable parameters are\n    those you could pass to `sk_params`, including fitting parameters.\n    In other words, you could use `grid_search` to search for the best\n    `batch_size` or `nb_epoch` as well as the model parameters.\n    '''\n\n    def __init__(self, build_fn=None, **sk_params):\n        self.build_fn = build_fn\n        self.sk_params = sk_params\n        self.check_params(sk_params)\n\n    def check_params(self, params):\n        '''Check for user typos in \"params\" keys to avoid\n        unwanted usage of default values\n\n        # Arguments\n            params: dictionary\n                The parameters to be checked\n        '''\n        legal_params_fns = [Sequential.fit, Sequential.predict,\n                            Sequential.predict_classes, Sequential.evaluate]\n        if self.build_fn is None:\n            legal_params_fns.append(self.__call__)\n        elif not isinstance(self.build_fn, types.FunctionType):\n            legal_params_fns.append(self.build_fn.__call__)\n        else:\n            legal_params_fns.append(self.build_fn)\n\n        legal_params = []\n        for fn in legal_params_fns:\n            legal_params += inspect.getargspec(fn)[0]\n        legal_params = set(legal_params)\n\n        for params_name in params:\n            if params_name not in legal_params:\n                assert False, '{} is not a legal parameter'.format(params_name)\n\n    def get_params(self, deep=True):\n        '''Get parameters for this estimator.\n\n        # Arguments\n            deep: boolean, optional\n                If True, will return the parameters for this estimator and\n                contained sub-objects that are estimators.\n\n        # Returns\n            params : dict\n                Dictionary of parameter names mapped to their values.\n        '''\n        res = copy.deepcopy(self.sk_params)\n        res.update({'build_fn': self.build_fn})\n        return res\n\n    def set_params(self, **params):\n        '''Set the parameters of this estimator.\n\n        # Arguments\n        params: dict\n            Dictionary of parameter names mapped to their values.\n\n        # Returns\n            self\n        '''\n        self.check_params(params)\n        self.sk_params.update(params)\n        return self\n\n    def fit(self, X, y, **kwargs):\n        '''Construct a new model with build_fn and fit the model according\n        to the given training data.\n\n        # Arguments\n            X : array-like, shape `(n_samples, n_features)`\n                Training samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y : array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.fit`\n\n        # Returns\n            history : object\n                details about the training history at each epoch.\n        '''\n\n        if self.build_fn is None:\n            self.model = self.__call__(**self.filter_sk_params(self.__call__))\n        elif not isinstance(self.build_fn, types.FunctionType):\n            self.model = self.build_fn(\n                **self.filter_sk_params(self.build_fn.__call__))\n        else:\n            self.model = self.build_fn(**self.filter_sk_params(self.build_fn))\n\n        loss_name = self.model.loss\n        if hasattr(loss_name, '__name__'):\n            loss_name = loss_name.__name__\n        if loss_name == 'categorical_crossentropy' and len(y.shape) != 2:\n            y = to_categorical(y)\n\n        fit_args = copy.deepcopy(self.filter_sk_params(Sequential.fit))\n        fit_args.update(kwargs)\n\n        history = self.model.fit(X, y, **fit_args)\n\n        return history\n\n    def filter_sk_params(self, fn, override={}):\n        '''Filter sk_params and return those in fn's arguments\n\n        # Arguments\n            fn : arbitrary function\n            override: dictionary, values to override sk_params\n\n        # Returns\n            res : dictionary dictionary containing variables\n                in both sk_params and fn's arguments.\n        '''\n        res = {}\n        fn_args = inspect.getargspec(fn)[0]\n        for name, value in self.sk_params.items():\n            if name in fn_args:\n                res.update({name: value})\n        res.update(override)\n        return res\n\n\nclass KerasClassifier(BaseWrapper):\n    '''Implementation of the scikit-learn classifier API for Keras.\n    '''\n\n    def predict(self, X, **kwargs):\n        '''Returns the class predictions for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict_classes`.\n\n        # Returns\n            preds: array-like, shape `(n_samples,)`\n                Class predictions.\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict_classes, kwargs)\n        return self.model.predict_classes(X, **kwargs)\n\n    def predict_proba(self, X, **kwargs):\n        '''Returns class probability estimates for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict_classes`.\n\n        # Returns\n            proba: array-like, shape `(n_samples, n_outputs)`\n                Class probability estimates.\n                In the case of binary classification (i.e. 1 output of 0 or 1)\n                 will return '(n_samples, 2)'\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)\n        probs = self.model.predict_proba(X, **kwargs)\n\n        # check if binary classification\n        if probs.shape[1] == 1:\n            # first column is probability of class 0 and second is of class 1\n            probs = numpy.hstack([1 - probs, probs])\n        return probs\n\n    def score(self, X, y, **kwargs):\n        '''Returns the mean accuracy on the given test data and labels.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y: array-like, shape `(n_samples,)` or `(n_samples, n_outputs)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.evaluate`.\n\n        # Returns\n            score: float\n                Mean accuracy of predictions on X wrt. y.\n        '''\n        kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)\n        outputs = self.model.evaluate(X, y, **kwargs)\n        if type(outputs) is not list:\n            outputs = [outputs]\n        for name, output in zip(self.model.metrics_names, outputs):\n            if name == 'acc':\n                return output\n        raise Exception('The model is not configured to compute accuracy. '\n                        'You should pass `metrics=[\"accuracy\"]` to '\n                        'the `model.compile()` method.')\n\n\nclass KerasRegressor(BaseWrapper):\n    '''Implementation of the scikit-learn regressor API for Keras.\n    '''\n\n    def predict(self, X, **kwargs):\n        '''Returns predictions for the given test data.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.predict`.\n        # Returns\n            preds: array-like, shape `(n_samples,)`\n                Predictions.\n        '''\n        kwargs = self.filter_sk_params(Sequential.predict, kwargs)\n        return self.model.predict(X, **kwargs)\n\n    def score(self, X, y, **kwargs):\n        '''Returns the mean loss on the given test data and labels.\n\n        # Arguments\n            X: array-like, shape `(n_samples, n_features)`\n                Test samples where n_samples in the number of samples\n                and n_features is the number of features.\n            y: array-like, shape `(n_samples,)`\n                True labels for X.\n            kwargs: dictionary arguments\n                Legal arguments are the arguments of `Sequential.evaluate`.\n\n        # Returns\n            score: float\n                Mean accuracy of predictions on X wrt. y.\n        '''\n        kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)\n        loss = self.model.evaluate(X, y, **kwargs)\n        if type(loss) is list:\n            return loss[0]\n        return loss\n", "path": "keras/wrappers/scikit_learn.py"}]}