Example #1
0
def SCMC(encDist, newS, kPoints=3):
    numPoints = encDist.shape[0]  #训练点集的个数
    numQuery = newS.shape[0]  #根据构造可以得出查询点的维度
    decDist = np.zeros((numPoints, numQuery), dtype=object)

    # decDist = np.zeros((numPoints, 1),dtype=object)
    for i in range(numPoints):
        decDist[i] = mvhe.decrypt(newS, encDist[i])
    return knn(decDist.T, kPoints)
Example #2
0
def SCMC(encDist, newS, kPoints=3):
    numPoints = encDist.shape[0]
    decDist = np.zeros((numPoints, 1), dtype=object)
    for i in range(numPoints):
        decDist[i][0] = mvhe.decrypt(newS, encDist[i])
    return knn(decDist, kPoints)
Example #3
0
def vhe_kmeans(dataset, k):
    enc_dataset = data_enc(dataset)
    k_point = enc_generate_k(enc_dataset, k)
    assignments = enc_assign_points(enc_dataset, k_point)
    print(assignments)
    print(k_point)
    old_assignments = None
    num = [0 for i in range(k)]
    iteration = 0
    while assignments != old_assignments:
        interval_start = time.time()

        new_centers = enc_update_centers(enc_dataset, assignments, k)
        old_assignments = assignments
        assignments = enc_assign_points(enc_dataset, new_centers)
        iteration += 1

        interval_end = time.time()
        print(iteration, ' ', (interval_end - interval_start) / 60, 'min')

    dec_dataset = np.zeros((row, col), dtype=object)
    for i in range(row):
        dec_dataset[i] = mvhe.decrypt(S, enc_dataset[i])
        # print(dec_dataset[i])

    result = list(zip(assignments, dec_dataset))

    print(
        '\n\n---------------------------------分类结果---------------------------------------\n\n'
    )
    print('聚类中心:')
    new_centers = np.array(new_centers)
    dec_new_centers = np.zeros((k, col), dtype=object)
    for i in range(k):
        dec_new_centers[i] = mvhe.decrypt(S, new_centers[i])
    print(dec_new_centers)

    for j in range(k):
        for i in range(len(dataset)):
            if assignments[i] == j:
                num[j] += 1

    print('聚类点个数:')
    print(num)

    print('迭代次数:')
    print(iteration)

    res = open("vhe_clusters.txt", 'a+')
    print('聚类中心:', file=res)
    for i in range(k):
        print(dec_new_centers[i], file=res)
    print('\n\n')
    print('聚类点个数:', file=res)
    print(num, file=res)
    print('\n\n')
    print('迭代次数:', file=res)
    print(iteration, file=res)
    res.close()

    print(
        '\n\n---------------------------------标号简记---------------------------------------\n\n'
    )
    # for out in result:
    #     print(out, end='\n')
    print(
        '\n\n---------------------------------聚类结果---------------------------------------\n\n'
    )
    listResult = [[] for i in range(k)]
    count = 0
    for i in assignments:
        listResult[i].append(count)
        count = count + 1
    write_vhe_results(listResult, dec_dataset, k)
    for kind in range(k):
        print("第%d类数据有:" % (kind + 1))
        count = 0
        for j in listResult[kind]:
            # print(j, end=' ')
            count = count + 1
            # if count % 25 == 0:
            #     print('\n')
        print(count)
        print('\n')
    print(
        '\n\n--------------------------------------------------------------------------------\n\n'
    )