1
我正在研究一個python項目,我無法繪製出一個包含這些值的圖表,我無法修正這個錯誤,因爲我無法找出列表。被拋出的錯誤,我已經提供了以下'列表索引必須是整數,而不是元組'在python中
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
X = [[ 0.24459249,0.11983918,-0.85683004,-0.89438038,-0.50183492],
[-0.7976035,1.95307959,-1.25068375,-0.00678967,-0.25262571],
[ 0.34136607,0.92766956,0.66223556,-1.46454436,-1.60347224],
[ 2.22677877,1.55027737,-0.02381159,-0.05812014,0.17297636],
[ 0.20753675,-1.00847549,-0.13858473,-0.93317858,0.18550176],
[ 1.38946893,-1.03658362,-0.62923293,1.44856682,0.59064268]]
y = [ 0,2,1,0,0,0,3,0,2,1,0,0,2,0,0,0,1,0,
0,0,0,0,1,3,4,0,0,0,0,3,0,2,1,0,0,0,
3,1,3,0,4,0,0,0,1,4,0,4,0,0,0,0,2,0,
1,1,1,1,0,0,2,0,1,0,2,2,1,0,2,1,0,3,
1,1,1,0,1,0,0,3,0,0,0,3,0,0,0,0,0,0,
0,3,0,0,0,1,2,3,0,0,0,0,0,0,3,0,2,1,
2,3,1,1,0,2,2,0,0,0,3,2,3,4,0,3,1,0,
3,3,0,0,0,0,0,0,0,0,4,3,1,0,0,1,0,1,
0,1,4,0,0,0,0,0,0,4,3,1,1,1,2,0,0,4,
0,0,0,0,0,0,1,0,3,0,1,0,4,1,0,1,0,0,
3,2,0,0,1,0,0,2,1,2,0,3,1,2,0,3,0,0,
0,1,0,0,0,0,0,3,3,3,0,1,0,4,0,3,1,0,
0,0,0,0,0,0,0,3,1,0,0,0,3,2,0,2,1,0,
0,3,2,1,0,0,0,0,0,2,0,2,2,1,3,0,0,1,
0,0,0,0,0,0,0,1,0,3,0,0,4,2,2,2,1,0,
1,0,2,0,1,0,0,0,1,0,2,0,3,0,2,4,2,0,
0,0,1,0,2,2,1,0,3,1,1,2,3,1,0]
def plot_2D(data, target, target_names):
colors = cycle('rgbcmykw')
target_ids = range(len(target_names))
plt.figure()
for i, c, label in zip(target_ids, colors, target_names):
plt.scatter(data[target == i, 0], data[target == i, 1],
c=c, label=label)
plt.legend()
plt.savefig('Reduced_PCA_Graph')
target_names = [0,1,2,3,4]
plot_2D(X, y, target_names)
ERROR錯誤是
Traceback (most recent call last):
File "mat.py", line 49, in <module>
plot_2D(X, y, target_names)
File "mat.py", line 40, in plot_2D
plt.scatter(data[target == i, 0], data[target == i, 1],
TypeError: list indices must be integers, not tuple
請始終包含完整的錯誤追溯。也就是說,'X',因此'data'是列表的列表。像'data [target == i,0]這樣的花式索引只適用於類似numpy的數組對象。 – cel
我認爲您的數據集X和Y存在問題,請檢查它。 –