attributes = list() attributes.append(nextId) while len(attributes) < d+1: meth = int(rand.normalvariate(normMean, normStdev)) #random number, 0-100 attributes.append(meth) nextStudent = tuple(attributes) dataset.append(nextStudent) nextId += 1 #sort the dataset by intelligence randomCopy = cp.deepcopy(dataset) dataset = sorted(dataset, key=lambda student: student[1]) originalCopy = cp.deepcopy(dataset) l1 = DP.cluster1D(dataset, size, 1, p) clusters = list() clusters.append(l1) dim = 2 while dim <= d: dataset = sorted(dataset, key=lambda student: student[dim]) l2 = DP.cluster1D(dataset, size, dim, p) clusters.append(l2) dim += 1 ##################################### # Cluster Ranking # # #
def HVC(X, p): n = len(X) l = n/p d = len(X[0]) clusters = [] for i in range(1, d): s = sorted(X, key=lambda x: x[i]) c = dp.cluster1D(s, len(s), i, p) clusters.append(c) #build relationship matrix #empty matrix [n, n] F = np.zeros((n, n)) #for each 1-d clustering for y in clusters: idx = 0 #take each cluster, with factor higher for earlier clusters while idx < len(y): c1 = y[idx] factor = len(y) - math.pow(6, idx) if factor < 0: factor = 0 #and for each pair in the cluster i1 = 0 while i1 < len(c1)-1: i2 = i1+1 while i2 < len(c1): #add the factor of the cluster to their partnership F[x, q] F[c1[i1][0], c1[i2][0]] += factor/(len(clusters)*p) F[c1[i2][0], c1[i1][0]] += factor/(len(clusters)*p) i2+=1 i1+=1 idx+=1 print F X = sorted(X, key=lambda student: student[0]) clusters = list() while len(clusters) < l: clusters.append(list()) first_round = True while len(clusters[l-1]) < p: #print "next round" for i in clusters: if first_round: while len(i) < 2: next_idx = rand.randint(0,n-1) if F[next_idx, 0] != -1: i.append(X[next_idx]) F[next_idx, :] = -1 else: maxIdx = 0 maxVal = 0 index = 0 while index < min(len(i), 2): test = i[index][0] possIdx = 0 for j in range(0, len(F[0])): # if possIdx[1] == test: # #F[possIdx[1], test] = -1 # possIdx = np.unravel_index(np.argmax(F[:, test]), (n,n)) possMax = F[j, test] if possMax >= maxVal: maxIdx = j maxVal = possMax index += 1 i.append(X[maxIdx]) F[maxIdx, :] = -1 first_round = False return clusters