Last active
December 14, 2015 19:09
-
-
Save yoneken/5135011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
import scipy as sp | |
from scipy.cluster.vq import kmeans2,whiten | |
from pylab import * | |
s_data = np.genfromtxt('CodeIQ_eaten.txt', dtype='f8,f8,S1', delimiter=' ') | |
data = np.genfromtxt('CodeIQ_data.txt', delimiter=' ') | |
data = sp.r_[[[x[0],x[1]] for x in s_data],data] | |
whitened = whiten(data) | |
centroid, p_labels = kmeans2(whitened, k=3) | |
result = [[[],[]],[[],[]],[[],[]]] | |
for i,p_label in enumerate(p_labels): | |
#print p_label,data[i] | |
result[p_label][0] += [data[i][0]] | |
result[p_label][1] += [data[i][1]] | |
#print result[0] | |
plot(result[0][0],result[0][1],'y.') | |
plot(result[1][0],result[1][1],'g.') | |
plot(result[2][0],result[2][1],'b.') | |
poisonous = np.array([[x[0],x[1]] for x in s_data if x[2]=='x']) | |
safemush = np.array([[x[0],x[1]] for x in s_data if x[2]=='o']) | |
plot(poisonous[:,0],poisonous[:,1], 'kx') | |
plot(safemush[:,0],safemush[:,1], 'ro') | |
show() | |
safe_id = [p_labels[i] for i,x in enumerate(s_data) if x[2]=='o'] | |
#print safe_id | |
for safe in set(safe_id): | |
for i in range(len(safe_id),len(result[safe][0])): | |
print result[safe][0][i],result[safe][1][i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment