Example #1
0
    def myCluster(self, str_clu, str_iter, str_thres):
        try:
            self.right_tableview.itemChanged.disconnect(
                self.item_Changed_Event)  # 断开连接,不然会很费时间
        except Exception:
            pass

        try:
            if str_clu != '':
                num_clu = int(str_clu)
            else:
                num_clu = 2
            if str_iter != '':
                num_iter = int(str_iter)
            else:
                num_iter = 500
            if str_thres != '':
                num_thres = float(str_thres)
            else:
                num_thres = 0.0001
        except Exception:
            QtWidgets.QMessageBox.critical(self, "错误", "输入错误")
            return

        kmeans = K_means.Kmeans(num_clu, num_iter, num_thres)

        self.SelectedToDf()
        if self.selectedDf is None:
            return
        if not self.selectedDf.empty:
            if self.selectedDf.T.shape[1] < 2:
                QtWidgets.QMessageBox.critical(self, "错误", "输入向量小于2维,无法聚类")
                return
            try:
                print(self.selectedDf)
                col = self.selectedDf.T.shape[1]
                data_X = self.selectedDf.T.iloc[:, :col].values
                predict_y = kmeans.predict(data_X)
                self.right_tableview.insertColumn(
                    self.right_tableview.columnCount())
                for i in range(len(predict_y)):
                    Input_item = QtWidgets.QTableWidgetItem(str(predict_y[i]))
                    Input_item.setTextAlignment(Qt.AlignHCenter
                                                | Qt.AlignVCenter)
                    self.right_tableview.setItem(
                        i,
                        self.right_tableview.columnCount() - 1, Input_item)
                temp_x = pd.DataFrame(data=data_X)
                temp_y = pd.Series(data=predict_y)
                temp_x["Predict"] = temp_y

                if self.selectedDf.T.shape[1] == 2:
                    ax = self.ClusterWindow.plot_widget.add_subplot(111)
                    for i in range(num_clu):
                        ax.scatter(x=temp_x[temp_x.Predict == i].iloc[:, 0],
                                   y=temp_x[temp_x.Predict == i].iloc[:, 1])

                elif self.selectedDf.T.shape[1] >= 3:
                    ax = Axes3D(self.ClusterWindow.plot_widget)
                    for i in range(num_clu):
                        ax.scatter(temp_x[temp_x.Predict == i].iloc[:, 0],
                                   temp_x[temp_x.Predict == i].iloc[:, 1],
                                   temp_x[temp_x.Predict == i].iloc[:, 2])
            except Exception:
                QtWidgets.QMessageBox.critical(self, "错误", "聚类失败,请检查输入数据的合法性")
                return
            try:
                self.item_Changed_Event()
                self.right_tableview.itemChanged.connect(
                    self.item_Changed_Event)  # 断开连接,不然会很费时间
            except Exception:
                pass
            self.ClusterWindow.plot_canvas.draw()
Example #2
0
        if clust is None or len(clust) <= 1:
            count1 += 1
        else:
            count2 += 1
    return (count1 / len(clusters)) * 100


#===========Main========================
_data = Mini_Parse("stars.txt")

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# We create an object of the Kmeans class and we put the data in it
kmeans = km.Kmeans(_data)
#The input defines the number of centroids and it calculates them
means = kmeans.CalculateMeans(15)
#Based on the means or centroids it creates the clusters
clusters = kmeans.FindClusters()

hullx = []
hully = []
hullz = []

# Call for each cluster the Jarvis March Algorithm
for i in range(len(clusters)):
    if (len(clusters[i])) <= 1:
        continue
    else:
        temp = d3ch.JarvisMarch(clusters[i])
    for idx in range(nP):
        # The random value to be added to the gene.
        for i in range(length):
            random_value = np.random.uniform(0.0, 1.0)
            cluster.__dict__['populasi'][i][idx][3] + random_value

    return cluster


def getCentroidPop(i):
    cluster.centroids = cluster.populasi[i]


k = 3
fitness = []
cluster = K_means.Kmeans('seeds.txt', k)

#------------------------- POPULATION, CHROMOSOME, CLUSTERING -------------------------#
createPopulation()

for i in range(0, 70):
    getCentroidPop(i)  # ngambil centroid d masing2 populasi
    cluster.clustering()
    centroid, SSE, acc = cluster.groupData()
    result = centroid, SSE, acc
    fitness.append(result)
    # per chromosome->centroid punya fitness function
    # print("SSE chromosome: %.2f" % SSE)
    # print("Acc chromosome: %.2f" % acc)

#--------------------------------- SELECTION ----------------------------------#