Esempio n. 1
0
def bestKElbow(X, options):
    K = 2
    finish = 10
    kmeans_list = []
    fits = []
    porcentage = []
    tolerance = 10  # 10% de tolerancia
    finished = False
    best_k = 0

    # kmeans con k=2
    kmeans = km.KMeans(X, K, options)
    kmeans_list.append(kmeans)
    fits.append(kmeans.fitting())

    K += 1

    # kmeans con k=3
    kmeans = km.KMeans(X, K, options)
    kmeans_list.append(kmeans)
    fits.append(kmeans.fitting())
    K += 1

    # Guardamos la resta y le asignamos un porcentage de 100%
    porcentage.append((fits[0] - fits[1], 100))

    while K <= finish and not finished:
        best_k = 0
        kmeans = km.KMeans(X, K, options)
        kmeans_list.append(kmeans)
        fits.append(kmeans.fitting())
        K += 1

        # restamos el penultimo con el antepenultimo (ultimos 2 kmeans calculados)
        resta = fits[-2] - fits[-1]
        # calculamos el porcentage respecto a la primera resta
        first_resta = porcentage[0][0]
        if first_resta == 0:
            first_resta = 0.000000000001
        porcentage.append((resta, resta * 100 / first_resta))

        # si las 2 ultimos porcentajes estan por debajo de la tolerancia cogemos la k anterior a estos 2
        if (porcentage[-2][1] < tolerance and porcentage[-1][1] < tolerance):
            # print "por telerancia ssale"
            finished = True
            # le kito por el valor k empieza la k y las 2 k me paso
            best_k = K - 2 - 2 - 1

    # Si no se ha cumplido el factor de tolerancia buscamos el % mas grande de caida y elegimos su segunda K
    if best_k == 0:
        max_porcentage = porcentage[1][1]
        porcentage_pos = 1
        for pos, x in enumerate(porcentage[2:]):
            if x[1] > max_porcentage:
                max_porcentage = x[1]
                porcentage_pos = pos

        best_k = porcentage_pos

    return kmeans_list[best_k]
Esempio n. 2
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """

    #########################################################
    ##  YOU MUST ADAPT THE CODE IN THIS FUNCTIONS TO:
    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    ##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    ##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    #########################################################

    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    if options['colorspace'].lower() == 'ColorNaming'.lower():
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'].lower() == 'RGB'.lower():
        pass
        # im = np.reshape(im, (-1, im.shape[2]))
    elif options['colorspace'].lower() == 'Lab'.lower():
        im = color.rgb2lab(im)
    elif options['colorspace'].lower() == 'HSV'.lower():
        im = color.rgb2hsv(im)

    ##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    if options['K'] < 2:
        kmeans = km.KMeans(im, 0, options)
        kmeans.bestK()
    else:
        kmeans = km.KMeans(im, options['K'], options)
        kmeans.run()

    ##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    if options['colorspace'].lower() == 'RGB'.lower():
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    elif options['colorspace'].lower() == 'Lab'.lower():
        kmeans.centroids = kmeans.centroids[:, newaxis, :]
        kmeans.centroids = color.lab2rgb(kmeans.centroids) * 255.0
        kmeans.centroids = np.reshape(
            kmeans.centroids,
            (kmeans.centroids.shape[0], kmeans.centroids.shape[2]))
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    elif options['colorspace'].lower() == 'HSV'.lower():
        kmeans.centroids = kmeans.centroids[:, newaxis, :]
        kmeans.centroids = color.hsv2rgb(kmeans.centroids)
        kmeans.centroids = np.reshape(
            kmeans.centroids,
            (kmeans.centroids.shape[0], kmeans.centroids.shape[2]))
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    colors, which = getLabels(kmeans, options)
    return colors, which, kmeans
Esempio n. 3
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """

    #########################################################
    ##  YOU MUST ADAPT THE CODE IN THIS FUNCTIONS TO:
    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    ##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    ##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    #########################################################

    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    if options['colorspace'] == 'ColorNaming':
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'] == 'Lab':
        im = color.rgb2lab(im)
    #No fa falta RGB per que no s'ha de transformar res

    img = rescale(im, 1, preserve_range=True)  #Reescalat per que si no explota
    img = np.reshape(img, (-1, img.shape[2]))

    ##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    if options['K'] < 2:  # find the best K
        kmeans = km.KMeans(im, 0, options)
        kmeans.bestK()
    else:
        kmeans = km.KMeans(img, options['K'], options)
        kmeans.run()

