Issue
I'm currently using PLS Regression to predict a 3 column matrix and using the default n_components of 2. I get a value error due to incorrect matrix dimensions when calling the predict function. This only occurs when there is a binary variable column in the X matrix.
Reproducible Example 1: Binary independent variable results in predict error
import numpy as np
from sklearn.cross_decomposition import PLSRegression
# Create x and y matrix
X_test = np.reshape(np.random.randn(30),(10, 3))
y_test = np.reshape(np.random.randn(30), (10,3))
# Create binary column
t = np.ndarray((10,1))
t[:5,:] = 0
t[5:10,:] = 1
X_test = np.hstack([X_test, t])
# Estimate PLS Regression
pls_reg = PLSRegression(n_components = 2)
pls_reg.fit(y_test, X_test)
pls_reg.predict(X_test)
This results in the following sklearn error:
ValueError Traceback (most recent call last)
/tmp/ipykernel_31713/1419766927.py in <module>
13 pls_reg = PLSRegression(n_components = 2)
14 pls_reg.fit(y_test, X_test)
---> 15 pls_reg.predict(X_test)
~/Documents/esgpls_scoring/lib64/python3.7/site-packages/sklearn/cross_decomposition/_pls.py in predict(self, X, copy)
382 X = check_array(X, copy=copy, dtype=FLOAT_DTYPES)
383 # Normalize
--> 384 X -= self._x_mean
385 X /= self._x_std
386 Ypred = np.dot(X, self.coef_)
ValueError: operands could not be broadcast together with shapes (10,4) (3,) (10,4)
Reproducible Example 2: Same X matrix removing binary column results in no error
# Using same y_test and X_test previously generated
pls_reg = PLSRegression(n_components = 2)
pls_reg.fit(y_test, X_test[:,:3])
pls_reg.predict(X_test[:,:3])
Summary
Is this an issue with the PLSRegression predict function? Is there a theoretical reason why you shouldn't be using a X matrix with this form? It isn't clear from the error thrown.
I'm running this on sklearn version 0.24.1
Solution
There is an error in your code. You should replace the line
pls_reg.fit(y_test, X_test)
with:
pls_reg.fit(X_test, y_test)
Answered By - Antoine Dubuis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.