使用sklearn中的kmeans算法

8 浏览
0 Comments

使用sklearn中的kmeans算法

我将CSV文件导入了Pandas dataframe,并尝试通过SciKit-Learn运行基本的KMeans聚类。这是我第一次尝试,遇到了一个我不理解的错误。

我得到的错误是:

Traceback (most recent call last):
  File "ml.spatial.clustering.py", line 4, in 
    import seaborn as sns; sns.set()
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/seaborn/__init__.py", line 6, in 
    from .rcmod import *
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/seaborn/rcmod.py", line 5, in 
    from . import palettes, _orig_rc_params
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/seaborn/palettes.py", line 12, in 
    from .utils import desaturate, set_hls_values, get_color_cycle
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/seaborn/utils.py", line 11, in 
    import matplotlib.pyplot as plt
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2374, in 
    switch_backend(rcParams["backend"])
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 14, in 
    from matplotlib.backends import _macosx
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

0
0 Comments

问题:使用sklearn的kmeans算法时出现的错误以及解决方法

如果在使用sklearn的kmeans算法时出现以下错误:

This is not a SK-Learn error, nor a really python installation error either. It is because of your matplotlib does not have a back-end to draw.

原因是matplotlib没有可绘制的后端。解决方法如下:

1. 在matplotlib的配置文件中设置后端:

backend : WXAgg

2. 确保已安装wxPython。

3. 可以参考以下问题:You need to set your backend,以及查看matplotlib的文档了解更多关于what is a backend的内容。

代码示例:

import matplotlib
matplotlib.use('WXAgg')

希望以上解决方法可以帮助你解决使用sklearn的kmeans算法时出现的错误。

0
0 Comments

问题出现的原因是与python安装相关,不是与scikit-learn相关。

可以在https://stackoverflow.com/questions/4130355 找到一个可能的解决方法。

祝好运!

0