Repository: keras-team/keras

Issue:
Scikit Learn wrapper predict() inappropriately squashes size-1 batch dimension
Please make sure that the boxes below are checked before you submit your issue.
If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue.

Thank you!

- [X] Check that you are up-to-date with the master branch of Keras. You can update with:
`pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps`
Using Keras version 2.2.4

- [X] Check that your version of TensorFlow is up-to-date. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).
Using Tensorflow version 1.12.0

- [X] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

If predict() is called on input with shape `(1, num_features)`, then the output is a 0-dimensional array instead of a 1-dimensional array with 1 element.

```python
import keras
import keras.wrappers.scikit_learn
import numpy as np
import sklearn.linear_model
import sklearn.metrics

def build_net():
    model = keras.models.Sequential([keras.layers.Dense(units=1, input_dim=2)])
    model.compile(loss=keras.losses.mean_squared_error, optimizer="sgd")
    return model

regressor = keras.wrappers.scikit_learn.KerasRegressor(build_fn=build_net)
# Works with the sklearn regressors
# regressor = sklearn.linear_model.LinearRegression()
X = np.zeros((1, 2))
Y = np.zeros((1,))
regressor.fit(X, Y)
Y_pred = regressor.predict(X)
print(Y_pred.shape)  # Is (), should be (1,)
# As a result, this fails with an exception
# TypeError: Singleton array array(0., dtype=float32) cannot be considered a valid collection.
print(sklearn.metrics.mean_squared_error(y_true=Y, y_pred=Y_pred))
```


Discussion:

Resolving patch:
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
@@ -320,7 +320,7 @@ def predict(self, x, **kwargs):
                 Predictions.
         """
         kwargs = self.filter_sk_params(Sequential.predict, kwargs)
-        return np.squeeze(self.model.predict(x, **kwargs))
+        return np.squeeze(self.model.predict(x, **kwargs), axis=-1)
 
     def score(self, x, y, **kwargs):
         """Returns the mean loss on the given test data and labels.


Test patch:
diff --git a/tests/keras/wrappers/scikit_learn_test.py b/tests/keras/wrappers/scikit_learn_test.py
--- a/tests/keras/wrappers/scikit_learn_test.py
+++ b/tests/keras/wrappers/scikit_learn_test.py
@@ -167,6 +167,24 @@ def assert_regression_works(reg):
     assert preds.shape == (num_test, )
 
 
+def test_regression_predict_shape_correct_num_test_0():
+    assert_regression_predict_shape_correct(num_test=0)
+
+
+def test_regression_predict_shape_correct_num_test_1():
+    assert_regression_predict_shape_correct(num_test=1)
+
+
+def assert_regression_predict_shape_correct(num_test):
+    reg = KerasRegressor(
+        build_fn=build_fn_reg, hidden_dims=hidden_dims,
+        batch_size=batch_size, epochs=epochs)
+    reg.fit(X_train, y_train, batch_size=batch_size, epochs=epochs)
+
+    preds = reg.predict(X_test[:num_test], batch_size=batch_size)
+    assert preds.shape == (num_test, )
+
+
 if __name__ == '__main__':
     pytest.main([__file__])