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()