【问题标题】:How to cluster dataset with more than 3 D and interet/Visualize?如何对超过 3D 和 interet/Visualize 的数据集进行聚类?
【发布时间】:2016-10-21 09:23:06
【问题描述】:

举个例子 X: 1 2 3 4 5 Y: .9 .91 .92 .93 .94 Z: 20 36 999 211 M. 4000 3456 1 0

当我有这样的数据集时,选择哪种聚类算法?另外,如何解释聚类后的结果? 含义:如何将 4D 数据集馈送到集群中。

我发现 DBSCAN 可以在互联网上获得 2D 的情节是可能的。由于我的数据集是 4D 并且不合逻辑地变化...我不敢将其提供给算法

`

import pdb
import matplotlib.pyplot as plt
from numpy.random import rand
from numpy import square, sqrt
def regionQuery(P, eps, D): 
    neighbourPts = []
    for point in D:
        #print point        
        if sqrt(square(P[1] - point[1]) + square(P[2] - point[2]))<eps:
            neighbourPts.append(point)
    return neighbourPts
def DBSCAN(D, eps, MinPts):
    noise = []
    visited = []
    C = []
    c_n = -1
    for point in D:
        visited.append(point) #marking point as visited
    #   print point     
        neighbourPts = regionQuery(point, eps, D)
        if len(neighbourPts) < MinPts:
            noise.append(point)
        else:
            C.append([])            
            c_n+=1
            expandCluster(point, neighbourPts, C, c_n,eps, MinPts, D, visited)
    print("no. of clusters: " , len(C)  )
    print("length of noise:", len(noise))
    for cluster in C:
        col =[rand(1),rand(1),rand(1)]      
        #print(cluster)     
        plt.scatter([i[1] for i in cluster],[i[2] for i in cluster],color=col)
    plt.show()
def expandCluster(P, neighbourPts, C, c_n,eps, MinPts, D, visited):
    C[c_n].append(P)
    for point in neighbourPts:
        if point not in visited:
            visited.append(point) 
            neighbourPts_2 = regionQuery(point, eps, D)
            if len(neighbourPts_2) >= MinPts:
                neighbourPts += neighbourPts_2
        if point not in (i for i in C):
            C[c_n].append(point)
eps =20#input("enter eps")
x=200*rand(10)
y=200*rand(10)
l=[]
for i in range(10):
    l.append([i,x[i],y[i]])
#pdb.set_trace()
DBSCAN(l,eps,1)` 

【问题讨论】:

  • 您使用哪种语言?

标签: python machine-learning cluster-analysis dbscan


【解决方案1】:

如果您使用的是 Python:

方法一:

from sklearn.metrics import confusion_matrix as cm
import pandas as pd

y_test = [1, 0, 0]
y_pred = [1, 0, 0]
confusion_matrix=cm(y_test, y_pred)

list1 = ["Actual 0", "Actual 1"]
list2 = ["Predicted 0", "Predicted 1"]
pd.DataFrame(confusion_matrix, list1,list2)

方法二:

虽然 sklearn.metrics.confusion_matrix 提供了一个数字矩阵,但您可以使用以下内容生成“报告”:

import pandas as pd
y_true = pd.Series([2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2])
y_pred = pd.Series([0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2])

pd.crosstab(y_true, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)

导致:

Predicted  0  1  2  All
True                   
0          3  0  0    3
1          0  1  2    3
2          2  1  3    6
All        5  2  5   12

这让我们看到:

  1. 对角线元素显示每个类别的正确分类数:类别 0、1 和 2 分别为 3、1 和 3。
  2. 非对角线元素提供错误分类:例如,第 2 类中有 2 个被错误分类为 0,第 0 类中没有一个被错误分类为 2,等等。
  3. y_truey_pred 中每个类的分类总数,来自“全部”小计

这种方法也适用于文本标签,对于数据集中的大量样本,可以扩展提供百分比报告。

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2020-04-20
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2021-07-27
    相关资源
    最近更新 更多