字符串索引为整数时,“字符串索引必须为整数错误”

时间:2019-11-17 01:14:18

标签: python pca

我遇到了错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-996316424e3b> in <module>
     11     #print (i)
     12     plt.text(pca[i, 0], pca[i, 1], str(clusters[i]),
---> 13              color=colours[y[i]],
     14              fontdict={'weight': 'bold', 'size': 50}
     15         )

TypeError: string indices must be integers

即使我的字符串索引是整数,如以下代码所示:

X = df.drop('season', axis = 1)
y = df.season
scaler = StandardScaler()
X_std = scaler.fit_transform(X)
clusters = agg_cluster.fit_predict(X_std)
pca = PCA(n_components=2).fit_transform(X_std)

plt.figure(figsize=(10,5))
colours = 'rbg'
for i in range(pca.shape[0]):
    plt.text(pca[i, 0], pca[i, 1], str(clusters[i]),
             color=colours[y[i]],
             fontdict={'weight': 'bold', 'size': 50}
        )

季节功能仅包含四个可能的值,并且为数字(0,1,2,3)。

0 个答案:

没有答案