K 表示图未正确显示

Posted

技术标签:

【中文标题】K 表示图未正确显示【英文标题】:K Means plot not showing properly 【发布时间】:2020-06-01 18:43:52 【问题描述】:

我正在尝试在Divorce dataset from UCI Machine Learning Repository 上可视化K-Means clustering implementation 的结果。

我的代码如下:

import pandas as pd, seaborn as sns1
import matplotlib.pyplot as plt
from scipy import cluster
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split

df = pd.read_csv('C:\\Users\\wundermahn\\Desktop\\code\\divorce.csv')
y = df['Class']
X = df.drop('Class', axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

y_pred = KMeans(n_clusters=2, random_state=170).fit_predict(X_test)
plt.subplot(221)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred)
plt.title("Guess")

plt.show()

这在很大程度上受到上面超链接 K-Means 链接的影响。

我收到一个错误:

Traceback (most recent call last):
  File "c:\Users\wundermahn\Desktop\code\kmeans.py", line 25, in <module>
    plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred)
  File "C:\Python367-64\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Python367-64\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 116, in pandas._libs.index.IndexEngine.get_loc
TypeError: '(slice(None, None, None), 0)' is an invalid key

我做错了什么?为什么我的 slice None 在我清楚地向它传递数据时是类型?

【问题讨论】:

【参考方案1】:

plt.scatter 期望 xy 为 array_like。显然,这个函数的数据框不是数组。

如果您将Xplt_scatter 的输入转换为Numpy 数组,它应该可以工作。

import pandas as pd, seaborn as sns1
import matplotlib.pyplot as plt
from scipy import cluster
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
import numpy as np
df = pd.read_csv('divorce.csv', sep=';')
y = df['Class']
X = np.array(df.drop('Class', axis=1))

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

y_pred = KMeans(n_clusters=2, random_state=170).fit_predict(X_test)
plt.subplot(221)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred)
plt.title("Guess")
plt.show()

【讨论】:

以上是关于K 表示图未正确显示的主要内容,如果未能解决你的问题,请参考以下文章

Python中两个图的子图未正确显示

响应式文件管理器缩略图未显示

Altair 堆积图未显示所有值

matplotlib 3d曲面图未显示[重复]

气泡图未显示元包 R 中的所有点

Python Seaborn 联合图未在图表上显示相关系数和 p 值