The numpy.triu does not work, tri() missing 1 required positional argument

460 Views Asked by At

the code is :

adjj = np.triu(self.adj,k=1)

then Pycharm stopped and reported :

File "D:/mapgeneralization/rastertest/dbscan.py", line 436, in init adjj = np.triu(self.adj,k=1) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\twodim_base.py", line 465, in triu mask = tri(*m.shape[-2:], k=k-1, dtype=bool) TypeError: tri() missing 1 required positional argument: 'N'

1

There are 1 best solutions below

0
hpaulj On

What is self.adj? I get this error message if I use a scalar argument:

In [420]: np.triu(3, k=1)
Traceback (most recent call last):
  File "<ipython-input-420-860353c7169f>", line 1, in <module>
    np.triu(3, k=1)
  File "<__array_function__ internals>", line 5, in triu
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/twodim_base.py", line 499, in triu
    mask = tri(*m.shape[-2:], k=k-1, dtype=bool)
TypeError: tri() missing 1 required positional argument: 'N'

but no error is I give it a 2d array:

In [421]: np.triu(np.ones((3,4)), k=1)
Out[421]: 
array([[0., 1., 1., 1.],
       [0., 0., 1., 1.],
       [0., 0., 0., 1.]])

As with many numpy functions, triu starts with

m = asanyarray(m)

If m is something like a scipy sparse matrix, the result is a 0d object dtype array. Argumentwise this will be the same as my scalar example.