Skip to content
Advertisement

inverse_transform() takes 2 positional arguments but 3 were given

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?

Advertisement

Answer

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)

If your are using version 1.0.2 then you will get an error as this is not supported in that version.This is for version 1.0.2:enter image description here

This one is the newest version: enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement