Help get this topic noticed by sharing it on Twitter, Facebook, or email.

From the book "Introduction to Machine Learning with Python 2016"

How do I import "mglearn" library and its associated modules in Python so I can use them as stated in the book "Introduction to Machine Learning with Python 2016?" Somehow when I write "import mglearn" I am getting a number of error messages.

Thanks.
2 people have
this question
+1
Reply
  • Hi Larry,

    Can you tell me the error messages you are getting when you try "import mglearn"?
    That can be helpful for figuring out what's going on.

    Best regards,
    Chris Olson
    O'Reilly Customer Service
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned

  • Hi, Please see error message below

    ---------------------------------------------------------------------------
    ImportError Traceback (most recent call last)
    in ()
    ----> 1 import mglearn

    C:\Users\larry\Documents\PYTHON LEARN\Python Codes VARIOUS\introduction_to_ml_with_python-master\mglearn\__init__.py in ()
    ----> 1 from . import plots
    2 from . import tools
    3 from .plots import cm3, cm2
    4 from .tools import discrete_scatter
    5 from .plot_helpers import ReBl

    C:\Users\larry\Documents\PYTHON LEARN\Python Codes VARIOUS\introduction_to_ml_with_python-master\mglearn\plots.py in ()
    9 plot_single_hidden_layer_graph,
    10 plot_two_hidden_layer_graph)
    ---> 11 from .plot_linear_regression import plot_linear_regression_wave
    12 from .plot_tree_nonmonotonous import plot_tree_not_monotone
    13 from .plot_scaling import plot_scaling

    C:\Users\larry\Documents\PYTHON LEARN\Python Codes VARIOUS\introduction_to_ml_with_python-master\mglearn\plot_linear_regression.py in ()
    3
    4 from sklearn.linear_model import LinearRegression
    ----> 5 from sklearn.model_selection import train_test_split
    6 from .datasets import make_wave
    7 from .plot_helpers import cm2

    ImportError: No module named model_selection
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Hi Larry.
    The book uses scikit-learn version 0.18. You are using scikit-learn 0.17 or earlier. The final 0.18 version will be release in the coming days, but for now you can install the release candidate with

    pip install scikit-learn==0.18.rc2

    Hope that helps.

    Andy
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly



  • I continue to experience this problem with the latest version.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • mrconfused. You are having a separate problem.
    You did not import mglearn.

    Are you running the notebooks from the book repository? Each notebook starts with the cell "from preamble import *" this imports (among others) the mglearn module. The first chapter in the book explains the imports that are assumed (page 11 on the bottom).

    The mglearn module is part of the code you can download from book repository.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned

  • You don't need to install mglearn if you're downloading the code from the book website. If you want to run any code involving mglearn outside of the folder that comes with the book, indeed pip install mglearn will do it.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • I too am struggling to use mglearn. Where can I find specific instructions? I think I am using notebooks, but since both "import mglearn" and "from preamble import *" result in error messages, I must not really be using the notebooks correctly. Those errors are:

    import mglearn

    ModuleNotFoundError Traceback (most recent call last)
    in ()
    ----> 1 import mglearn

    ModuleNotFoundError: No module named 'mglearn'

    and

    from preamble import *

    ModuleNotFoundError Traceback (most recent call last)
    in ()
    7 import sklearn
    8 from IPython.display import display
    ----> 9 from preamble import *

    ModuleNotFoundError: No module named 'preamble'
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • @Brad how are you running this code? If you want to use the mglearn library outside the notebooks that are provided, the best way to do that is to "pip install mglearn". Did you do that?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • I would prefer to run within the notebooks but I must not be doing it right. I brought up the Github repository with the book's title, and I see the examples there. I do not understand how to open a notebook there. I have been opening notebooks with Anaconda, and I know I am missing something here since none of the imports are present unless I import them and mglearn is nowhere to be found. Thanks for the prompt reply
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Running them locally with Anaconda is the right way to do it. You should ensure that you download the whole content of the repository, though. The github repository contains a folder called mglearn, that needs to be in the same folder as the notebook.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated happy, confident, thankful, excited

  • OK, so now I have read the documentation for Importing IPython Notebooks as Modules and Notebook Loader presented here:
    http://nbviewer.jupyter.org/github/ip..., but I am not sure this is the way to go.
    I previously tried to use the Github desktop to move the folders that comprise introduction_to_ml_with_python to a new folder in Jupyter Notebooks, but I only seem to be able to get it to my list of down loads or to the Github desktop and not into a Jupyter notebook.
    I am still here:
    mglearn
    NameError Traceback (most recent call last)
    in ()
    ----> 1 mglearn

    NameError: name 'mglearn' is not defined
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Sorry I wasn't clear. You shouldn't need to do what's in the documentation you linked to.

    Does the folder that contains the notebook you are running also contain a folder called "mglearn"?

    You can just run "ls" in the notebook to find out what the folder contains.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • So yes, I used cd to move from the downloads folder to another folder that does contain the unzipped folders including mglearn and confirmed that it is present with ls. I still could not get the pair plot of the iris dataset to display,
    iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
    grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
    hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
    returned an empty cell;
    running plt_show() made the plot display ... so progress, but how do I arrange my environment so that next time I want to open a notebook I am in the right place and have benefit of the preamble and other necessary files? I do appreciate your patience.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated happy, confident, thankful, excited

  • Start jupyter notebook in that folder via the command line. That's the easiest way to do it. So "cd" into that folder and do "jupyter notebook".
    I guess you're on windows, so use the anaconda shell.

    The plotting issue is that you need to run "%matplotlib inline" in the notebook (which is in the first cell, which probably failed for you because of the path issue)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Hello again,
    with your previous help I have been proceeding through the book and able to see the examples work. Now I have encountered a problem with graphviz. I have imported it, and can open it on the Mac from my applications. In the jupyter notebook I have been using, however, I get:
    import graphviz

    ModuleNotFoundError Traceback (most recent call last)
    in ()
    ----> 1 import graphviz

    ModuleNotFoundError: No module named 'graphviz'

    Do I need to move the application to the directory in which I am working? Or is there another way to specify a path for python or jupyter to look for the app? Thanks.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • This one is a bit tricky. How did you install graphviz? As I tried to explain in the repository, you need the python and C libraries for graphiviz. I expect you did "conda install graphviz" which installs only the C library, not the python library. You have to also install the python library using "pip install graphviz".
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned

  • I’m confused
    Hi Andy-

    I am having the same problem as the initial poster but have not been able to resolve it (or fully understand some of the explanation) in this thread and a similar thread someone began. I've been enjoying the text in your book but haven't been able to run much code, and am unfortunately not the most computer literate.

    I use Anaconda on Mac OSX. My sci-learn is 0.18. Using jupyter notebooks 4.3.1, I would prefer to write all the code just for practice instead of downloading the GitHub "repository," and so I've tried typing individually:
    pip install mglearn
    $ pip install mglearn
    import mglearn
    from preamble import .....
    into the web based notebook, as well as the jupyter_mac.command window, and in spyder, but mostly get this syntax error:
    ------------
    pip install mglearn
    --
    file "", line 2
    pip install mglearn
    ^
    SyntaxError: invalid syntax
    -------------------------------------------

    Since this hasn't worked, I have then tried downloading "mglearn" folder from the GitHub site (which downloaded as """GitHub - amueller_introduction_to_ml_with_python_ Notebooks and code for the book _Introduction to Machine Learning with Python_"""). Not sure if this is the correct folder but if it is, I'm not sure what file path/folder I should be adding it to in terms of anaconda/jupyter/notebooks.

    I've also read the full Read Me on github, but did not understand it nor your response to a similar question in another thread, which reads:
    ----
    That repository is where "There" is, and also "which notebooks".

    That repository, which is also mentioned in the book, contains all the notebooks containing the code in the book. You can download (or in version control speak "check out") that repository, and get all the code in the book and mglearn and all the datasets.
    -------

    I hope this makes some sense and thank you very much for any help you can offer.

    -Alex D
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly

  • Hi Andy-

    Thank you very much for the quick response and using '! pip install mglearn' worked. Can't wait to get into the book more this weekend.

    -Alex
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned

  • I am having the same trouble with mglearn, it is showing on pip list but none of the python shells are able to import it.
    • Hi Mark.
      It's likely you installed it into a different environment than any of the shells work in.
      You can find out which environment the pip belongs to by typing ``which pip`` and you can find out which environment your python belongs to with ``which python`` and ``python -c "import sys; print(sys.path)"``
      The path that mglearn was installed into needs to show up in the list produced by ``print(sys.path)``. Feel free to open an issue on github.
    • Hello thanks for the help I managed to install mglearn on the jupyter notebook with the !pip install mglearn. Thanks a lot.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • >>> import mglearn
    Traceback (most recent call last):
    File "", line 1, in
    import mglearn
    File "C:\Python36\lib\site-packages\mglearn\__init__.py", line 1, in
    from . import plots
    File "C:\Python36\lib\site-packages\mglearn\plots.py", line 1, in
    from .plot_linear_svc_regularization import plot_linear_svc_regularization
    File "C:\Python36\lib\site-packages\mglearn\plot_linear_svc_regularization.py", line 3, in
    from sklearn.svm import LinearSVC
    File "C:\Python36\lib\site-packages\sklearn\__init__.py", line 134, in
    from .base import clone
    File "C:\Python36\lib\site-packages\sklearn\base.py", line 12, in
    from .utils.fixes import signature
    File "C:\Python36\lib\site-packages\sklearn\utils\__init__.py", line 11, in
    from .validation import (as_float_array,
    File "C:\Python36\lib\site-packages\sklearn\utils\validation.py", line 18, in
    from ..utils.fixes import signature
    File "C:\Python36\lib\site-packages\sklearn\utils\fixes.py", line 144, in
    from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa
    File "C:\Python36\lib\site-packages\scipy\sparse\linalg\__init__.py", line 113, in
    from .isolve import *
    File "C:\Python36\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in
    from .iterative import *
    File "C:\Python36\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in
    from . import _iterative
    ImportError: DLL load failed: The specified module could not be found.

    This is the error which i am getting while importing the mglearn module.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited