For mono-task outputs, use %s
Package:
scikit-learn
47032

Exception Class:
ValueError
Raise code
y = y.astype(X.dtype)
if hasattr(self, 'l1_ratio'):
model_str = 'ElasticNet'
else:
model_str = 'Lasso'
if y.ndim == 1:
raise ValueError("For mono-task outputs, use %s" % model_str)
n_samples, n_features = X.shape
_, n_tasks = y.shape
if n_samples != y.shape[0]:
raise ValueError("X and y have inconsistent dimensions (%d != %d)"
% (n_samples, y.shape[0]))
Links to the raise (1)
https://github.com/scikit-learn/scikit-learn/blob/c67518350f91072f9d37ed09c5ef7edf555b6cf6/sklearn/linear_model/_coordinate_descent.py#L1920Ways to fix
If the shape of the label array is 1D then the model ElasticNet
should be used instead of MultiTaskElasticNet.
Reproducing the error:
from sklearn import linear_model
clf = linear_model.MultiTaskElasticNet(alpha=0.1,)
X = [[0,0], [1, 1], [2, 2]]
y = [[0, 0], [1, 1], [2, 2]]
y = [0,1,1]
clf.fit(X,y)
print(clf.coef_)
Output Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-5aa9de9a397b> in <module>()
4 y = [[0, 0], [1, 1], [2, 2]]
5 y = [0,1,1]
----> 6 clf.fit(X,y)
7 print(clf.coef_)
/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_coordinate_descent.py in fit(self, X, y)
1760 model_str = 'Lasso'
1761 if y.ndim == 1:
-> 1762 raise ValueError("For mono-task outputs, use %s" % model_str)
1763
1764 n_samples, n_features = X.shape
ValueError: For mono-task outputs, use ElasticNet
Fixed version of the code:
from sklearn import linear_model
clf = linear_model.ElasticNet(alpha=0.1,)
X = [[0,0], [1, 1], [2, 2]]
y = [[0, 0], [1, 1], [2, 2]]
y = [0,1,1]
clf.fit(X,y)
print(clf.coef_)
Output:
[0.20493848 0.20470839]
Summary:
This exception is thrown when the fit function is called on an instance of MultiTaskElasticNet. The fit function takes in 2 parameters: X and y. Both of them must be 2 dimensional array-like values. X should be of shape (n_smaples, n_features) and y should be of shape (n_samples, n_tasks). This exception is thrown if the value of y is only one-dimensional. Therefore you should ensure that y is a 2D array to avoid this exception.
Code to Reproduce the Exception (Wrong):
from sklearn.linear_model._coordinate_descent import MultiTaskElasticNet
import numpy as np
mten = MultiTaskElasticNet()
X = np.array([[1,2],[3,4]])
y = np.array([1,2, 3, 4])
mten.fit(X, y)
Error Message:
ValueError Traceback (most recent call last)
<ipython-input-46-ab7580b5d789> in <module>()
5 X = np.array([[1,2],[3,4]])
6 y = np.array([1,2, 3, 4])
----> 7 mten.fit(X, y)
/usr/local/lib/python3.7/dist-packages/sklearn/linear_model/_coordinate_descent.py in fit(self, X, y)
1760 model_str = 'Lasso'
1761 if y.ndim == 1:
-> 1762 raise ValueError("For mono-task outputs, use %s" % model_str)
1763
1764 n_samples, n_features = X.shape
ValueError: For mono-task outputs, use ElasticNet
Working Version (Right):
from sklearn.linear_model._coordinate_descent import MultiTaskElasticNet
import numpy as np
mten = MultiTaskElasticNet()
X = np.array([[1,2],[3,4]])
y = np.array([[1,2],[3,4]])
mten.fit(X, y)
Add a possible fix
Please authorize to post fix