internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: scipy/scipy

Issue:
csc matrix .mean returns single element matrix rather than scalar
``` python
import numpy as np

import scipy
from scipy.sparse.csc import csc_matrix

matrix = csc_matrix(np.ones((5, 5)))
mean = matrix.mean()
print('scipy version:', scipy.__version__)
print(mean, type(mean))
```

Output for scipy 0.17.0:

```
scipy version: 0.17.0
1.0 <class 'numpy.float64'>
```

Output for scipy master:

```
scipy version: 0.18.0.dev0+3ca41c4
[[ 1.]] <class 'numpy.matrixlib.defmatrix.matrix'>
```

This causes the scikit-learn tests using numpy and scipy master to fail. See this Travis [log](https://travis-ci.org/scikit-learn/scikit-learn/jobs/114659796) for example.



Discussion:
Very likely related to https://github.com/scipy/scipy/pull/5907.


Resolving patch:
diff --git a/scipy/sparse/base.py b/scipy/sparse/base.py
--- a/scipy/sparse/base.py
+++ b/scipy/sparse/base.py
@@ -792,9 +792,9 @@ def mean(self, axis=None):
             res_dtype = self.dtype
 
         if axis is None:
-            m, n = self.shape
-            return self.astype(res_dtype).sum() / np.matrix(m * n,
-                                                            dtype=res_dtype)
+            mean = self.astype(res_dtype).sum()
+            mean /= np.array(self.shape[0] * self.shape[1], dtype=res_dtype)
+            return mean
 
         if axis < 0:
             axis += 2


Test patch:
diff --git a/scipy/sparse/tests/test_base.py b/scipy/sparse/tests/test_base.py
--- a/scipy/sparse/tests/test_base.py
+++ b/scipy/sparse/tests/test_base.py
@@ -760,6 +760,7 @@ def check(dtype, j):
             datsp = self.spmatrix(dat, dtype=dtype)
             assert_array_almost_equal(dat.sum(), datsp.sum())
             assert_equal(dat.sum().dtype, datsp.sum().dtype)
+            assert_(np.isscalar(datsp.sum(axis=None)))
             assert_array_almost_equal(dat.sum(axis=None), datsp.sum(axis=None))
             assert_equal(dat.sum(axis=None).dtype, datsp.sum(axis=None).dtype)
             assert_array_almost_equal(dat.sum(axis=0), datsp.sum(axis=0))
@@ -784,6 +785,7 @@ def check(dtype):
 
             assert_array_almost_equal(dat.mean(), datsp.mean())
             assert_equal(dat.mean().dtype, datsp.mean().dtype)
+            assert_(np.isscalar(datsp.mean(axis=None)))
             assert_array_almost_equal(dat.mean(axis=None), datsp.mean(axis=None))
             assert_equal(dat.mean(axis=None).dtype, datsp.mean(axis=None).dtype)
             assert_array_almost_equal(dat.mean(axis=0), datsp.mean(axis=0))
Repository: scipy/scipy

Issue:
Bug in computation of dtype for sum or product linear operators
The routine _get_dtype in scipy.sparse.linalg.interface has a mutable keyword argument to which in the routine the dtypes of the input operators are appended. This has the effect that dtypes are not always correctly computed. For example, if first a sum of two complex operators is computed, and then the sum of two real operators, the sum of the real operators also gets the type 'complex128' since this is still stored in the mutable dtypes list from the first addition.

Possible solution: Make the dtypes keyword argument non-optional, and modify the calls to _get_dtype in the other routines of the module correspondingly.



Discussion:
A short reproducer would be helpful. A PR with a flx and a test would be even better.

Here is an example that reproduces the error:

```
from scipy.sparse.linalg import LinearOperator
from scipy.sparse.linalg import aslinearoperator
import numpy as np

mat_complex = np.random.rand(2,2) + 1j * np.random.rand(2,2)
mat_real = np.random.rand(2,2)

complex_operator = aslinearoperator(mat_complex)
real_operator = aslinearoperator(mat_real)

sum_complex = complex_operator + complex_operator
sum_real = real_operator + real_operator

print(sum_complex.dtype)
print(sum_real.dtype)
```

The output is:

```
complex128
complex128
```

If you take out the complex sum then the real sum gets the correct type.

In what format do you require a patch and a fix?


Resolving patch:
diff --git a/scipy/sparse/linalg/interface.py b/scipy/sparse/linalg/interface.py
--- a/scipy/sparse/linalg/interface.py
+++ b/scipy/sparse/linalg/interface.py
@@ -483,7 +483,9 @@ def _adjoint(self):
                                      dtype=self.dtype)
 
 
-def _get_dtype(operators, dtypes=[]):
+def _get_dtype(operators, dtypes=None):
+    if dtypes is None:
+        dtypes = []
     for obj in operators:
         if obj is not None and hasattr(obj, 'dtype'):
             dtypes.append(obj.dtype)


Test patch:
diff --git a/scipy/sparse/linalg/tests/test_interface.py b/scipy/sparse/linalg/tests/test_interface.py
--- a/scipy/sparse/linalg/tests/test_interface.py
+++ b/scipy/sparse/linalg/tests/test_interface.py
@@ -326,3 +326,18 @@ def _matmat(self, x):
 
     mm = MatmatOnly(np.random.randn(5, 3))
     assert_equal(mm.matvec(np.random.randn(3)).shape, (5,))
+
+def test_dtypes_of_operator_sum():
+    # gh-6078
+
+    mat_complex = np.random.rand(2,2) + 1j * np.random.rand(2,2)
+    mat_real = np.random.rand(2,2)
+
+    complex_operator = interface.aslinearoperator(mat_complex)
+    real_operator = interface.aslinearoperator(mat_real)
+
+    sum_complex = complex_operator + complex_operator
+    sum_real = real_operator + real_operator
+
+    assert_equal(sum_real.dtype, np.float64)
+    assert_equal(sum_complex.dtype, np.complex128)
Repository: scipy/scipy

Issue:
Doc for optimize.linprog should say minimize instead of maximize
As of today, the documentation [page](http://docs.scipy.org/doc/scipy/reference/optimize.linprog-simplex.html) for `optimize.linprog` says that the function maximizes `cT * x`, but I believe it minimizes it (as you can also tell from the example at the bottom of the page).

Indeed, calling `linprog([1], [[1]], [1])` returns an optimal solution with objective value 0 instead of 1. Calling `linprog([1], [[-1]], [-1])` returns an optimal solution with objective value 1.

The documentation is generated from [here](https://github.com/scipy/scipy/blob/e82931d48b5fa1f79917b252b7cf4ba7226d1dd8/scipy/optimize/_linprog.py#L398).

Furthermore, the `fun` field in the return value for `linprog` is not documented properly -- it is only shown in the example.



Discussion:

Resolving patch:
diff --git a/scipy/optimize/_linprog.py b/scipy/optimize/_linprog.py
--- a/scipy/optimize/_linprog.py
+++ b/scipy/optimize/_linprog.py
@@ -395,7 +395,7 @@ def _linprog_simplex(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None,
     Solve the following linear programming problem via a two-phase
     simplex algorithm.
 
-    maximize:     c^T * x
+    minimize:     c^T * x
 
     subject to:   A_ub * x <= b_ub
                   A_eq * x == b_eq
@@ -403,7 +403,7 @@ def _linprog_simplex(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None,
     Parameters
     ----------
     c : array_like
-        Coefficients of the linear objective function to be maximized.
+        Coefficients of the linear objective function to be minimized.
     A_ub : array_like
         2-D array which, when matrix-multiplied by x, gives the values of the
         upper-bound inequality constraints at x.
@@ -461,6 +461,8 @@ def _linprog_simplex(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None,
         x : ndarray
             The independent variable vector which optimizes the linear
             programming problem.
+        fun : float
+            Value of the objective function.
         slack : ndarray
             The values of the slack variables.  Each slack variable corresponds
             to an inequality constraint.  If the slack is zero, then the


Test patch:
Repository: scipy/scipy

Issue:
Error in dot method of sparse COO array, when used with numpy arrays with 0 columns.
If A is sparse array in COO format of shape (M,N) and B is a numpy array of shape (N,0), then A.dot(B) throws an IndexError in dot. 

One would expect it to return an empty array of shape (M,0). This is for instance the case if A is a sparse matrix in CSR format or a numpy array.

To reproduce:

```
import scipy.sparse as sp
import numpy as np
sp.csr_matrix(np.eye(3,2)).dot(np.eye(2,0)).shape  # Works, (3,0)
sp.coo_matrix(np.eye(3,2)).dot(np.eye(2,0))  # Throws error
```



Discussion:
Yep, that's a bug. I'll open a PR with the fix soon.


Resolving patch:
diff --git a/scipy/sparse/coo.py b/scipy/sparse/coo.py
--- a/scipy/sparse/coo.py
+++ b/scipy/sparse/coo.py
@@ -496,7 +496,11 @@ def _mul_vector(self, other):
         return result
 
     def _mul_multivector(self, other):
-        return np.hstack([self._mul_vector(col).reshape(-1,1) for col in other.T])
+        result = np.zeros((other.shape[1], self.shape[0]),
+                          dtype=upcast_char(self.dtype.char, other.dtype.char))
+        for i, col in enumerate(other.T):
+            coo_matvec(self.nnz, self.row, self.col, self.data, col, result[i])
+        return result.T.view(type=type(other))
 
 
 def isspmatrix_coo(x):


Test patch:
diff --git a/scipy/sparse/tests/test_base.py b/scipy/sparse/tests/test_base.py
--- a/scipy/sparse/tests/test_base.py
+++ b/scipy/sparse/tests/test_base.py
@@ -1354,13 +1354,14 @@ def test_rmatvec(self):
         assert_array_almost_equal(row*M, row*M.todense())
 
     def test_small_multiplication(self):
-        # test that A*x works for x with shape () (1,) and (1,1)
+        # test that A*x works for x with shape () (1,) (1,1) and (1,0)
         A = self.spmatrix([[1],[2],[3]])
 
         assert_(isspmatrix(A * array(1)))
         assert_equal((A * array(1)).todense(), [[1],[2],[3]])
         assert_equal(A * array([1]), array([1,2,3]))
         assert_equal(A * array([[1]]), array([[1],[2],[3]]))
+        assert_equal(A * np.ones((1,0)), np.ones((3,0)))
 
     def test_binop_custom_type(self):
         # Non-regression test: previously, binary operations would raise
Repository: scipy/scipy

Issue:
dtype=bool sparse matrices don't implement __neg__ similarly to np.ndarray
As noted in gh-5826:

Numpy has a convention that `-np.array([True]) == np.array([False])`, but this is not followed by scipy's sparse matrices:

```
import scipy.sparse
x = scipy.sparse.csr_matrix([[True, False],[False, True]], dtype=bool)
(-x).todense()
# Out[4]:
# matrix([[False, False],
#        [False, False]], dtype=bool)
-x.todense()
# matrix([[False,  True],
#        [ True, False]], dtype=bool)
```

The missing part seems to be a proper implementation of `__neg__`, and maybe also dealing with implicit zeros in `__sub__`.



Discussion:

Resolving patch:
diff --git a/scipy/sparse/data.py b/scipy/sparse/data.py
--- a/scipy/sparse/data.py
+++ b/scipy/sparse/data.py
@@ -44,6 +44,9 @@ def _imag(self):
         return self._with_data(self.data.imag)
 
     def __neg__(self):
+        if self.dtype.kind == 'b':
+            raise NotImplementedError('negating a sparse boolean '
+                                      'matrix is not supported')
         return self._with_data(-self.data)
 
     def __imul__(self, other):  # self *= other
diff --git a/scipy/sparse/dok.py b/scipy/sparse/dok.py
--- a/scipy/sparse/dok.py
+++ b/scipy/sparse/dok.py
@@ -350,9 +350,13 @@ def __radd__(self, other):
         return new
 
     def __neg__(self):
+        if self.dtype.kind == 'b':
+            raise NotImplementedError('negating a sparse boolean '
+                                      'matrix is not supported')
         new = dok_matrix(self.shape, dtype=self.dtype)
         for key in self.keys():
             new[key] = -self[key]
+
         return new
 
     def _mul_scalar(self, other):


Test patch:
diff --git a/scipy/sparse/tests/test_base.py b/scipy/sparse/tests/test_base.py
--- a/scipy/sparse/tests/test_base.py
+++ b/scipy/sparse/tests/test_base.py
@@ -620,8 +620,12 @@ def test_elementwise_power(self):
         assert_raises(NotImplementedError, self.spmatrix(A).power, A)       
 
     def test_neg(self):
-        A = matrix([[-1, 0, 17],[0, -5, 0],[1, -4, 0],[0,0,0]],'d')
-        assert_equal(-A,(-self.spmatrix(A)).todense())
+        A = matrix([[-1, 0, 17], [0, -5, 0], [1, -4, 0], [0, 0, 0]], 'd')
+        assert_equal(-A, (-self.spmatrix(A)).todense())
+
+        # see gh-5843
+        A = matrix([[True, False, False], [False, False, True]])
+        assert_raises(NotImplementedError, self.spmatrix(A).__neg__)
 
     def test_real(self):
         D = matrix([[1 + 3j, 2 - 4j]])
Repository: scipy/scipy

Issue:
scipy.stats.dirichlet computes multivariate gaussian differential entropy??
The docs seem to be wrong here:
"entropy(alpha) Compute the differential entropy of the multivariate normal."

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.dirichlet.html



Discussion:
Clearly a copy-paste error which slipped past reviews. Would you be interested in a PR with a fix?


Resolving patch:
diff --git a/scipy/stats/_multivariate.py b/scipy/stats/_multivariate.py
--- a/scipy/stats/_multivariate.py
+++ b/scipy/stats/_multivariate.py
@@ -1151,7 +1151,7 @@ class dirichlet_gen(multi_rv_generic):
     ``var(alpha)``
         The variance of the Dirichlet distribution
     ``entropy(alpha)``
-        Compute the differential entropy of the multivariate normal.
+        Compute the differential entropy of the Dirichlet distribution.
 
     Parameters
     ----------


Test patch:
Repository: scipy/scipy

Issue:
scipy.stats.entropy does *not* calculate the KL divergence
As stated in the documentation for scipy.stats.entropy, the KL divergence is S = sum(pk \* log(pk / qk), axis=0). However, by calling scipy.special.kl_div, there are extra linear terms in there (pk - qk) that should not be there. The fix is easy: replace the call to scipy.special.kl_div with scipy.special.rel_entr.



Discussion:

Resolving patch:
diff --git a/scipy/stats/_distn_infrastructure.py b/scipy/stats/_distn_infrastructure.py
--- a/scipy/stats/_distn_infrastructure.py
+++ b/scipy/stats/_distn_infrastructure.py
@@ -18,7 +18,7 @@
 from scipy._lib._util import check_random_state, _lazywhere, _lazyselect
 from scipy._lib._util import _valarray as valarray
 
-from scipy.special import (comb, chndtr, entr, kl_div, xlogy, ive)
+from scipy.special import (comb, chndtr, entr, rel_entr, kl_div, xlogy, ive)
 
 # for root finding for discrete distribution ppf, and max likelihood estimation
 from scipy import optimize
@@ -2496,7 +2496,7 @@ def entropy(pk, qk=None, base=None):
         if len(qk) != len(pk):
             raise ValueError("qk and pk must have same length.")
         qk = 1.0*qk / np.sum(qk, axis=0)
-        vec = kl_div(pk, qk)
+        vec = rel_entr(pk, qk)
     S = np.sum(vec, axis=0)
     if base is not None:
         S /= log(base)


Test patch:
Repository: scipy/scipy

Issue:
Differential_evolution maxiter causing memory overflow
Hi,

In the DifferentialEvolutionSolver I'm getting an error if I make my maxiter too large. Its becuase in the `solve` (line 500 of _differentialevolution.py) method you are using `range(1, self.maxiter + 1)`. I think to avoid this all one would have to do is change `range` to `xrange`

Thanks



Discussion:
xrange is not present in Py3. What value are you using for maxiter? Could
you provide a small self contained problem that demonstrates the issue?

`xrange` is in six though--`from scipy._lib.six import xrange`.

> xrange is in six though--from scipy._lib.six import xrange.

That's true. However, maxiter is more on the lines of a few thousand so I'm
surprised there's memory problems.

Ok, so fired up Py2 (been a long while since I did that!), and used a large number for `maxiter`. True to form the minimisation ground to a halt because of memory issues with a `range` call.
I guess I hadn't foreseen that really large numbers would be used for maxiter (I've never used more than maxiter ~ 1000).
I'll submit a PR with the change.


Resolving patch:
diff --git a/scipy/optimize/_differentialevolution.py b/scipy/optimize/_differentialevolution.py
--- a/scipy/optimize/_differentialevolution.py
+++ b/scipy/optimize/_differentialevolution.py
@@ -7,6 +7,7 @@
 from scipy.optimize import OptimizeResult, minimize
 from scipy.optimize.optimize import _status_message
 from scipy._lib._util import check_random_state
+from scipy._lib.six import xrange
 import warnings
 
 
@@ -510,7 +511,7 @@ def solve(self):
             self._calculate_population_energies()
 
         # do the optimisation.
-        for nit in range(1, self.maxiter + 1):
+        for nit in xrange(1, self.maxiter + 1):
             # evolve the population by a generation
             try:
                 next(self)


Test patch:
Repository: scipy/scipy

Issue:
center_of_mass accepts negative values
center_of_mass accepts negative values without warning, which leads to very unexpected behavior, e.g a center of mass that is outside the dimensions of the input array:

```
from scipy.ndimage.measurements import center_of_mass
center_of_mass(np.array([1,3,-5]))
```

returns `(7.0,)`. Of course, a negative mass is not a very sensible input, but this should be at least mentioned in the documentation. Probably even better would be to give a warning or only consider positive values, as is done when negative labels are specified.



Discussion:

Resolving patch:
diff --git a/scipy/ndimage/measurements.py b/scipy/ndimage/measurements.py
--- a/scipy/ndimage/measurements.py
+++ b/scipy/ndimage/measurements.py
@@ -1242,7 +1242,8 @@ def center_of_mass(input, labels=None, index=None):
     Parameters
     ----------
     input : ndarray
-        Data from which to calculate center-of-mass.
+        Data from which to calculate center-of-mass. The masses can either
+        be positive or negative.
     labels : ndarray, optional
         Labels for objects in `input`, as generated by `ndimage.label`.
         Only used with `index`.  Dimensions must be the same as `input`.
@@ -1276,6 +1277,22 @@ def center_of_mass(input, labels=None, index=None):
     >>> ndimage.measurements.center_of_mass(b, lbl, [1,2])
     [(0.33333333333333331, 1.3333333333333333), (3.5, 2.5)]
 
+    Negative masses are also accepted, which can occur for example when
+    bias is removed from measured data due to random noise.
+
+    >>> c = np.array(([-1,0,0,0],
+    ...               [0,-1,-1,0],
+    ...               [0,1,-1,0],
+    ...               [0,1,1,0]))
+    >>> ndimage.measurements.center_of_mass(c)
+    (-4.0, 1.0)
+
+    If there are division by zero issues, the function does not raise an
+    error but rather issues a RuntimeWarning before returning inf and/or NaN.
+
+    >>> d = np.array([-1, 1])
+    >>> ndimage.measurements.center_of_mass(d)
+    (inf,)
     """
     normalizer = sum(input, labels, index)
     grids = numpy.ogrid[[slice(0, i) for i in input.shape]]


Test patch:
Repository: scipy/scipy

Issue:
Documentation for scipy.stats.norm.fit is incorrect
Currently, this function says it should return a 3-tuple:

```
In [1]: import scipy.stats as s

In [2]: print(s.norm.fit.__doc__)
*snip*
        Returns
        -------
        shape, loc, scale : tuple of floats
            MLEs for any shape statistics, followed by those for location and
            scale.
*snip*

In [5]: scipy.__version__
Out[5]: '0.18.1'
```

But the normal distribution is a special case, and its `fit` implementation [only returns a 2-tuple](https://github.com/scipy/scipy/blob/master/scipy/stats/_continuous_distns.py#L185).



Discussion:
Sigh.

To explain (definitely not to defend) what's going on: these method docstrings are inherited verbatim from `rv_continuous`. Since stats distributions are instances, we rewrite docstrings on import, when instances are constructed. This way we can customize docstrings of the instances themselves (this is what ends up in e.g. https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html). Docstrings of instance methods, however, are not writeable under python 2 at least. So they end up being generic, including mysteries like (check `norm.pdf.__doc__`)

```
arg1, arg2, arg3,... : array_like
      The shape parameter(s) for the distribution (see docstring of the instance object for more information).
```

Basically, the best one can do is to _add_ some clarifying info to the docstring of `rv_continuous.fit`. A pull request would be gladly accepted.


Resolving patch:
diff --git a/scipy/stats/_distn_infrastructure.py b/scipy/stats/_distn_infrastructure.py
--- a/scipy/stats/_distn_infrastructure.py
+++ b/scipy/stats/_distn_infrastructure.py
@@ -2079,7 +2079,8 @@ def func(theta, x):
 
     def fit(self, data, *args, **kwds):
         """
-        Return MLEs for shape, location, and scale parameters from data.
+        Return MLEs for shape (if applicable), location, and scale
+        parameters from data.
 
         MLE stands for Maximum Likelihood Estimate.  Starting estimates for
         the fit are given by input arguments; for any arguments not provided
@@ -2122,9 +2123,10 @@ def fit(self, data, *args, **kwds):
 
         Returns
         -------
-        shape, loc, scale : tuple of floats
-            MLEs for any shape statistics, followed by those for location and
-            scale.
+        mle_tuple : tuple of floats
+            MLEs for any shape statistics (if applicable), followed by those
+            for location and scale. For most random variables, shape statistics
+            will be returned, but there are exceptions (e.g. ``norm``).
 
         Notes
         -----


Test patch:
Repository: scipy/scipy

Issue:
use OrderedDict in io.netcdf
See https://github.com/scipy/scipy/pull/5228#issuecomment-159407562. It's in the `pupynere.py` file in the given CVS link; can be easily synced to scipy.



Discussion:
I would like to help in this issue.How Do I do it?


Resolving patch:
diff --git a/scipy/io/netcdf.py b/scipy/io/netcdf.py
--- a/scipy/io/netcdf.py
+++ b/scipy/io/netcdf.py
@@ -39,6 +39,8 @@
 import warnings
 import weakref
 from operator import mul
+from collections import OrderedDict
+
 import mmap as mm
 
 import numpy as np
@@ -245,8 +247,8 @@ def __init__(self, filename, mode='r', mmap=None, version=1,
         self.version_byte = version
         self.maskandscale = maskandscale
 
-        self.dimensions = {}
-        self.variables = {}
+        self.dimensions = OrderedDict()
+        self.variables = OrderedDict()
 
         self._dims = []
         self._recs = 0
@@ -258,7 +260,7 @@ def __init__(self, filename, mode='r', mmap=None, version=1,
             self._mm = mm.mmap(self.fp.fileno(), 0, access=mm.ACCESS_READ)
             self._mm_buf = np.frombuffer(self._mm, dtype=np.int8)
 
-        self._attributes = {}
+        self._attributes = OrderedDict()
 
         if mode in 'ra':
             self._read()
@@ -278,7 +280,7 @@ def close(self):
             try:
                 self.flush()
             finally:
-                self.variables = {}
+                self.variables = OrderedDict()
                 if self._mm_buf is not None:
                     ref = weakref.ref(self._mm_buf)
                     self._mm_buf = None
@@ -615,7 +617,7 @@ def _read_att_array(self):
             raise ValueError("Unexpected header.")
         count = self._unpack_int()
 
-        attributes = {}
+        attributes = OrderedDict()
         for attr in range(count):
             name = asstr(self._unpack_string())
             attributes[name] = self._read_values()
@@ -844,7 +846,7 @@ def __init__(self, data, typecode, size, shape, dimensions,
         self.dimensions = dimensions
         self.maskandscale = maskandscale
 
-        self._attributes = attributes or {}
+        self._attributes = attributes or OrderedDict()
         for k, v in self._attributes.items():
             self.__dict__[k] = v
 
@@ -1044,3 +1046,4 @@ def _apply_missing_value(data, missing_value):
 
 NetCDFFile = netcdf_file
 NetCDFVariable = netcdf_variable
+


Test patch: