votes up 6

For mono-task outputs, use %s

Package:
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]))
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

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]

Jul 15, 2021 kellemnegasi answer
kellemnegasi 30.0k
votes up 1 votes down

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)
Jul 15, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix