def twoDisjointLinesWithMSClustering(): t = arange(-1,1,0.002) x = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) y = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) z = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) line1 = array(zip(x,y,z)) line = vstack((line1, line1 + 3)) lpc = LPCImpl(start_points_generator = lpcMeanShift(ms_h = 1), h = 0.05, mult = None, it = 200, cross = False, scaled = False, convergence_at = 0.001) lpc_curve = lpc.lpc(X=line) #Plot results fig = plt.figure() ax = Axes3D(fig) labels = lpc._startPointsGenerator._meanShift.labels_ labels_unique = unique(labels) cluster_centers = lpc._startPointsGenerator._meanShift.cluster_centers_ n_clusters = len(labels_unique) colors = cycle('bgrcmyk') for k, col in zip(range(n_clusters), colors): cluster_members = labels == k cluster_center = cluster_centers[k] ax.scatter(line[cluster_members, 0], line[cluster_members, 1], line[cluster_members, 2], c = col, alpha = 0.1) ax.scatter([cluster_center[0]], [cluster_center[1]], [cluster_center[2]], c = 'b', marker= '^') curve = lpc_curve[k]['save_xd'] ax.plot(curve[:,0],curve[:,1],curve[:,2], c = col, linewidth = 3) plt.show()
def gaia(): gaia = array(readData('../resources/gaia.dat')) lpc = LPCImpl(start_points_generator = lpcMeanShift(), mult = 1, scaled = False) curve = lpc.lpc(X = gaia) fig = plt.figure() ax = Axes3D(fig) ax.scatter(gaia[:,0], gaia[:,1], gaia[:,2], alpha = 0.3) save_xd = curve[0]['save_xd'] ax.plot(save_xd[:,0], save_xd[:,1], save_xd[:,2])
def calSpeedFlow(): calspeedflow = array(readData('../resources/calspeedflow.dat')) print calspeedflow lpc = LPCImpl(start_points_generator = lpcMeanShift(ms_h = 0.1), mult = 1) lpc.lpc(X = calspeedflow) curve = lpc.getCurve(unscale = True) fig = plt.figure() plt.scatter(calspeedflow[:,0], calspeedflow[:,1], alpha = 0.3) save_xd = curve[0]['save_xd'] plt.plot(save_xd[:,0], save_xd[:,1])
def _initStartPoints(self): start_points_parameters = self._parser.getStartPointsParameters() if start_points_parameters['type'] == 'lpcMeanShift': self._start_points_generator = lpcMeanShift(**start_points_parameters['params']) else: raise ValueError, 'Specified type of start points generator is not recognised'
def testlpcMeanShift5(self): lpcMS = lpcMeanShift(ms_h = 1) sp = lpcMS(self._line_cluster_2_, n = None) print sp
def testlpcMeanShift4(self): print 'Generate start points based on ms_h: 0.5, ms_sub: 30, 1 explicitly defined cluster seed point and n = 10' lpcMS = lpcMeanShift( ms_h = 0.5) sp = lpcMS(self._line_cluster, x0 = [[1.5, 1.5, 1.5]] , n = 10) print sp
def testlpcMeanShift3(self): print 'Generate start points based on ms_h: 0.5, ms_sub: 30, n = 10 and no explicitly defined cluster seed points' lpcMS = lpcMeanShift( ms_h = 0.5) sp = lpcMS(self._line_cluster, n = 10) print sp
def testlpcMeanShift2(self): print 'Generate start points based on ms_h: 0.5, ms_sub: 30 and 1 explicitly defined cluster seed point ' lpcMS = lpcMeanShift( ms_h = 0.5) x0 = array([[1.5, 1.5, 1.5]]) sp = lpcMS(self._line_cluster, n = 10, x0 = x0) print sp
def testlpcMeanShift2Fail(self): print 'Generate start points based on ms_h: 0.5, ms_sub: 30 and 1 explicitly defined cluster seed point ' print 'Raises error, no nearest neighbour within bandwidth - empty array raises error in sklearn BallTree' lpcMS = lpcMeanShift( ms_h = 0.5) x0 = array([[0.5, 0.5, 0.5]]) self.assertRaises(ValueError, lpcMS, self._line_cluster, None, x0)