2016-11-04 2 views
1

私は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 
+2

必ず完全なエラートレースバックを含めてください。それは 'X'であり、' data'はリストのリストです。 'data [target == i、0]'のような素晴らしい索引付けは、numpyのような配列オブジェクトでのみ機能します。 – cel

+0

あなたのデータセットXに問題があると思って、それを確認してください。 –

答えて

0

私はわからないけど、多分あなたはそのようにしたいISの下に、私はエラーを提供しています。

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') 
    plt.show() 
関連する問題