import numpy as np
import matplotlib.pyplot as plt
# 选择聚类方法：clique 类
from pyclustering.cluster.clique import clique
# clique 可视化
from pyclustering.cluster.clique import clique_visualizer
import time
from collections import Counter



def loadDataSet(filename):
    data=[]
    fr=open(filename)
    for line in fr.readlines():
        curLine=line.strip('\n').split(' ')
        fltLine=list(map(float,curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M

data_M = loadDataSet('five_cluster.txt')
TestData = data_M[:,[1,2]]
g_truth = data_M[:,0]
# 创建 CLIQUE 算法进行处理
# 定义每个维度中网格单元的数量
intervals = 15
# 密度阈值
threshold = 10
clique_instance = clique(TestData, intervals, threshold)

'''
five_cluster.txt  intervals = 15,threshold = 10  0.812    t=0.039999961853027344
spiral.txt no
ThreeCircles.txt  intervals = 20,threshold = 1  0.990563419372745   t=0.0800008773803711
Twomoons.txt  intervals = 15,threshold = 1    0.9880159786950732   t=0.04000043869018555
'''


# 开始聚类过程并获得结果
t1 = time.time()
clique_instance.process()
t2 = time.time()
clique_cluster = clique_instance.get_clusters()  # allocated clusters
# 被认为是异常值的点（噪点）
noise = clique_instance.get_noise()
# CLIQUE形成的网格单元
cells = clique_instance.get_cells()

#print(clique_cluster[3])

zeros = [0]*len(g_truth)

for each in clique_cluster[0]:
    zeros[each] = 3
for each in clique_cluster[1]:
    zeros[each] = 2
for each in clique_cluster[2]:
    zeros[each] = 4
for each in clique_cluster[3]:
    zeros[each] = 5
for each in clique_cluster[4]:
    zeros[each] = 1
g_answer = zeros

#print(g_answer)
sum = 0
for i in range(len(g_truth)):
    if g_answer[i] == g_truth[i]:
        sum+=1

rate = sum/len(g_truth)

print(rate)

#print(g_truth)
#print(g_answer)

print("Amount of clusters:", len(clique_cluster))


print(clique_cluster)
print(t2-t1)   #运行时间
# 显示由算法形成的网格
clique_visualizer.show_grid(cells, TestData)
# 显示聚类结果
clique_visualizer.show_clusters(TestData, clique_cluster, noise)  # show clustering results

d2175c430b778e8c9dbbf8f503819d3ae5060c44
<mask token>


def loadDataSet(filename):
    data = []
    fr = open(filename)
    for line in fr.readlines():
        curLine = line.strip('\n').split(' ')
        fltLine = list(map(float, curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M


<mask token>

<mask token>


def loadDataSet(filename):
    data = []
    fr = open(filename)
    for line in fr.readlines():
        curLine = line.strip('\n').split(' ')
        fltLine = list(map(float, curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M


<mask token>
clique_instance.process()
<mask token>
for each in clique_cluster[0]:
    zeros[each] = 3
for each in clique_cluster[1]:
    zeros[each] = 2
for each in clique_cluster[2]:
    zeros[each] = 4
for each in clique_cluster[3]:
    zeros[each] = 5
for each in clique_cluster[4]:
    zeros[each] = 1
<mask token>
for i in range(len(g_truth)):
    if g_answer[i] == g_truth[i]:
        sum += 1
<mask token>
print(rate)
print('Amount of clusters:', len(clique_cluster))
print(clique_cluster)
print(t2 - t1)
clique_visualizer.show_grid(cells, TestData)
clique_visualizer.show_clusters(TestData, clique_cluster, noise)

<mask token>


def loadDataSet(filename):
    data = []
    fr = open(filename)
    for line in fr.readlines():
        curLine = line.strip('\n').split(' ')
        fltLine = list(map(float, curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M


data_M = loadDataSet('five_cluster.txt')
TestData = data_M[:, [1, 2]]
g_truth = data_M[:, 0]
intervals = 15
threshold = 10
clique_instance = clique(TestData, intervals, threshold)
<mask token>
t1 = time.time()
clique_instance.process()
t2 = time.time()
clique_cluster = clique_instance.get_clusters()
noise = clique_instance.get_noise()
cells = clique_instance.get_cells()
zeros = [0] * len(g_truth)
for each in clique_cluster[0]:
    zeros[each] = 3
for each in clique_cluster[1]:
    zeros[each] = 2
for each in clique_cluster[2]:
    zeros[each] = 4
for each in clique_cluster[3]:
    zeros[each] = 5
for each in clique_cluster[4]:
    zeros[each] = 1
g_answer = zeros
sum = 0
for i in range(len(g_truth)):
    if g_answer[i] == g_truth[i]:
        sum += 1
rate = sum / len(g_truth)
print(rate)
print('Amount of clusters:', len(clique_cluster))
print(clique_cluster)
print(t2 - t1)
clique_visualizer.show_grid(cells, TestData)
clique_visualizer.show_clusters(TestData, clique_cluster, noise)

import numpy as np
import matplotlib.pyplot as plt
from pyclustering.cluster.clique import clique
from pyclustering.cluster.clique import clique_visualizer
import time
from collections import Counter


def loadDataSet(filename):
    data = []
    fr = open(filename)
    for line in fr.readlines():
        curLine = line.strip('\n').split(' ')
        fltLine = list(map(float, curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M


data_M = loadDataSet('five_cluster.txt')
TestData = data_M[:, [1, 2]]
g_truth = data_M[:, 0]
intervals = 15
threshold = 10
clique_instance = clique(TestData, intervals, threshold)
<mask token>
t1 = time.time()
clique_instance.process()
t2 = time.time()
clique_cluster = clique_instance.get_clusters()
noise = clique_instance.get_noise()
cells = clique_instance.get_cells()
zeros = [0] * len(g_truth)
for each in clique_cluster[0]:
    zeros[each] = 3
for each in clique_cluster[1]:
    zeros[each] = 2
for each in clique_cluster[2]:
    zeros[each] = 4
for each in clique_cluster[3]:
    zeros[each] = 5
for each in clique_cluster[4]:
    zeros[each] = 1
g_answer = zeros
sum = 0
for i in range(len(g_truth)):
    if g_answer[i] == g_truth[i]:
        sum += 1
rate = sum / len(g_truth)
print(rate)
print('Amount of clusters:', len(clique_cluster))
print(clique_cluster)
print(t2 - t1)
clique_visualizer.show_grid(cells, TestData)
clique_visualizer.show_clusters(TestData, clique_cluster, noise)

import numpy as np
import matplotlib.pyplot as plt
# 选择聚类方法：clique 类
from pyclustering.cluster.clique import clique
# clique 可视化
from pyclustering.cluster.clique import clique_visualizer
import time
from collections import Counter



def loadDataSet(filename):
    data=[]
    fr=open(filename)
    for line in fr.readlines():
        curLine=line.strip('\n').split(' ')
        fltLine=list(map(float,curLine))
        data.append(fltLine)
    data_M = np.array(data)
    return data_M

data_M = loadDataSet('five_cluster.txt')
TestData = data_M[:,[1,2]]
g_truth = data_M[:,0]
# 创建 CLIQUE 算法进行处理
# 定义每个维度中网格单元的数量
intervals = 15
# 密度阈值
threshold = 10
clique_instance = clique(TestData, intervals, threshold)

'''
five_cluster.txt  intervals = 15,threshold = 10  0.812    t=0.039999961853027344
spiral.txt no
ThreeCircles.txt  intervals = 20,threshold = 1  0.990563419372745   t=0.0800008773803711
Twomoons.txt  intervals = 15,threshold = 1    0.9880159786950732   t=0.04000043869018555
'''


# 开始聚类过程并获得结果
t1 = time.time()
clique_instance.process()
t2 = time.time()
clique_cluster = clique_instance.get_clusters()  # allocated clusters
# 被认为是异常值的点（噪点）
noise = clique_instance.get_noise()
# CLIQUE形成的网格单元
cells = clique_instance.get_cells()

#print(clique_cluster[3])

zeros = [0]*len(g_truth)

for each in clique_cluster[0]:
    zeros[each] = 3
for each in clique_cluster[1]:
    zeros[each] = 2
for each in clique_cluster[2]:
    zeros[each] = 4
for each in clique_cluster[3]:
    zeros[each] = 5
for each in clique_cluster[4]:
    zeros[each] = 1
g_answer = zeros

#print(g_answer)
sum = 0
for i in range(len(g_truth)):
    if g_answer[i] == g_truth[i]:
        sum+=1

rate = sum/len(g_truth)

print(rate)

#print(g_truth)
#print(g_answer)

print("Amount of clusters:", len(clique_cluster))


print(clique_cluster)
print(t2-t1)   #运行时间
# 显示由算法形成的网格
clique_visualizer.show_grid(cells, TestData)
# 显示聚类结果
clique_visualizer.show_clusters(TestData, clique_cluster, noise)  # show clustering results
