Allen Campbell
Allen Campbell

Reputation: 1

Numpy hstack gives a positional argument error

I'm writing a code where I'm trying to add a column of ones to a numpy matrix (X). The following is the line that throws an error:

self.X = np.hstack((np.ones((self.X.shape[0],1)), self.X))

The error I get is the following, even though I have passed a single tuple as an argument within np.hstack :

TypeError: _vhstack_dispatcher() takes 1 positional argument but 2 were given

Upvotes: 0

Views: 2004

Answers (1)

hpaulj
hpaulj

Reputation: 231385

Expected behavior:

In [58]: np.hstack((np.array([1,2,3]),np.array([1])))
Out[58]: array([1, 2, 3, 1])

Your error message:

In [59]: np.hstack(np.array([1,2,3]),np.array([1]))
Traceback (most recent call last):
  File "<ipython-input-59-f122cbe3ebd9>", line 1, in <module>
    np.hstack(np.array([1,2,3]),np.array([1]))
  File "<__array_function__ internals>", line 4, in hstack
TypeError: _vhstack_dispatcher() takes 1 positional argument but 2 were given

Simulating your case:

In [60]: X = np.ones((3,4))
In [61]: np.hstack((np.ones((X.shape[0],1)), X))
Out[61]: 
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

But without a reproducible example I don't think we can help you.

Upvotes: 1

Related Questions