Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension mismatch- Not able to apply the transformation on new data #97

Closed
NaSed opened this issue Sep 21, 2020 · 2 comments
Closed

Dimension mismatch- Not able to apply the transformation on new data #97

NaSed opened this issue Sep 21, 2020 · 2 comments

Comments

@NaSed
Copy link

NaSed commented Sep 21, 2020

Hi,
I am using MCA and apply it on training data and then after fitting the model on training data I would like to apply the model on my validation data that has smaller samples than training data and the below error is produced:

trInput.shape: (4393, 13405) # Size of training data
valInput.shape: (1883, 13405)# Size of validation data
MCA(benzecri=False, check_input=True, copy=True, engine='sklearn',
    n_components=2, n_iter='auto', random_state=0)
  File "/Modular_helpers_v3.py", line 88, in Aggregate_RareSNPs
    Feat_val = mcaObj.transform(valInput)
  File "/mca.py", line 272, in transform
    return self.row_coordinates(X)
  File "/mca.py", line 260, in row_coordinates
    return super().row_coordinates(pd.get_dummies(X))
  File "/Code/mca.py", line 197, in row_coordinates
    data=X @ sparse.diags(self.col_masses_.to_numpy() ** -0.5) @ self.V_.T,
  File "/lib/python3.6/site-packages/scipy/sparse/base.py", line 566, in __rmatmul__
    return self.__rmul__(other)
  File "/lib/python3.6/site-packages/scipy/sparse/base.py", line 550, in __rmul__
    return (self.transpose() * tr).transpose()
  File "/lib/python3.6/site-packages/scipy/sparse/base.py", line 516, in __mul__
    raise ValueError('dimension mismatch')
ValueError: dimension mismatch

BTW, since I am using skopt package and it cannot work with the latest version of sklearn, I am using sklearn = 0.22.0 and copy the MCA code from prince's source code in a file called mca.py and made it working.

@NaSed
Copy link
Author

NaSed commented Sep 22, 2020

I found the reason of causing this issue and hope that you guys resolve this issue in your package.
The thing is that the number of different categories in my training data is different from my validation data. For example, column one in training data includes values of ['a', 'b','c','a','b','b','c'] and column one in validation data includes values of ['b', 'a', 'a']. So the first column in training data creates 3 dummy variables (for a, b and c) using get_dummy() function while the first column in validation data creates only 2 dummy variables (for a and b). This difference makes the number of dummy columns different in training and validation data and ValueError: dimension mismatch occurs.

This is an example:

X=pd.DataFrame(data={'x':['d','b','b','a','s','s','c','c'],'y':['d','d','b','b','a','a','c','c']})
mca = prince.MCA(n_components=2, n_iter='auto', copy=True, check_input=True, engine='sklearn', random_state=0)
mca = mca.fit(X)
mca.transform(X.iloc[0:5,])

@MaxHalford
Copy link
Owner

Just fixed this in master! The fix will be part of version 0.7.2. Thanks for opening the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants