1

I'm attempting to make a 3D scattered plot to classify data (for an assignment) from a CSV file..

I have three attributes and I'd like to see which class they belong to (Either class 1 or class 2) based on the first three attrib values.

So far, I've managed to make a simple scattered plot (labels etc to be added later) and I want to see if I can somehow colour code my plots according to class. Like let's say I want class 1 plots to be red and class 2 plots to be green.

I have managed to code something but keep on getting "TypeError: list indices must be integers, not list" on the

ax.scatter(x,y,z, zdir='z', c=colormap[categories])

I sort of understand the error but can't really think of a proper way to fix it. I've tried defining a function just to colour code the plots but it didn't pan out as well.

Also, for some really odd reason, my values in the array are printed in scientific notation. it's not an issue, but looks sore to my eye. I'm not really sure what to put for loadtxt's dtype argument if I just want it to be printed like 0.00 instead of 0.0000000e+SomeNumberHere.

Could someone explain this and perhaps provide some help to rectify? Thank you. This is my code:

import numpy as np
import matplotlib.pyplot as pyplot
import csv

myDataset2 = np.loadtxt(open('C:/DM1/DM201501.Assign1.Dataset02.csv', 'rb'),delimiter=',')
print myDataset2

a1=[]
a2=[]
a3=[]

for i in range(100):
    a1.append(myDataset2[i][0])   
    a2.append(myDataset2[i][1])
    a3.append(myDataset2[i][2])

from mpl_toolkits.mplot3d import Axes3D

colormap = [' ', 'r', 'g']
myClasses = []

for j in range(100):
    if myDataset2[j][3]==1:
       myClasses.append(1)
    elif myDataset2[j][3]==2:
        myClasses.append(2)

x=a1
y=a2
z=a3
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x,y,z, zdir='z', c=colormap[myClasses])

pyplot.show()
koffery
  • 53
  • 1
  • 1
  • 5
  • What happens if you make `colormap` a numpy array? e.g. `colormap = np.array([' ', 'r', 'g'])`? – mgilson Feb 25 '15 at 16:13
  • It's embarrassing that I didn't know numpy.exists. Alas, this is a beginner's self-depreciating journey :) Thank you so much! The code looks lovely now :) I am able to instantly tell which one is of Class 1 and Class 2 respectively. Thank you for bearing with my word/code vomit :D – koffery Feb 25 '15 at 16:23

2 Answers2

1

myClasses is a list, as is colormap. So what your doing with:

colormap[myClasses]

is trying to index a list with another list. Which is exactly what your error says.

You will need to do something like:

for class in myClasses:
    ax.scatter(x,y,z, zdir='z', c=colormap[class])
Drew Verlee
  • 1,880
  • 5
  • 21
  • 30
1

The problem is that colormap is a list as is myClasses. Lists can't be indexed with lists (only integers and slices). The solution is to provide a datatype that can be indexed using lists -- np.ndarray comes to mind immediately.

colormap = np.array([' ', 'r', 'g'])

An alternative would be to continue to use lists for both, but do more or less what numpy does when you index an array with a list -- when you go to make the plot, you'd need to create a new list to store the color data (one point for each item in myClasses):

colors = [colormap[idx] for idx in myClasses]
ax.scatter(x,y,z, zdir='z', c=colors)
mgilson
  • 300,191
  • 65
  • 633
  • 696