function missing 1 required positional argument: 'shape'
Package:
scipy
8546
Exception Class:
TypeError
Raise code
if not (-2 <= axis <= 1):
raise ValueError("axis out of range")
def check_shape(args, current_shape=None):
"""Imitate numpy.matrix handling of shape arguments"""
if len(args) == 0:
raise TypeError("function missing 1 required positional argument: "
"'shape'")
elif len(args) == 1:
try:
shape_iter = iter(args[0])
except TypeError:
new_shape = (operator.index(args[0]), )
else:
Links to the raise (1)
https://github.com/scipy/scipy/blob/e4b3e6eb372b8c1d875f2adf607630a31e2a609c/scipy/sparse/sputils.py#L280Ways to fix
This exception is raised when trying to call the reshape method without the shape parameter.
Code to reproduce the exception:
import numpy as np
from scipy.sparse import csr_matrix
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sm = csr_matrix((data, (row, col)), shape=(4, 3))
print(sm.shape)
new_sm = sm.reshape()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-d33536532489> in <module>() 11 sm = csr_matrix((data, (row, col)), shape=(4, 3)) 12 print(sm.shape) ---> 13 new_sm = sm.reshape()
/usr/local/lib/python3.7/dist-packages/scipy/sparse/base.py in reshape(self, *args, **kwargs) 121 # If the shape already matches, don't bother doing an actual reshape 122 # Otherwise, the default is to convert to COO and use its reshape --> 123 shape = check_shape(args, self.shape) 124 order, copy = check_reshape_kwargs(kwargs) 125 if shape == self.shape:
/usr/local/lib/python3.7/dist-packages/scipy/sparse/sputils.py in check_shape(args, current_shape) 268 """Imitate numpy.matrix handling of shape arguments""" 269 if len(args) == 0: --> 270 raise TypeError("function missing 1 required positional argument: " 271 "'shape'") 272 elif len(args) == 1:
TypeError: function missing 1 required positional argument: 'shape'
Fixed version of the code:
import numpy as np
from scipy.sparse import csr_matrix
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sm = csr_matrix((data, (row, col)), shape=(4, 3))
print(sm.shape)
new_sm = sm.reshape((6,2))
(4, 3)
Add a possible fix
Please authorize to post fix