8
respostas

Erro na importação do MultinomialNB

Pessoal, boa noite.

Estou tendo dificuldades em importar a função MultinomialNB. O seguinte erro é apresentado no terminal:

python classifica_acessos.py
Traceback (most recent call last):
  File "classifica_acessos.py", line 5, in <module>
    from sklearn.naive_bayes import MultinomialNB
ImportError: No module named sklearn.naive_bayes

Vocês conseguem me ajudar?

Obrigado,

8 respostas

Bom dia Arthur,

A indicação do erro é que ele não encontrou a instalação do sklearn. Pode tentar instalar o pip e o scikit-learn com esses comandos? https://cursos.alura.com.br/course/introducao-a-machine-learning-com-classificacao/task/14540

[]s

Obrigado, Guilherme.

De fato, existe um problema de instalação do sklearn. Encontrei essa declaração abaixo:

c:\Python27\Lib\site-packages
λ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.naive_bayes import MultinomialNB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sklearn\__init__.py", line 56, in <module>
    from . import __check_build
  File "sklearn\__check_build\__init__.py", line 46, in <module>
    raise_build_error(e)
  File "sklearn\__check_build\__init__.py", line 41, in raise_build_error
    %s""" % (e, local_dir, ''.join(dir_content).strip(), msg))
ImportError: DLL load failed: %1 nÒo Ú um aplicativo Win32 vßlido.
___________________________________________________________________________
Contents of sklearn\__check_build:
setup.py                  setup.pyc                 _check_build.pyd
__init__.py               __init__.pyc
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.

If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.

If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.

Quando fui seguir a orientação acima, tive o seguinte retorno

C:\Python27\Lib\site-packages\sklearn
λ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 84, in <module>
    from numpy.distutils.core import setup
ImportError: No module named numpy.distutils.core

Estou cometendo algum erro primário na instalação?

Obrigado,

Parece que falta instalar o numpy, e que é um erro razoavelente comum: https://github.com/scikit-learn/scikit-learn/issues/4164

Você pode tentar então:

pip install numpy

Ou então primeiro deinstalar o numpy para tentar novamente:

pip install -U numpy
pip install numpy

E depois tentar instalar o scipy novamente:

pip install scipy

Se precisar, tente desinstalar e instalar o scipy novamente:

pip install -U scipy
pip install scipy

Pelo que li nos commits relativos ao problema, isso deve ajudar.

Perfeito, consegui instalar o numpy.

No entanto, ainda estou tendo problemas - dessa vez, para instalar o scipy.

c:\Python27\Scripts
λ pip install scipy
Collecting scipy
  Downloading scipy-0.18.1.tar.gz (13.1MB)
    100% |################################| 13.1MB 48kB/s
Installing collected packages: scipy
  Running setup.py install for scipy ... error
    Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\arthur~1\\appdata\\local\\temp\\pip-build-r9tji9\\scipy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\arthur~1\appdata\local\temp\pip-ussxqz-record\install-record.txt --single-version-externally-managed --compile:

    Note: if you need reliable uninstall behavior, then install
    with pip instead of using `setup.py install`:

      - `pip install .`       (from a git repo or downloaded source
                               release)
      - `pip install scipy`   (last SciPy release on PyPI)


    lapack_opt_info:
    openblas_lapack_info:
      libraries openblas not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    lapack_mkl_info:
      libraries mkl_rt not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    atlas_3_10_threads_info:
    Setting PTATLAS=ATLAS
    c:\python27\lib\site-packages\numpy\distutils\system_info.py:639: UserWarning: Specified path C:\projects\numpy-wheels\windows-wheel-builder\atlas-builds\atlas-3.10.1-sse2-32\lib is invalid.
      warnings.warn('Specified path %s is invalid.' % d)
    <class 'numpy.distutils.system_info.atlas_3_10_threads_info'>
      NOT AVAILABLE

    atlas_3_10_info:
    <class 'numpy.distutils.system_info.atlas_3_10_info'>
      NOT AVAILABLE

    atlas_threads_info:
    Setting PTATLAS=ATLAS
    <class 'numpy.distutils.system_info.atlas_threads_info'>
      NOT AVAILABLE

    atlas_info:
    <class 'numpy.distutils.system_info.atlas_info'>
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1532: UserWarning:
        Atlas (http://math-atlas.sourceforge.net/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [atlas]) or by setting
        the ATLAS environment variable.
      warnings.warn(AtlasNotFoundError.__doc__)
    lapack_info:
      libraries lapack not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1543: UserWarning:
        Lapack (http://www.netlib.org/lapack/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [lapack]) or by setting
        the LAPACK environment variable.
      warnings.warn(LapackNotFoundError.__doc__)
    lapack_src_info:
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1546: UserWarning:
        Lapack (http://www.netlib.org/lapack/) sources not found.
        Directories to search for the sources can be specified in the
        numpy/distutils/site.cfg file (section [lapack_src]) or by setting
        the LAPACK_SRC environment variable.
      warnings.warn(LapackSrcNotFoundError.__doc__)
      NOT AVAILABLE

    Running from scipy source directory.
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "c:\users\arthur~1\appdata\local\temp\pip-build-r9tji9\scipy\setup.py", line 415, in <module>
        setup_package()
      File "c:\users\arthur~1\appdata\local\temp\pip-build-r9tji9\scipy\setup.py", line 411, in setup_package
        setup(**metadata)
      File "c:\python27\lib\site-packages\numpy\distutils\core.py", line 135, in setup
        config = configuration()
      File "c:\users\arthur~1\appdata\local\temp\pip-build-r9tji9\scipy\setup.py", line 335, in configuration
        config.add_subpackage('scipy')
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 1000, in add_subpackage
        caller_level = 2)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 969, in get_subpackage
        caller_level = caller_level + 1)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 906, in _get_configuration_from_setup_py
        config = setup_module.configuration(*args)
      File "scipy\setup.py", line 15, in configuration
        config.add_subpackage('linalg')
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 1000, in add_subpackage
        caller_level = 2)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 969, in get_subpackage
        caller_level = caller_level + 1)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 906, in _get_configuration_from_setup_py
        config = setup_module.configuration(*args)
      File "scipy\linalg\setup.py", line 20, in configuration
        raise NotFoundError('no lapack/blas resources found')
    numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

    ----------------------------------------
Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\arthur~1\\appdata\\local\\temp\\pip-build-r9tji9\\scipy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\arthur~1\appdata\local\temp\pip-ussxqz-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\arthur~1\appdata\local\temp\pip-build-r9tji9\scipy\
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

Tentei desinstalá-lo, mas tive uma resposta semelhante

c:\Python27\Scripts
λ pip install -U scipy
Collecting scipy
  Using cached scipy-0.18.1.tar.gz
Installing collected packages: scipy
  Running setup.py install for scipy ... error
    Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\arthur~1\\appdata\\local\\temp\\pip-build-v1zg6s\\scipy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\arthur~1\appdata\local\temp\pip-fcvwft-record\install-record.txt --single-version-externally-managed --compile:

    Note: if you need reliable uninstall behavior, then install
    with pip instead of using `setup.py install`:

      - `pip install .`       (from a git repo or downloaded source
                               release)
      - `pip install scipy`   (last SciPy release on PyPI)


    lapack_opt_info:
    openblas_lapack_info:
      libraries openblas not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    lapack_mkl_info:
      libraries mkl_rt not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    atlas_3_10_threads_info:
    Setting PTATLAS=ATLAS
    c:\python27\lib\site-packages\numpy\distutils\system_info.py:639: UserWarning: Specified path C:\projects\numpy-wheels\windows-wheel-builder\atlas-builds\atlas-3.10.1-sse2-32\lib is invalid.
      warnings.warn('Specified path %s is invalid.' % d)
    <class 'numpy.distutils.system_info.atlas_3_10_threads_info'>
      NOT AVAILABLE

    atlas_3_10_info:
    <class 'numpy.distutils.system_info.atlas_3_10_info'>
      NOT AVAILABLE

    atlas_threads_info:
    Setting PTATLAS=ATLAS
    <class 'numpy.distutils.system_info.atlas_threads_info'>
      NOT AVAILABLE

    atlas_info:
    <class 'numpy.distutils.system_info.atlas_info'>
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1532: UserWarning:
        Atlas (http://math-atlas.sourceforge.net/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [atlas]) or by setting
        the ATLAS environment variable.
      warnings.warn(AtlasNotFoundError.__doc__)
    lapack_info:
      libraries lapack not found in ['c:\\python27\\lib', 'C:\\', 'c:\\python27\\libs']
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1543: UserWarning:
        Lapack (http://www.netlib.org/lapack/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [lapack]) or by setting
        the LAPACK environment variable.
      warnings.warn(LapackNotFoundError.__doc__)
    lapack_src_info:
      NOT AVAILABLE

    c:\python27\lib\site-packages\numpy\distutils\system_info.py:1546: UserWarning:
        Lapack (http://www.netlib.org/lapack/) sources not found.
        Directories to search for the sources can be specified in the
        numpy/distutils/site.cfg file (section [lapack_src]) or by setting
        the LAPACK_SRC environment variable.
      warnings.warn(LapackSrcNotFoundError.__doc__)
      NOT AVAILABLE

    Running from scipy source directory.
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "c:\users\arthur~1\appdata\local\temp\pip-build-v1zg6s\scipy\setup.py", line 415, in <module>
        setup_package()
      File "c:\users\arthur~1\appdata\local\temp\pip-build-v1zg6s\scipy\setup.py", line 411, in setup_package
        setup(**metadata)
      File "c:\python27\lib\site-packages\numpy\distutils\core.py", line 135, in setup
        config = configuration()
      File "c:\users\arthur~1\appdata\local\temp\pip-build-v1zg6s\scipy\setup.py", line 335, in configuration
        config.add_subpackage('scipy')
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 1000, in add_subpackage
        caller_level = 2)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 969, in get_subpackage
        caller_level = caller_level + 1)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 906, in _get_configuration_from_setup_py
        config = setup_module.configuration(*args)
      File "scipy\setup.py", line 15, in configuration
        config.add_subpackage('linalg')
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 1000, in add_subpackage
        caller_level = 2)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 969, in get_subpackage
        caller_level = caller_level + 1)
      File "c:\python27\lib\site-packages\numpy\distutils\misc_util.py", line 906, in _get_configuration_from_setup_py
        config = setup_module.configuration(*args)
      File "scipy\linalg\setup.py", line 20, in configuration
        raise NotFoundError('no lapack/blas resources found')
    numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

    ----------------------------------------
Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\arthur~1\\appdata\\local\\temp\\pip-build-v1zg6s\\scipy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\arthur~1\appdata\local\temp\pip-fcvwft-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\arthur~1\appdata\local\temp\pip-build-v1zg6s\scipy\
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

Estou cometendo mais algum erro?

Obrigado,

Não consigo ver nada de errado, somente a sugestão dele de atualizar o pip, ele está na versão 8.1.1 e a versão atual é a 9.0.1. A linha que ele sugere no fim do comando é:

python -m pip install --upgrade pip'

Boa noite,

tive o seguinte erro:

from sklearn.naivebayes import MultinomialNB Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\sklearn_init.py", line 57, in from .base import clone File "C:\Python27\lib\site-packages\sklearn\base.py", line 10, in from scipy import sparse File "C:\Python27\lib\site-packages\scipy\init.py", line 61, in from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl ImportError: cannot import name NUMPY_MKL

alguem poderia me ajudar?

Tudo bem Matheus? Tudo indica que não está instalado o numpy+mkl.

Tentei baixar ele daqui: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

E instalar com o comando pip install nome-do-arquivo.whl

Olá Guilherme, instalei o numpy e funcionou. Muito obrigado pela ajuda

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software