##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    if options['colorspace'] != 'ColorNaming':
        kmeans.centroids = np.reshape(
            kmeans.centroids,
            (-1, 1, kmeans.centroids.shape[1]
             ))  #Un altre reshape per que si no falla el test amb Lab o RGB
        if options['colorspace'] == 'Lab':
            kmeans.centroids = color.lab2rgb(kmeans.centroids) * 255  #El extra
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)
        kmeans.centroids = np.reshape(kmeans.centroids,
                                      (-1, kmeans.centroids.shape[2]))

#########################################################
##  THE FOLLOWING 2 END LINES SHOULD BE KEPT UNMODIFIED
#########################################################
    colors, which = getLabels(kmeans, options)
    return colors, which, kmeans
Esempio n. 4
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """

    #########################################################
    ##  YOU MUST ADAPT THE CODE IN THIS FUNCTIONS TO:
    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    ##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    ##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    #########################################################
    #im = im.astype('uint8')
    ##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    if options['colorspace'].lower() == 'ColorNaming'.lower():
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'].lower() == 'RGB'.lower():
        pass  #im = color.convert_colorspace(im, 'RGB', options['colorspace'])
    elif options['colorspace'].lower() == 'Lab'.lower():
        im = im.astype('float64')
        im = color.rgb2lab(im / 255)
    elif options['colorspace'].lower() == 'HSV'.lower():
        im = color.rgb2hsv(im.astype('uint8'))

##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    if options['K'] < 2:  # find the best K
        kmeans = km.KMeans(im, 0, options)
        kmeans.bestK()
    else:
        kmeans = km.KMeans(im, options['K'], options)
        kmeans.run()

##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    if options['colorspace'].lower() == 'RGB'.lower():
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    elif options['colorspace'].lower() == 'Lab'.lower():
        kmeans.centroids = color.lab2rgb([kmeans.centroids])[0] * 255
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)
    elif options['colorspace'].lower() == 'HSV'.lower():
        kmeans.centroids = color.hsv2rgb([kmeans.centroids])[0]
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

#########################################################
##  THE FOLLOWING 2 END LINES SHOULD BE KEPT UNMODIFIED
#########################################################
    colors, which = getLabels(kmeans, options)
    return colors, which, kmeans
Esempio n. 5
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """
    """
    Aquesta Ă©s la funciĂł principal en la que es basa tot el projecte. 
    Aqui s'ha de transformar la imatge donada a un espai de tres dimensions per
    tal que es pugui treballar amb el nostre algorisme K-Means.
    Després es troben les etiquetes de color a partir dels centroids i es determina
    el percentatge d'èxit que ha tingut la nostra execució.
    """

    ##  1- Canviem l'espai de color donat per a que funcioni amb el nostre algorisme.
    if options['colorspace'].lower() == 'ColorNaming'.lower():
        im = cn.ImColorNamingTSELabDescriptor(im)

    elif options['colorspace'].lower() == 'RGB'.lower():
        pass

    elif options['colorspace'].lower() == 'Lab'.lower():
        im = color.rgb2lab(im)

##  2- Analitzem el parĂ metre K per determinar si hem o no d'executar el bestK o no.
##      Com que nosaltres fem la crida si i només si K=0, en cas que sigui K=1, executem bestK.

    if options['K'] < 2:
        kmeans = km.KMeans(im, 0, options)
    else:
        kmeans = km.KMeans(im, options['K'], options)

    kmeans.run()

    ##  3- Obtenim les etiquetes dels colors de la nostra imatge.
    if options['colorspace'].lower() == 'Lab'.lower():
        kmeans.centroids = color.lab2rgb([kmeans.centroids])[0] * 255
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    elif options['colorspace'].lower() == 'RGB'.lower():
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

#########################################################
##  THE FOLLOWING 2 END LINES SHOULD BE KEPT UNMODIFIED
#########################################################
    colors, which = getLabels(kmeans, options)
    return colors, which, kmeans
