AxisError: axis -1 is out of bounds
Question:
I already referred the posts here,here and here
Am trying to run a lassoCV
model and fit it on my training dataset.
So, I tried the below code (this works)
from numpy import arange
from sklearn.linear_model import LassoCV
from sklearn.model_selection import RepeatedKFold
# define model evaluation method
cv = RepeatedKFold(n_splits=10, n_repeats=5, random_state=1)
# define model
model = LassoCV(alphas=arange(0, 1, 0.01), cv=cv)
# fit model
model.fit(X_train, y_train)
# summarize chosen configuration
print('alpha: %f' % model.alpha_) # returns 0.78 as best value
y_pred = model.predict(X_test)
However, when I try the below in next cell of jupyter notebook, I get an error
# define model
model_new = LassoCV(alphas=0.78)
# fit model
model_new.fit(X_train,pd.DataFrame(y_train)) # I also tried just X_train and y_train but still doesn't work
AxisError Traceback (most recent call
last) Input In [136], in <cell line: 4>()
2 model_new = LassoCV(alphas=0.78)
3 # fit model
—-> 4 model_new.fit(X_train,pd.DataFrame(y_train))
File
~Anaconda3libsite-packagessklearnlinear_model_coordinate_descent.py:1687,
in LinearModelCV.fit(self, X, y, sample_weight) 1685
check_scalar_alpha(alpha, f"alphas[{index}]") 1686 # Making
sure alphas is properly ordered.
-> 1687 alphas = np.tile(np.sort(alphas)[::-1], (n_l1_ratio, 1)) 1689 # We want n_alphas to be the number of alphas used for each
l1_ratio. 1690 n_alphas = len(alphas[0])
File <array_function internals>:5, in sort(*args, **kwargs)
File ~Anaconda3libsite-packagesnumpycorefromnumeric.py:998, in
sort(a, axis, kind, order)
996 else:
997 a = asanyarray(a).copy(order="K")
–> 998 a.sort(axis=axis, kind=kind, order=order)
999 return a
AxisError: axis -1 is out of bounds for array of dimension 0
My shape of X_train and y_train looks like below
updated post
Answers:
Try this code, give alpha in nump array
reg = LassoCV(alphas=np.array([0.5]), cv=5, random_state=0).fit(x_train, y_train)
I already referred the posts here,here and here
Am trying to run a lassoCV
model and fit it on my training dataset.
So, I tried the below code (this works)
from numpy import arange
from sklearn.linear_model import LassoCV
from sklearn.model_selection import RepeatedKFold
# define model evaluation method
cv = RepeatedKFold(n_splits=10, n_repeats=5, random_state=1)
# define model
model = LassoCV(alphas=arange(0, 1, 0.01), cv=cv)
# fit model
model.fit(X_train, y_train)
# summarize chosen configuration
print('alpha: %f' % model.alpha_) # returns 0.78 as best value
y_pred = model.predict(X_test)
However, when I try the below in next cell of jupyter notebook, I get an error
# define model
model_new = LassoCV(alphas=0.78)
# fit model
model_new.fit(X_train,pd.DataFrame(y_train)) # I also tried just X_train and y_train but still doesn't work
AxisError Traceback (most recent call
last) Input In [136], in <cell line: 4>()
2 model_new = LassoCV(alphas=0.78)
3 # fit model
—-> 4 model_new.fit(X_train,pd.DataFrame(y_train))File
~Anaconda3libsite-packagessklearnlinear_model_coordinate_descent.py:1687,
in LinearModelCV.fit(self, X, y, sample_weight) 1685
check_scalar_alpha(alpha, f"alphas[{index}]") 1686 # Making
sure alphas is properly ordered.
-> 1687 alphas = np.tile(np.sort(alphas)[::-1], (n_l1_ratio, 1)) 1689 # We want n_alphas to be the number of alphas used for each
l1_ratio. 1690 n_alphas = len(alphas[0])File <array_function internals>:5, in sort(*args, **kwargs)
File ~Anaconda3libsite-packagesnumpycorefromnumeric.py:998, in
sort(a, axis, kind, order)
996 else:
997 a = asanyarray(a).copy(order="K")
–> 998 a.sort(axis=axis, kind=kind, order=order)
999 return aAxisError: axis -1 is out of bounds for array of dimension 0
My shape of X_train and y_train looks like below
updated post
Try this code, give alpha in nump array
reg = LassoCV(alphas=np.array([0.5]), cv=5, random_state=0).fit(x_train, y_train)