Home > Enterprise >  inverse_transform() takes 2 positional arguments but 3 were given
inverse_transform() takes 2 positional arguments but 3 were given

Time:09-09

I'm trying to use Canonical Correlation Analysis (CCA) in scikit-learn. Still, I'm getting a TypeError, which asserts inverse_transform() takes 2 positional arguments but 3 were given.

Here is my code:

from sklearn.ensemble import RandomForestRegressor
from sklearn.cross_decomposition import CCA
from sklearn.model_selection import RandomizedSearchCV


mycca = CCA(n_components=4)
mycca.fit(x, y)
x, y = mycca.transform(x, y)
x_test = mycca.transform(x_test)


parameters = {
'n_estimators': [50 , 100, 200, 300],
'max_depth': [3, 5, 6 ,8 , 9 ,10]
}
modell = RandomForestRegressor(max_leaf_nodes=2)
modell = RandomizedSearchCV(modell,
                            param_distributions=parameters,
                            cv=5,
                            n_iter=10,
                            n_jobs=-1
)
modell.fit(x , y)
predicted = modell.predict(x_test).flatten()
mycca.inverse_transform(x_test, predicted)

And the last line throws a TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-7d035a65b067> in <module>
----> 1 mycca.inverse_transform(x_test, predicted)

TypeError: inverse_transform() takes 2 positional arguments but 3 were given

It's ridiculous since I passed two arguments exactly, named x_test and predicted. If you're curious about shape of x_test and predicted:

>>>x_test.shape, predicted.shape
((1, 4), (4,))

>>>x_test, predicted
(array([[-0.36689807, -0.03637745,  0.1099326 , -0.22595879]]),
 array([-0.02223153, -0.11932753,  0.08806947, -0.0044004 ]))

How to fix this?

CodePudding user response:

You are not counting the "self" argument that is given to the method when calling it. This means the third argument is the instance of CCA class which is "mycca". try:

mycca.inverse_transform(X=x_test, Y=predicted)

CodePudding user response:

Don't forget this is a method, so one of the arguments is always self. Handling Y for cross decomposition inverse_transform() was added not so long ago (sklearn 1.0.1 I believe), perhaps your version is too old. Alternatively, try invoking inverse_transform() with x_test only.

  • Related