Esempio n. 6
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """

#########################################################
##  YOU MUST ADAPT THE CODE IN THIS FUNCTIONS TO:
##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
#########################################################

##  1- CHANGE THE IMAGE TO THE CORRESPONDING COLOR SPACE FOR KMEANS
    if options['colorspace'].lower() == 'ColorNaming'.lower():  
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'].lower() == 'RGB'.lower():        
        pass  #Ya estamos en RGB
    elif options['colorspace'].lower() == 'Lab'.lower():        
        im = color.rgb2lab(im)
        im = np.array(im).reshape((-1,3))


##  2- APPLY KMEANS ACCORDING TO 'OPTIONS' PARAMETER
    if options['K']<2: # find the bes K
        kmeans = km.KMeans(im, 0, options)
        kmeans.bestK() #bestK segons fitting
    else:
        kmeans = km.KMeans(im, options['K'], options) 
        kmeans.run()

##  3- GET THE NAME LABELS DETECTED ON THE 11 DIMENSIONAL SPACE
    if options['colorspace'].lower() == 'RGB'.lower():        
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)
    elif options['colorspace'].lower() == 'Lab'.lower():
        pass
        

#########################################################
##  THE FOLLOWING 2 END LINES SHOULD BE KEPT UNMODIFIED
#########################################################
    colors, which = getLabels(kmeans, options)   
    return colors, which, kmeans
Esempio n. 7
0
 def __init__(self, n_clusters=40):
     self.n_clusters = n_clusters
     self.kmeans_obj = KMeans(n_clusters=n_clusters)
     self.kmeans_ret = None
     self.descriptor_vstack = None
     self.mega_histogram = None
     self.clf = GaussianNB()
Esempio n. 8
0
def main():
    try:
        graphyboi = GraphController()
        graphyboi.update()
        kmeansyboi = KMeans(graphyboi.coords)
    except:
        print("Nothing in coords")
Esempio n. 9
0
def processImage(im, options):
    """@brief   Finds the colors present on the input image
    
    @param  im      LIST    input image
    @param  options DICTIONARY  dictionary with options
    
    @return colors  LIST    colors of centroids of kmeans object
    @return indexes LIST    indexes of centroids with the same label
    @return kmeans  KMeans  object of the class KMeans
    """

    if options['colorspace'].lower() == 'ColorNaming'.lower():
        im = cn.ImColorNamingTSELabDescriptor(im)

    elif options['colorspace'].lower() == 'RGB'.lower():
        pass
    elif options['colorspace'].lower() == 'Lab'.lower():
        im = im.astype('float64')
        im = color.rgb2lab(im / 255)

    kmeansAlgorithm = km.KMeans(im, options['K'], options)
    kmeansAlgorithm.run()

    if options['colorspace'].lower() == 'RGB'.lower():
        kmeansAlgorithm.centroids = cn.ImColorNamingTSELabDescriptor(
            kmeansAlgorithm.centroids)

    elif options['colorspace'].lower() == 'Lab'.lower():
        kmeansAlgorithm.centroids = color.lab2rgb([kmeansAlgorithm.centroids
                                                   ])[0] * 255
        kmeansAlgorithm.centroids = cn.ImColorNamingTSELabDescriptor(
            kmeansAlgorithm.centroids)

    colors_obt, which_obt = getLabels(kmeansAlgorithm, options)
    return colors_obt, which_obt, kmeansAlgorithm
Esempio n. 10
0
    def KMeans(self, X):
        kmeans = KMeans(n_clusters=2, random_state=0).fit(X)

        kmeans.labels_
        kmeans.predict([[0, 0], [4, 4]])
        kmeans.cluster_centers_

        return kmeans
Esempio n. 11
0
    def find(self, scores):
        scale = 100
        data = np.mat(scores).T * scale
        indices, distances, center = KMeans(
            lambda X, k: np.mat([X.min(), X.mean(), X.max()]).T).clustering(
                data, 3, 1)
        print("anomaly score centers:{0}".format(center.T))

        checkValue = center[2, 0]
        minCheckValue = self._minCheckValue * scale
        maxCheckValue = self._maxCheckValue * scale
        defaultThreshold = self._defaultThreshold * scale
        minValue = data[(indices == 2).A.flatten(), :].min(0)[0, 0]
        maxValue = data[(indices == 2).A.flatten(), :].max(0)[0, 0]

        if maxValue <= defaultThreshold:
            return defaultThreshold / scale

        if checkValue >= defaultThreshold:
            checkValue = (minValue + checkValue) / 2
        elif checkValue <= minCheckValue:
            checkValue = (checkValue + maxValue) / 2
        if checkValue < minCheckValue:
            checkValue = minCheckValue
        elif checkValue > maxCheckValue:
            checkValue = maxCheckValue
        print("threshold check value: {0}".format(checkValue))

        i = None
        for j in range(0, data.shape[0]):
            if data[j, 0] >= checkValue and i is None:
                i = j

            if data[j, 0] < checkValue and i is not None:
                if j - i > CurvesThresholdFinder.MIN_SAMPLES_NUMBER * 2:
                    x, y = self._fit(data[i:j, 0])

                    if self.__showPlot:
                        plt.figure(1, (16, 10))
                        plt.plot(list(range(0, j - i)),
                                 data[i:j, 0].A.flatten().tolist(),
                                 color="b",
                                 marker="x")
                        if x is not None and y is not None:
                            plt.plot(x, y, color="r")
                        plt.show()

                i = None
        print("threshold all values: {0}".format(self._values))

        threshold = (np.mean(self._values)
                     if len(self._values) > 0 else defaultThreshold) / scale
        print("threshold found: {0}".format(threshold))

        self.__reset()

        return threshold
Esempio n. 12
0
 def ReDo(self, actions, eegs):
     self.db = []
     if self.data != []:
         for r in range(len(self.data)):
             t = []
             for c in range(len(self.data[r]) - 1):
                 t.append(self.data[r][c])
             self.db.append(t)
         self.k = KMeans(self.db, actions[0], actions[1], actions[2],
                         actions[3])
         self.viewButton.Enable()
Esempio n. 13
0
def get_cluster():
    cluster_list = dict()
    for k in range(3, 9):
        k_temp = KMeans.KMeans(k)
        for j in range(2010, 2013):
            print("Fitting, k=", k, "Year=", j)
            k_temp.fit(data2test[j])
            s_key = str(k) + "_" + str(j)
            cluster_list[s_key] = k_temp.cluster

    return cluster_list
def processImage(im, options):  # NO TOCAR
    im = rescale(im, 0.25, preserve_range=True)

    if options['colorspace'] == 'ColorNaming':  # NO TOCAR
        im = np.reshape(im, (-1, im.shape[2]))
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'] == 'RGB':  # NO TOCAR
        im = np.reshape(im, (-1, im.shape[2]))
    elif options['colorspace'] == 'Lab':  # NO TOCAR
        im = cn.RGB2Lab(im)
        im = np.reshape(im, (-1, im.shape[2]))

    if options['K'] < 2:  # trobar la millor K
        i = 2
        fitting = []
        for i in range(2, 15):
            kmeans = km.KMeans(im, i, options)
            fitting.append(kmeans.fitting())
        print "k optima = ", fitting.index(min(fitting)) + 3
        kmeans = km.KMeans(im, fitting.index(min(fitting)) + 3, options)
    else:
        kmeans = km.KMeans(im, options['K'], options)  # NO TOCAR

    if options['colorspace'] == 'RGB':
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(
            kmeans.centroids)  #centroides a cn
    elif options['colorspace'] == 'Lab':
        kmeans.centroids = np.reshape(kmeans.centroids,
                                      (-1, 1, kmeans.centroids.shape[1]))
        kmeans.centroids = color.lab2rgb(kmeans.centroids) * 255
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(
            kmeans.centroids)  #centroides a cn
        kmeans.centroids = np.reshape(kmeans.centroids,
                                      (-1, kmeans.centroids.shape[2]))

    #obtenir una representacio dels kmeans.centroids en funcio del 11 colors basics
    #normalitzar: obtenir una representacio dels kmeans.centroids que sumi 1 per fila

    colors, which = getLabels(kmeans, options)  # NO TOCAR
    return colors, which, kmeans  # NO TOCAR
Esempio n. 15
0
 def post(self):
     year = self.get_argument("year")
     taskId = self.get_argument("taskId")
     self.db = MysqlDriver(
         DATABASE.host, DATABASE.username, DATABASE.password,
         DATABASE.dbname)  # connect to MySql database server
     dataset = self.db.getFeatures()
     km = KMeans(dataset, 50, 3, year, taskId)  #initiatating ML algo
     MLthread = Thread(target=km.main, args=())
     MLthread.daemon = True
     MLthread.start()
     self.write("Request created")  #send the response of requested created
     self.finish()
Esempio n. 16
0
 def __init__(self, n_cluster, data, use_kmeans=True, w=0.5, c1=0.8, c2=0.6):
     index = np.random.choice(list(range(len(data))), n_cluster)
     self.centroids = data[index].copy()
     if use_kmeans:
         kmeans = KMeans(n_cluster=n_cluster, init_pp=False)
         kmeans.fit(data)
         self.centroids = kmeans.centroid.copy()
     self.best_position = self.centroids.copy()
     self.best_score = quantization_error(self.centroids, self._predict(data), data)
     self.best_sse = calc_sse(self.centroids, self._predict(data), data)
     self.velocity = np.zeros_like(self.centroids)
     self._w = w
     self._c1 = c1
     self._c2 = c2
Esempio n. 17
0
    def __initialization(self):
        """
        Initialization all parameters by k-means.

        Returns:
            Initial mu, sigma, gamma
        """
        km = KMeans.KMeans(self.k)
        labels = km.fit_predict(self.x, 50)
        mu = np.array(
            [np.average(self.x[labels == i], axis=0) for i in range(self.k)])
        sigma = np.array([np.eye(self.dimension) + 1 for i in range(self.k)])
        gamma = np.array([[-math.log(self.k, math.e)] * self.k] *
                         self.x.shape[0])
        return mu, sigma, gamma
Esempio n. 18
0
    def __init__(self, G, model):
        self.all_vectors_after_pca = make_PCA(model)

        # Make base graph (without algorithm)
        self.BaseGraph = BaseGraph.BaseGraph(self.all_vectors_after_pca)

        # Make Kmeans
        self.kmeans = KMeans.KMeans(self.all_vectors_after_pca, "red", 20)

        # Make Connected Componenet
        self.cc = ConnectedComponents.ConnectedComponents(
            self.all_vectors_after_pca, G, "green", 40)

        # Make Spectral
        self.spectral = SpectralClustering.SpectralClustering(
            self.all_vectors_after_pca, "yellow", 20)

        #Make Combined
        self.Combined = Combined.Combined(self.kmeans, self.spectral, self.cc)
Esempio n. 19
0
 def kmeans(self, event):
     self.db = []
     self.GetParent().setStatus("Aplicando K-means...", 1)
     for r in range(len(self.data)):
         t = []
         for c in range(len(self.data[r]) - 1):
             t.append(self.data[r][c])
         self.db.append(t)
     self.pbutton.actions = [
         self.clusC.GetValue(),
         self.tipeC.GetStringSelection(),
         self.iterC.GetValue(),
         self.epochsC.GetValue()
     ]
     self.k = KMeans(self.db, self.clusC.GetValue(),
                     self.tipeC.GetStringSelection(), self.iterC.GetValue(),
                     self.epochsC.GetValue())
     self.parent.km = self.k
     self.viewButton.Enable()
     self.GetParent().setStatus("", 0)
Esempio n. 20
0
def run():
    points = np.vstack(
        ((np.random.randn(150, 2) * 0.75 + np.array([2, 0])),
         (np.random.randn(50, 2) * 0.25 + np.array([-1.5, 0.5])),
         (np.random.randn(50, 2) * 0.5 + np.array([-0.5, -0.5]))))
    K = 3
    kmeans = KMeans.KMeans()
    kmeans.fit(points, K, 10)
    #plt.scatter(points[:, 0], points[:, 1])
    c = kmeans.centroids

    t = np.random.randn(1000, 2) * 1
    cid = kmeans.predict(t)
    for k in np.arange(K):
        w = t[cid == k]
        plt.scatter(w[:, 0], w[:, 1])

    plt.scatter(c[:, 0], c[:, 1])

    plt.show()
Esempio n. 21
0
def processImage(im, options):  # NO TOCAR
    """
    processImage : Converts image to configured color space
    :param
        - im: Image to process
        - options: DICT program options
    :return
        - colors : LIST of our label
        - wich : ???
        - kmeans : KMEANS class
    """
    if options['colorspace'] == 'ColorNaming':  # NO TOCAR
        im = cn.ImColorNamingTSELabDescriptor(im)
    elif options['colorspace'] == 'RGB':  # NO TOCAR
        im = im
    elif options['colorspace'] == 'Lab':  # NO TOCAR
        im = color.rgb2lab(im)

    X = np.reshape(im, (-1, im.shape[2]))

    if options['K'] < 2:
        kmeans = bestKElbow(X, options)
    else:
        kmeans = km.KMeans(X, options['K'], options)  # NO TOCAR

    if options['colorspace'] == 'RGB':
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)
    elif options['colorspace'] == 'Lab':
        kmeans.centroids = np.reshape(kmeans.centroids,
                                      (-1, 1, kmeans.centroids.shape[1]))
        kmeans.centroids = color.lab2rgb(kmeans.centroids) * 255
        kmeans.centroids = np.reshape(kmeans.centroids,
                                      (-1, kmeans.centroids.shape[2]))
        kmeans.centroids = cn.ImColorNamingTSELabDescriptor(kmeans.centroids)

    colors, which = getLabels(kmeans, options)  # NO TOCAR
    return colors, which, kmeans  # NO TOCAR
Esempio n. 22
0
        def type(event):

            aantal = int(float(n.get("1.0", "end-1c")))
            kmeans = km.KMeans(aantal_clusters=aantal,
                               vector_lijst=self.points)

            def print_iteration(kmeans, iter):
                print iter, "\t", sum(kmeans.fout)

            kmeans.cluster(stap_callback=print_iteration)

            for widget in self.show_frame.winfo_children():
                widget.destroy()

            for centroid in kmeans.get_centroids():
                rgb = tuple(centroid.astype(np.int32))
                color = "#%02x%02x%02x" % rgb
                print rgb, "\t", color

                col = Tkinter.Label(self.show_frame,
                                    width=1,
                                    height=2,
                                    bg=color)
                col.pack(side=Tkinter.LEFT, fill="both", expand=True)
Esempio n. 23
0
# -*- coding: utf-8 -*-
import json
from skimage import io
from skimage.transform import rescale
import numpy as np
import ColorNaming as cn

import os.path
import Labels as lb
import KMeans as km

Options = {
    'colorspace': 'RGB',
    'K': 6,
    'km_init': 'first',
    'fitting': 'Fisher',
    'single_thr': 0.6,
    'metric': 'basic',
    'verbose': False,
    'max_iter': 1
}

im = io.imread('Images/0010.png')
k_m = km.KMeans(im, Options['K'], Options)
k_m.run()
fisher = k_m.fitting()

print(fisher)
Esempio n. 24
0
                                  7: lambda s: 0 if s == b"-1" else float(s)
                              })

validationDates = np.genfromtxt(validationcsv, delimiter=';', usecols=[0])

#checkDict = {}
KRange = 5
RunTimes = 10

distList = [None]
for k in range(1, KRange):
    distList.append(0)
    print('K =', k, ')')

    for times in range(RunTimes):
        km = KMeans(datasetcsv, k)
        km.SetDates(validationDates)
        km.SetLabels()
        foundSeasons = km.RunKMeans(validationSet)
        distances = km.GetDistances()
        print('=' * 5)
        for x in foundSeasons:
            print(x[0], round(x[1], 2), '%')
        totSom = 0
        for clust in distances:
            for punt in distances[clust]:
                totSom += punt
        distList[k] += totSom
    print('\n' * 5)

plt.plot(range(0, KRange), distList)
Esempio n. 25
0
import KMeans
import numpy as np
from matplotlib import pyplot as plt
def plot(x,labels,centroids):
    colors = np.array(['b', 'g', 'r', 'c', 'm', 'y'])
    fig, ax = plt.subplots()
    ax.scatter(x[:,0],x[:,1],color=colors[labels])
    ax.scatter(centroids[:,0],centroids[:,1],color='k', marker='*',s = 200)
    plt.show()
        
#load data
data = np.genfromtxt('iris.data',delimiter=',')
data = data[1:,0:4]

k = 3 #the number of clusters
max_iter = 100 #maximum number of iterations
c = KMeans.KMeans(k,max_iter)
c.fit(data)

print(c.labels)
print(c.centroids)
plot(data,c.labels,c.centroids)
Esempio n. 26
0
plt.close("all")
if __name__ == "__main__":
    im = io.imread('Images/0047.jpg')
    #im = rescale(im, 0.4, preserve_range=True)
    plt.figure(1)
    plt.imshow(im / 255)
    plt.axis('off')

    X = np.reshape(im, (-1, im.shape[2]))
    print X
    results = []
    for k in range(1, 13):
        plt.figure(3)
        options = {'verbose': True, 'km_init': 'first'}

        k_m = km.KMeans(X, k, options)
        t = time.time()
        k_m.run()
        print time.time() - t
        results.append(k_m.fitting())
        plt.figure(2)
        plt.cla()
        plt.plot(range(1, k + 1), results)
        plt.xlabel('K')
        plt.ylabel('fitting score')
        plt.draw()
        plt.pause(0.01)

    print k_m.centroids

    plt.figure(3)
Esempio n. 27
0
import KMeans as km
import numpy as np

# calcular la distancia entre 2 punts:

print("distancia")
print(km.distance(np.array([[57,280,95]]), np.array([[299,212,204]])))
print(km.distance(np.array([[57,280,95]]), np.array([[249,69,120]])))

# centroides:

print("2 - clusters")
X = np.array([[57,280,95], [131,237,79], [67,155,259], [85, 250, 74], [147, 250, 82], [96, 133, 88]])
C = np.array([[299,212,204], [249,69,120]])

a = km.KMeans(X, 2)
a.centroids = C
a._cluster_points()
print([i+1 for i in a.clusters])


# iteracio
print("3 - nous centroides")
X = np.array([[57,280,95], [131,237,79], [67,155,259], [85, 250, 74], [147, 250, 82], [96, 133, 88]])
C = np.array([[299,212,204], [249,69,120]])

a = km.KMeans(X, 2)
a.centroids = C
a._cluster_points()
a._get_centroids()
print(a.centroids)
Esempio n. 28
0
def createClustering(k, x, y):
    name = "First Attempt of Clustering"
    return KMeans.KMeans(k, name, x, y)
Esempio n. 29
0
import matplotlib.pyplot as plt
import time

import KMeans as km

plt.close("all")
if __name__ == "__main__":
    import os
    print(os.path.dirname(os.path.realpath(__file__)))
    im = io.imread('Images/0053.png')
    # plt.figure(1)
    # plt.imshow(im)
    # plt.axis('off')
    # plt.show()
    options = {'verbose': False, 'km_init': 'first'}
    k_m = km.KMeans(im, 3, options)
    k_m.run()
    print(k_m.centroids)

    X = np.array([[50, 224, 42], [82, 207, 277], [22, 200, 253], [56, 285, 64],
                  [39, 78, 256], [62, 181, 92]])
    k_m = km.KMeans(X, 2, options)
    k_m.centroids = np.array([[59.0, 142.0, 30.0], [103.0, 248.0, 181.0]])
    #    np.copyto(k_m.centroids, np.array([[59.0,142.0,30.0],[103.0,248.0,181.0]]))
    k_m._iterate()
    print(k_m.centroids)

    k_m = km.KMeans(X, 2, options)
    k_m.centroids = np.array([[59.0, 142.0, 30.0], [103.0, 248.0, 181.0]])
    #    np.copyto(k_m.centroids, np.array([[59.0,142.0,30.0],[103.0,248.0,181.0]]))
    k_m._cluster_points()
Esempio n. 30
0
    Y3 = Y[:, 2]
    for i, key in enumerate(dict1):
        Y1[Y1[:] == key] = i
        Yk.append(Y1)

    for i, key in enumerate(dict2):
        Y2[Y2[:] == key] = i
        Yk.append(Y2)

    for i, key in enumerate(dict3):
        Y3[Y3[:] == key] = i
        Yk.append(Y3)


    pca = PCA()
    kmeans = KMeans.KMeans()
    reduced_X = pca.train(X, 0.7)
    print(reduced_X.shape)

    x = reduced_X[:, 0]
    y = reduced_X[:, 1]
    plt.scatter(x, y, s=1, marker=".")
    plt.show()

    k_choices = [4, 8, 10]
    purity_choice = []
    RI_choice = []

    for i, k in enumerate(k_choices):
        Y_test = Yk[i]
        purity = 0