def test_distance(self): point1 = (20, 30, 40) point2 = (20, 30, 40) point3 = (250, 240, 230) print "Running test 1.a" assert kmeans.distance(point1, point2) == 0 print "Running test 1.b" assert kmeans.distance(point1, point3) == 133100
def nearest(point, cluster_centers): min_dist = FLOAT_MAX m = np.shape(cluster_centers)[0] # 当前已经初始化的聚类中心的个数 for i in range(m): # 计算point与每个聚类中心之间的距离 d = distance(point, cluster_centers[ i, ]) #计算当前点与已经初始化的每个点的距离,并且得到最小值,即假设该点与初始化的第二个簇中心最短 # 选择最短距离 if min_dist > d: min_dist = d return min_dist
def nearest(point, cluster_centers): ''' 计算point和cluster_centers之间的最小距离 input: point(mat):当前的样本点 cluster_centers(mat):当前已经初始化的聚类中心 output: min_dist(float):点point和当前的聚类中心之间的最短距离 ''' min_dist = FLOAT_MAX m = np.shape(cluster_centers)[0] # 当前已经初始化的聚类中心的个数 for i in range(m): # 计算point与每个聚类中心之间的距离 d = distance(point, cluster_centers[i, ]) # 选择最短距离 if min_dist > d: min_dist = d return min_dist
def generate_path(probability_matrix, state_prototypes, action_prototypes, state_indices, iterations): state_index = random.sample(state_indices, 1)[0] # select random point path = [] current_position = state_prototypes[state_index] # choose closest state prototype for i in range(iterations): path.append(current_position) # following should not be the case but who knows what is going on with 2nd map if np.sum(probability_matrix[state_index]) == 0: best_action_index = np.argmax((probability_matrix[state_index])) else: # choose best action using matrix of joint probabilities best_action_index = np.argmax((probability_matrix[state_index] / np.sum(probability_matrix[state_index]))) best_action = action_prototypes[best_action_index] current_position = current_position + best_action dist = [kmeans.distance(current_position, k) for k in state_prototypes] min_index, = np.where(dist == np.min(dist)) # choose closest prototype state_index = min_index[0] return path
def problem2_4_4(): data, featureNames = load_data() k = 6 iniCenters = orderedCenters(data, k) clusters, centers = kmeans(data, k, initialCenters=iniCenters) avgAll = avg(data.keys(), data) print "average: %s" % avgAll print "########################" print "cluster # -> (distance from average)" print " [# in cluster]:[items in cluster]" print "" for c in clusters: print "cluster %d -> %s: " % (c, distance(avgAll, centers[c])) print " [%d]:%s " % (len(clusters[c]), clusters[c]) print ""
def generate_path(probability_matrix, state_prototypes, action_prototypes, state_indices, iterations): state_index = random.sample(state_indices, 1)[0] # select random point path = [] current_position = state_prototypes[ state_index] # choose closest state prototype for i in range(iterations): path.append(current_position) # following should not be the case but who knows what is going on with 2nd map if np.sum(probability_matrix[state_index]) == 0: best_action_index = np.argmax((probability_matrix[state_index])) else: # choose best action using matrix of joint probabilities best_action_index = np.argmax( (probability_matrix[state_index] / np.sum(probability_matrix[state_index]))) best_action = action_prototypes[best_action_index] current_position = current_position + best_action dist = [kmeans.distance(current_position, k) for k in state_prototypes] min_index, = np.where(dist == np.min(dist)) # choose closest prototype state_index = min_index[0] return path
def test_distance(self, two_dimensional_points, three_dimensional_points): assert distance(*two_dimensional_points) > 7.06 assert distance(*two_dimensional_points) < 7.08
def test_distance(self): sample1 = [0.074936026, 0.0683657238] center1 = [0.079113924099999997, 0.065822784800000006] dist = distance(sample1, center1) self.assertAlmostEqual(dist, 0.00489095)
def rbfMult(x, c, s): return (1 + (km.distance(x, c))**2)**0.5
def rbfGaus(x, c, s): return np.exp(-1 / (2 * s**2) * (km.distance(x, c))**2)
def rbfMultIn(x, c, s): return 1 / (1 + (km.distance(x, c))**2)**0.5