本文介绍了导入错误:无法从“sklearn.base"导入名称“_UnstableArchMixin"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的环境是anaconda3(python 3.7).

My environment is anaconda3(python 3.7).

我用这段代码来测试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()

但是当我在 pycharm 中运行这段代码时,错误信息是:

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)

我该如何解决?

推荐答案

我修改了这个文件 E:\Anaconda37\Lib\site-packages\sklearn\manifold\locally_linear.py

from ..base import BaseEstimator, TransformerMixin, _UnstableArchMixi

改为:

from ..base import BaseEstimator, TransformerMixin

还有

class LocallyLinearEmbedding(BaseEstimator, TransformerMixin,_UnstableArchMixin):

改为

class LocallyLinearEmbedding(BaseEstimator, TransformerMixin):

然后错误消失了.我在python3.6中检查了这个文件,文件中没有_UnstableArchMixin.

Then the error disappeared.And I checked this file in python3.6,there is no _UnstableArchMixin in the file.

这篇关于导入错误:无法从“sklearn.base"导入名称“_UnstableArchMixin"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:11