Changeset 5035:a2fed2b12195 in orange


Ignore:
Timestamp:
07/30/08 21:36:19 (6 years ago)
Author:
janezd <janez.demsar@…>
Branch:
default
Convert:
3f24a1a68bfbeefd89e8d9cfabded0a4fbd3da13
Message:
  • fixed problems when the data contained too few different examples for the required number of clusters; the true problem is in the code Aleks machine-translated from Fortran to C, and I wouldn't want to go there, so I just check whether the clustering's results are sensible and report an error if they are not
File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/Unsupervised/OWKMeans.py

    r4983 r5035  
    6565 
    6666    def cluster(self): 
     67        self.error() 
    6768        if self.data: 
    6869            examples = [[float(x) for x in d] for d in self.data] 
    6970            self.mc = orngCluster.MClustering(examples, int(self.K), self.DistanceMeasure+1) 
    70             self.mc.medoids = [x-1 for x in self.mc.medoids] 
     71            # This fix is needed since orngCluster.MClustering does not report errors, 
     72            # and only returns erroneous results instead 
     73            if max(self.mc.mapping) > self.K or min(self.mc.cdisp) < 0: 
     74                self.error("Check whether your data contains enough distinct examples\nfor the desired number of clusters")   
     75                self.mc = None 
     76            else: 
     77                self.mc.medoids = [x-1 for x in self.mc.medoids] 
    7178        else: 
    7279            self.mc = None 
     
    8188            return 
    8289         
     90        actualK = self.K 
    8391        self.table.setColumnCount(4) 
    84         self.table.setRowCount(self.K+1) 
     92        self.table.setRowCount(actualK+1) 
    8593 
    8694        self.header = self.table.horizontalHeader() 
     
    8997            self.table.setHorizontalHeaderItem(i, QTableWidgetItem(h)) 
    9098 
    91         dist = [0] * self.K 
     99        dist = [0] * actualK 
    92100        for m in self.mc.mapping: 
    93101            dist[m-1] += 1 
    94102 
    95103        bic, cbic = self.compute_bic() 
    96         for k in range(self.K): 
     104        for k in range(actualK): 
    97105            self.table.setItem(k, 0, QTableWidgetItem(str(k+1))) 
    98106            self.table.setItem(k, 1, QTableWidgetItem(str(dist[k]))) 
    99107            self.table.setItem(k, 2, QTableWidgetItem("%5.3f" % self.mc.cdisp[k])) 
    100             self.table.setItem(k, 3, QTableWidgetItem("%6.2f" % cbic[k])) 
    101  
    102         colorItem(self.table, self.K, 0, "Total") 
    103         colorItem(self.table, self.K, 1, str(len(self.data))) 
    104         colorItem(self.table, self.K, 2, "%5.3f" % self.mc.disp) 
    105         colorItem(self.table, self.K, 3, "%6.2f" % bic) 
     108            self.table.setItem(k, 3, QTableWidgetItem(bic is None and u"\u221E" or ("%6.2f" % cbic[k]))) 
     109 
     110        colorItem(self.table, actualK, 0, "Total") 
     111        colorItem(self.table, actualK, 1, str(len(self.data))) 
     112        colorItem(self.table, actualK, 2, "%5.3f" % self.mc.disp) 
     113        colorItem(self.table, actualK, 3, bic is None and u"\u221E" or ("%6.2f" % bic)) 
    106114 
    107115        for i in range(4): 
     
    174182            s2 += sum( [(float(x[i]) - float(medoid[i]))**2 for i in cidx] ) 
    175183        s2 /= (R - K) 
     184        if s2 < 1e-20: 
     185            return None, [None]*K 
    176186        # log-lokehood of clusters: l(Dn) 
    177187        # log-likehood of clustering: l(D) 
     
    179189        bicc = [] 
    180190        for k in range(1, 1+K): 
    181             print Ri[k], M, K, R, k 
    182191            ldn = -1. * Ri[k] * ((math.log(2. * math.pi, 2) / -2.) - (M * math.log(s2, 2) / 2.) + (K / 2.) + math.log(Ri[k], 2) - math.log(R, 2)) 
    183192            ld += ldn 
     
    190199    def __init__(self, table, i, j, text, flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable, color=Qt.lightGray): 
    191200        self.color = color 
    192         QTableWidgetItem.__init__(self, str(text)) 
     201        QTableWidgetItem.__init__(self, unicode(text)) 
    193202        self.setFlags(flags) 
    194203        table.setItem(i, j, self) 
Note: See TracChangeset for help on using the changeset viewer.