1

I have installed sklearn in anaconda prompt by pip install scikit-learn and showing successful

(base) C:\Users\user>pip install scikit-learn
Requirement already satisfied: scikit-learn in c:\users\user\anaconda3\lib\site-packages (1.0.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\user\anaconda3\lib\site-packages (from scikit-learn) (3.1.0)
Requirement already satisfied: scipy>=1.1.0 in c:\users\user\anaconda3\lib\site-packages (from scikit-learn) (1.7.3)
Requirement already satisfied: numpy>=1.14.6 in c:\users\user\anaconda3\lib\site-packages (from scikit-learn) (1.21.5)
Requirement already satisfied: joblib>=0.11 in c:\users\user\anaconda3\lib\site-packages (from scikit-learn) (1.1.0)

However , When I try to import sklearn with below code, it output errors.

# Import Libraries needed to load the data 
import pandas as pd 
from sklearn.linear_model import LinearRegression

`` `--------------------------------------------------------------------------- ``
`ImportError                               Traceback (most recent call last)`
`~\AppData\Local\Temp/ipykernel_17980/4110711431.py in <module>`
`1 # Import Libraries needed to load the data`
`2 import pandas as pd`
`----> 3 from sklearn.linear_model import LinearRegression`

`~\Anaconda3\lib\site-packages\sklearn_`_`init`_`_.py in <module>`
`78     from . import _distributor_init  # noqa: F401`
`79     from . import __check_build  # noqa: F401`
`---> 80     from .base import clone`
`81     from .utils._show_versions import show_versions`
`82`

`~\Anaconda3\lib\site-packages\sklearn\base.py in <module>`
`19 from . import `__`version`__
`20 from ._config import get_config`
`---> 21 from .utils import _IS_32BIT`
`22 from .utils.validation import check_X_y`
`23 from .utils.validation import check_array`

`~\Anaconda3\lib\site-packages\sklearn\utils_`_`init`_`_.py in <module>`
`21`
`22 from .murmurhash import murmurhash3_32`
`---> 23 from .class_weight import compute_class_weight, compute_sample_weight`
`24 from . import _joblib`
`25 from ..exceptions import DataConversionWarning`

`~\Anaconda3\lib\site-packages\sklearn\utils\class_weight.py in <module>`
`5 import numpy as np`
`6`
`----> 7 from .validation import _deprecate_positional_args`
`8`
`9`

`~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in <module>`
`23 from contextlib import suppress`
`24`
`---> 25 from .fixes import _object_dtype_isnan, parse_version`
`26 from .. import get_config as _get_config`
`27 from ..exceptions import NonBLASDotWarning, PositiveSpectrumWarning`

`~\Anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>`
`16 import scipy.sparse as sp`
`17 import scipy`
`---> 18 import scipy.stats`
`19 from scipy.sparse.linalg import lsqr as sparse_lsqr  # noqa`
`20 from numpy.ma import MaskedArray as _MaskedArray  # TODO: remove in 0.25`

`~\Anaconda3\lib\site-packages\scipy\stats_`_`init`_`_.py in <module>`
`439 """`
`440`
`--> 441 from .stats import *`
`442 from .distributions import *`
`443 from .morestats import *`

`~\Anaconda3\lib\site-packages\scipy\stats\stats.py in <module>`
`35 from numpy import array, asarray, ma`
`36`
`---> 37 from scipy.spatial.distance import cdist`
`38 from scipy.ndimage import measurements`
`39 from scipy._lib._util import (check_random_state, MapWrapper,`

`~\Anaconda3\lib\site-packages\scipy\spatial_`_`init`_`_.py in <module>`
`96 from .kdtree import *`
`97 from .ckdtree import *`
`---> 98 from .qhull import *`
`99 from ._spherical_voronoi import SphericalVoronoi`
`100 from ._plotutils import *`

`` ImportError: DLL load failed: The specified module could not be found.` ``

I have tried below amendment but problems still. Hope someone can help.

$> conda remove --force numpy, scipy
$> pip install numpy, scipy
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
miltonc
  • 11
  • 1
  • 4
  • I don't use `conda` but it seems mixing `pip install` and `conda install` is full of issues. Perhaps try `conda install scikit-learn`? Also, is `base` a virtual environment? If so, you'll need to ensure that `jupyter` is using the same virtual environment where you installed `scikit-learn`. – rickhg12hs Apr 03 '22 at 00:33

2 Answers2

2

Create a cell in the notebook with the following content

%pip install scikit-learn

and execute it.

  • @QuinSinclair, please use current best practices when advising folks. To avoid the issues caused by environments in the shell and notebook perhaps being different, it is now modern best practices to use `%pip install` or `%conda install`, see [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez). These magics commands were added recently to help mitigate environment problems. In fact, because of automagics usually being on by default, even no symbol in front of `pip` or `conda` will often trigger the correct magic command. – Wayne Apr 01 '22 at 16:08
0

I'm seeing in your list of errors the following:

from scipy.spatial.distance import cdist

This makes me wonder if you installed scipy, similar to what is suggested here and here, if that would help?

You can try in a cell in your notebook:

%pip install scipy

If it says it is already installed, maybe change it to read uninstall and then try the install of scipy again and then try your from sklearn.linear_model import LinearRegression line again.

Wayne
  • 6,607
  • 8
  • 36
  • 93