question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleNotFoundError: No module named 'sklearn.linear_model'; 'sklearn' is not a package

See original GitHub issue

Hi, I have already install sklearn package on my computer and i’ve also checked using cmd. Everything checkout fine, like its installed and its in the directory as well.

But i cant seem to import it in my pycharm

Code:

import numpy as np
import matplotlib.pyplot as plt 

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

X = np.arange(100).reshape(100, 1)
y = X**4 + X**3 + X + 1

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

rmses = []
degrees = np.arange(1, 10)
min_rmse, min_deg = 1e10, 0

for deg in degrees:

    # Train features
    poly_features = PolynomialFeatures(degree=deg, include_bias=False)
    x_poly_train = poly_features.fit_transform(x_train)

    # Linear regression
    poly_reg = LinearRegression()
    poly_reg.fit(x_poly_train, y_train)

    # Compare with test data
    x_poly_test = poly_features.fit_transform(x_test)
    poly_predict = poly_reg.predict(x_poly_test)
    poly_mse = mean_squared_error(y_test, poly_predict)
    poly_rmse = np.sqrt(poly_mse)
    rmses.append(poly_rmse)
    
    # Cross-validation of degree
    if min_rmse > poly_rmse:
        min_rmse = poly_rmse
        min_deg = deg

# Plot and present results
print('Best degree {} with RMSE {}'.format(min_deg, min_rmse))
        
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(degrees, rmses)
ax.set_yscale('log')
ax.set_xlabel('Degree')
ax.set_ylabel('RMSE')

image

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ogriselcommented, Dec 9, 2021

It might be the case that you have not activate your environment correctly. Try the following commands to understand where pip3 and python come from (in our out your venv) and where scikit-learn is installed:

where pip3
where python
pip3 show scikit-learn
python -m pip show scikit-learn
0reactions
andyquekcommented, Dec 9, 2021

i understand but when i check the venv is already activated and under the folder there is already scikit_learn-1.0.1.dist-info

image

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

ModuleNotFoundError: No module named 'sklearn'
When I use the command: conda install scikit-learn should this not just work? Where does anaconda install the package? I was checking the ......
Read more >
Fix ModuleNotFoundError: No module named 'sklearn'
This error indicates that the scikit-learn (aka sklearn ) package was not installed, or even if it was installed for some reason it...
Read more >
Modulenotfounderror no module named sklearn in Python
Fix the no module named sklearn error in Python · Save your program / notebook. Invoke terminal or the command prompt. · Package...
Read more >
No module named sklearn.linear_model.logistic when I do ...
Hi,. It seems a package is missing in your code env. You can check that on Administration > Code Envs > <select the...
Read more >
ModuleNotFoundError: No module named 'sklearn' in Python
The Python "ModuleNotFoundError: No module named 'sklearn'" occurs when we forget to install the scikit-learn module before importing it or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found