3

I am running the Python 2 notebook at this site https://github.com/vsmolyakov/experiments_with_python/blob/master/chp01/ensemble_methods.ipynb to practice ensemble methods with python, and getting an error when running this part of the code in Python 3:

plt.figure()
(_, caps, _) = plt.errorbar(num_est, bg_clf_cv_mean, yerr=bg_clf_cv_std, c='blue', fmt='-o', capsize=5)
for cap in caps:
    cap.set_markeredgewidth(1)                                                                                                                                
plt.ylabel('Accuracy'); plt.xlabel('Ensemble Size'); plt.title('Bagging Tree Ensemble');
plt.show()

The error is "matplotlib does not support generators as input" What is the solution?

2
  • what is the line that is causing the error ? and what kind of input are you feeding matplotlib ?
    – user7111260
    Dec 20, 2018 at 5:43
  • That notebook is clearly Python 2.x code from 2017 (you can tell from the no parentheses in a print statement). So, if you can't find a later version, you have to update it from 2-to-3. I suggest you contact the author to contribute your 3.x updates.
    – smci
    Nov 22, 2023 at 22:44

1 Answer 1

5

In that example there is a line num_est = map(int, np.linspace(1,100,20)). This produces a list in python 2.7. But in python 3 it is just a generator. The map is strange anyways, so I'd recommend to replace that line by

num_est = np.linspace(1,100,20).astype(int)
2
  • is it ok to typecast into list(map). How do I find which one is more efficient?
    – AAI
    Aug 6, 2019 at 11:35
  • @AAI .astype(int) is way faster, although if you only have some 20 elements both would still be in the microsecond regime or below. Aug 6, 2019 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.