My environment is anaconda3(python 3.7).
I use this code to test sklearn.cluster
:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
plt.figure()
X, y = make_blobs(n_samples=1000, n_features=2, centers=[[-1,-1], [0,0], [1,1], [2,2]], cluster_std=[0.4, 0.2, 0.2, 0.2], random_state =9) #生成测试数据
for index,k in enumerate((2,3,4,5)):
plt.subplot(2,2,index+1)
y_pred = KMeans(n_clusters=k, random_state=9).fit_predict(X)
score=metrics.calinski_harabaz_score(X, y_pred)
plt.scatter(X[:, 0], X[:, 1], c=y_pred,s=10,edgecolor='k')
plt.text(.99, .01, ('k=%d, score: %.2f' % (k,score)),transform=plt.gca().transAxes, size=10,horizontalalignment='right')
plt.show()
But when I run this code in pycharm,the error message is:
Traceback (most recent call last):
File "E:/test/test1.py", line 5, in <module>
from sklearn.cluster import KMeans
File "E:\Anaconda37\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
from .spectral import spectral_clustering, SpectralClustering
File "E:\Anaconda37\lib\site-packages\sklearn\cluster\spectral.py", line 17, in <module>
from ..manifold import spectral_embedding
File "E:\Anaconda37\lib\site-packages\sklearn\manifold\__init__.py", line 5, in <module>
from .locally_linear import locally_linear_embedding, LocallyLinearEmbedding
File "E:\Anaconda37\lib\site-packages\sklearn\manifold\locally_linear.py", line 12, in <module>
from ..base import BaseEstimator, TransformerMixin, _UnstableArchMixin
ImportError: cannot import name '_UnstableArchMixin' from 'sklearn.base' (E:\Anaconda37\lib\site-packages\sklearn\base.py)
How can I fix it?
I modified this file E:\\Anaconda37\\Lib\\site-packages\\sklearn\\manifold\\locally_linear.py
from ..base import BaseEstimator, TransformerMixin, _UnstableArchMixi
change to:
from ..base import BaseEstimator, TransformerMixin
And
class LocallyLinearEmbedding(BaseEstimator, TransformerMixin,_UnstableArchMixin):
change to
class LocallyLinearEmbedding(BaseEstimator, TransformerMixin):
Then the error disappeared.And I checked this file in python3.6,there is no _UnstableArchMixin
in the file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.