If you use scikit-learn in python, an ImportError will occur.

Asked 1 years ago, Updated 1 years ago, 39 views

I'm reading the introduction to the technical critic's Data Scientist Training Reading Machine Learning
The introductory scikit-learn page starts on page 128.
When I tried the code over there, I got a scikit-learn ImportError

I don't know what's wrong with you, but do you know anyone?

Mac OSX 10.9
python 3.4.3

is in use.

This is the result of running a file called sklearn.py in python

Code (sklearn.py)

#coding:utf-8

import numpy as np 
import matplotlib.pyplot asplt 
from sklearn import linear_model, datasets

# Generate random number data
np.random.seed(0)
regdata=datasets.make_regression(100,1, noise=20.0)

# Learn and view model parameters
lin=linear_model.LinearRegression()
lin.fit (regdata[0], regdata[1])
print("coef and intercept:", lin.coef_, lin.intercept_)
print("score:", lin.score(regdata[0], regdata[1]))

# Draw Graphs
xr = [-2.5, 2.5]
plt.plot(xr,lin.coef_*xr+lin.intercept_)
plt.scatter(regdata[0], regdata[1])

plt.show()

Error

Traceback (most recent call last):
File "sklearn.py", line 6, in <module>
from sklearn import datasets
File"/Users/hoge/programming/training/python/sklearn.py", line 6, in <module>
from sklearn import datasets
ImportError: cannot import name 'datasets'

python machine-learning scikit-learn

2022-09-30 17:00

1 Answers

This script is trying to import itself.
from sklearn is written with the intention of import from an external library called sklearn, but my file name is sklearn.py, so I load myself.

Let's rename the file.

Note: To determine from which file the module is imported, for example:

import sklearn
print(sklearn.__file__)


2022-09-30 17:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.