索引器错误:只有整数、片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或布尔数组是有效的索引。如何修复它?

2024-06-16 16:49:48 发布

您现在位置:Python中文网/ 问答频道 /正文

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
import matplotlib.cm as cm
import numpy as np # linear algebra
import pandas as pd

kanser = pd.read_csv('breast-cancer-wisconsin.csv') #veri seti okuma
kanser.dropna(inplace=True)  #kayıp değerlerin olduğu yerleri kaldırma

kanser.Class = [1 if i == "benign" else 0 for i in kanser.Class] #target için tür döüşümü yapma
X=kanser.drop('Class', axis = 1) 
y=kanser.Class.values

from sklearn.manifold import TSNE
tsne = TSNE(verbose=1, perplexity=40, n_iter= 4000)
Y = tsne.fit_transform(X)

from sklearn.cluster import KMeans
kmns = KMeans(n_clusters=2, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1, algorithm='auto')
kY = kmns.fit_predict(X)

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

ax1.scatter(Y[:,0],Y[:,1],  c=kY, cmap = "jet", edgecolor = "None", alpha=0.35)
ax1.set_title('k-means clustering plot')

ax2.scatter(Y[:,0],Y[:,1],  c = y['Class'], cmap = "jet", edgecolor = "None", alpha=0.35)
ax2.set_title('Actual clusters')

我得到这个错误

IndexError                                Traceback (most recent call last)
<ipython-input-3-fd6a6e8ca8a9> in <module>()
     26 ax1.set_title('k-means clustering plot')
     27 
---> 28 ax2.scatter(Y[:,0],Y[:,1],  c = y['Class'], cmap = "jet", edgecolor = "None", alpha=0.35)
     29 ax2.set_title('Actual clusters')
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我能为此做些什么


Tags: inimportnonetruetitlematplotlibasclass
1条回答
网友
1楼 · 发布于 2024-06-16 16:49:48

我运行了您的代码并查看了变量。对象“y”似乎是一个列表,其em>getitem方法不接受字符串

from sklearn import datasets
data = datasets.load_breast_cancer()

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
import matplotlib.cm as cm
import numpy as np # linear algebra
import pandas as pd

# kanser = pd.read_csv('breast-cancer-wisconsin.csv') #veri seti okuma
kanser = pd.DataFrame(data.data, columns = data.feature_names)
kanser['Class'] = data.target_names[data.target]
kanser.dropna(inplace=True)  #kayıp değerlerin olduğu yerleri kaldırma

kanser.Class = [1 if i == "benign" else 0 for i in kanser.Class] #target için tür döüşümü yapma
X=kanser.drop('Class', axis = 1) 
y=kanser.Class.values

from sklearn.manifold import TSNE
tsne = TSNE(verbose=1, perplexity=40, n_iter= 4000)
Y = tsne.fit_transform(X)

from sklearn.cluster import KMeans
kmns = KMeans(n_clusters=2, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1, algorithm='auto')
kY = kmns.fit_predict(X)

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

ax1.scatter(Y[:,0],Y[:,1],  c=kY, cmap = "jet", edgecolor = "None", alpha=0.35)
ax1.set_title('k-means clustering plot')


xs = Y[:,0]
ys = Y[:,1]

print(f'y = {y}') 

cs = y['Class']


ax2.scatter(xs, ys, c = cs, cmap = "jet", edgecolor = "None", alpha=0.35)
ax2.set_title('Actual clusters')

输出:

[t-SNE] Computing 121 nearest neighbors...
[t-SNE] Indexed 569 samples in 0.001s...
[t-SNE] Computed neighbors for 569 samples in 0.018s...
[t-SNE] Computed conditional probabilities for sample 569 / 569
[t-SNE] Mean sigma: 33.679708
[t-SNE] KL divergence after 250 iterations with early exaggeration: 50.078777
[t-SNE] KL divergence after 4000 iterations: 0.221172
y = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0
 1 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
<ipython-input-5-73736c0bb107> in <module>()
     37 print(f'y = {y}')
     38 
 -> 39 cs = y['Class']
     40 
     41 

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

相关问题 更多 >