I am currently trying to create a binary classification using Logistic regression. Currently I am in determining the feature importance. I already did the data preprocessing (One Hot Encoding and sampling) and ran it with XGBoost and RandomFOrestClassifier, no problem
However, when I tried to fit a LogisticRegression model (below is my code in Notebook),
from sklearn.linear_model import LogisticRegression
#Logistic Regression
# fit the model
model = LogisticRegression()
# fit the model
model.fit(np.array(X_over), np.array(y_over))
# get importance
importance = model.coef_[0]
# summarize feature importance
df_imp = pd.DataFrame({'feature':list(X_over.columns), 'importance':importance})
display(df_imp.sort_values('importance', ascending=False).head(20))
# plot feature importance
plt.bar(list(X_over.columns), importance)
plt.show()
it gave an error
...
~\AppData\Local\Continuum\anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
223 with parallel_backend(self._backend, n_jobs=self._n_jobs):
224 return [func(*args, **kwargs)
--> 225 for func, args, kwargs in self.items]
226
227 def __len__(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py in _logistic_regression_path(X, y, pos_class, Cs, fit_intercept, max_iter, tol, verbose, solver, coef, class_weight, dual, penalty, intercept_scaling, multi_class, random_state, check_input, max_squared_sum, sample_weight, l1_ratio)
762 n_iter_i = _check_optimize_result(
763 solver, opt_res, max_iter,
--> 764 extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
765 w0, loss = opt_res.x, opt_res.fun
766 elif solver == 'newton-cg':
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\optimize.py in _check_optimize_result(solver, result, max_iter, extra_warning_msg)
241 " https://scikit-learn.org/stable/modules/"
242 "preprocessing.html"
--> 243 ).format(solver, result.status, result.message.decode("latin1"))
244 if extra_warning_msg is not None:
245 warning_msg += "\n" + extra_warning_msg
AttributeError: 'str' object has no attribute 'decode'
I googled it and mostly all the responses said that this error is because the scikit-learn library tried to decode an already decoded string. But I don't know how to solve it in my case here. I made sure all my data is either integer or float64, and no strings.
I tried to upgrade my scikit-learn
using the below command, still, that didn't solve the AttributeError: 'str' object has no attribute 'decode'
issue
pip install scikit-learn -U
Finally, below code snippet solved the issue, add the solver as liblinear
model = LogisticRegression(solver='liblinear')
In the most recent version of scikit-learn (now 0.24.1) the problem has been fixed enclosing a part of code in a try-catch block which I report below: the file is
optimize.py -> _check_optimize_result(solver, result, max_iter=None,
extra_warning_msg=None)
and the code piece is
if solver == "lbfgs":
if result.status != 0:
try:
# The message is already decoded in scipy>=1.6.0
result_message = result.message.decode("latin1")
except AttributeError:
result_message = result.message
warning_msg = (
"{} failed to converge (status={}):\n{}.\n\n"
"Increase the number of iterations (max_iter) "
"or scale the data as shown in:\n"
" https://scikit-learn.org/stable/modules/"
"preprocessing.html"
).format(solver, result.status, result_message)
Which was just
if solver == "lbfgs":
if result.status != 0:
warning_msg = (
"{} failed to converge (status={}):\n{}.\n\n"
"Increase the number of iterations (max_iter) "
"or scale the data as shown in:\n"
" https://scikit-learn.org/stable/modules/"
"preprocessing.html"
).format(solver, result.status, result.message.decode("latin1"))
before. So upgrading scikit-learn solves the problem.
There is a bug with solver='lbfgs'. Changing to 'sag' works around it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With