python - 将 kmeans 与 sklearn 一起使用
问题描述
我有一个 CSV,我已将其拉入 Pandas 数据框,我正在尝试通过 SciKit-Learn 运行基本的 KMeans 聚类。这是我第一次这样做,我遇到了一个我不明白的错误。
import pandas as pd
import sklearn
import numpy as np
import seaborn as sns; sns.set()
from sklearn.cluster import KMeans
input_csv = '/Users/reallymemorable/Documents/Data.Repository/Analysis/Purchasers.Strats.Appends.csv'
# Read the CSV into a dataframe
df = pd.read_csv(input_csv)
# Select out only the relevant columns
df_shortlist = df[['Contact_ID', 'Sales_Stage_Sub', 'Sale_Type', 'Offered_Amount', 'Down_Payment']]
# Create binary dummy columns for Sales_Stage_Sub
df_shortlist_dummy_sales_stage_sub = pd.concat([df_shortlist, pd.get_dummies(df['Sales_Stage_Sub'])], axis=1)
# Create binary dummy columns for Sale_Type
df_shortlist_dummy_sales_stage_sub_and_sale_type = pd.concat([df_shortlist_dummy_sales_stage_sub, pd.get_dummies(df['Sale_Type'])], axis=1)
kmeans = KMeans(n_clusters=4)
kmeans.fit(df_shortlist_dummy_sales_stage_sub_and_sale_type)
y_means = kmeans.predict(df_shortlist_dummy_sales_stage_sub_and_sale_type)
plt.scatter(X[:, 0], X[:, 1], c=y_means, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);
这是我得到的错误:
Traceback (most recent call last):
File "ml.spatial.clustering.py", line 4, in <module>
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 <module>
from .rcmod import *
File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/seaborn/rcmod.py", line 5, in <module>
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 <module>
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 <module>
import matplotlib.pyplot as plt
File "/Users/reallymemorable/.pyenv/versions/3.6.7/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2374, in <module>
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 <module>
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.
解决方案
推荐阅读
- c# - 我需要帮助编写一个通过目录、显示文件并说明有多少和哪些扩展名的程序
- angular - 如何使用 Angular 7 从热敏打印机打印 POS 备忘录
- python-3.x - 无法在 Python 中两次为链表显示相同的输出
- java - Android BottomNavigationView 应用程序崩溃,Android Studio - Java
- r - ggpot中带有facet_wrap()的有序条形图
- selenium - 如何在网页上单击具有 href 属性的按钮
- sql-server - 将列拆分为每个输入/输出记录的单行
- neo4j - py2neo 查询返回无,但相同的查询适用于 neo4j 控制台
- javascript - 仅访问父级来反应兄弟级通信
- javascript - 如何使用 javascript 输入 window.prompt() 的值?