Giter Club home page Giter Club logo

kmedoids's Introduction

kmedoids

The Python implementation of k-medoids.

Example

from sklearn.metrics.pairwise import pairwise_distances
import numpy as np

import kmedoids

# 3 points in dataset
data = np.array([[1,1], 
                [2,2], 
                [10,10]])

# distance matrix
D = pairwise_distances(data, metric='euclidean')

# split into 2 clusters
M, C = kmedoids.kMedoids(D, 2)

print('medoids:')
for point_idx in M:
    print( data[point_idx] )

print('')
print('clustering result:')
for label in C:
    for point_idx in C[label]:
        print('label {0}: {1}'.format(label, data[point_idx]))

Output:

medoids:
[1 1]
[10 10]

clustering result:
label 0: [1 1]
label 0: [2 2]
label 1: [10 10]

License

This code is from:

Bauckhage C. Numpy/scipy Recipes for Data Science: k-Medoids Clustering[R]. Technical Report, University of Bonn, 2015.

Please cite the article if the code is used in your research.

kmedoids's People

Contributors

letiantian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kmedoids's Issues

ValueError: attempt to get argmin of an empty sequence

This error only occurs sometimes.

I think when a cluster size is zero this happens. When trying to get the min of an empty array.
line 29, in kMedoids j = np.argmin(J)

Stack trace -
`F:\Programs\Anaconda3\lib\site-packages\numpy\core_methods.py:59: RuntimeWarning: Mean of empty slice.
warnings.warn("Mean of empty slice.", RuntimeWarning)
Traceback (most recent call last):

File "", line 1, in
runfile('D:/University/FYP/code/fyp_scritps/clustering/phrase_cluster.py', wdir='D:/University/FYP/code/fyp_scritps/clustering')

File "F:\Programs\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)

File "F:\Programs\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "D:/University/FYP/code/fyp_scritps/clustering/phrase_cluster.py", line 43, in
M, C = kmedoids.kMedoids(D, 2)

File "D:\University\FYP\code\fyp_scritps\clustering\kmedoids\kmedoids.py", line 29, in kMedoids
j = np.argmin(J)

File "F:\Programs\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 1033, in argmin
return argmin(axis, out)

File "F:\Programs\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py", line 895, in argmin
return N.ndarray.argmin(self, axis, out)._align(axis)

ValueError: attempt to get argmin of an empty sequence`

distance matrice

what is the tmax varaible for? and why did u set it to 100?
if i have a dataset of 800 points should i put tmax=800?

"TypeError: 'range' object does not support item assignment"

Hi, I tried to get the kMedoids function to work on the provided example but when running the following line:

# split into 2 clusters
M, C = kMedoids(D, 2)

I get the following error:

Traceback (most recent call last):
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "", line 1, in
File "", line 9, in kMedoids
File "mtrand.pyx", line 4832, in mtrand.RandomState.shuffle
File "mtrand.pyx", line 4835, in mtrand.RandomState.shuffle
TypeError: 'range' object does not support item assignment

Any idea what is causing this? Should it work on Python 3.5?

[Q] Asymmetric input matrix

Hey, thank you for sharing this code !

Can the algorithm handle a matrix of distances where dist(A->B) != dist(B->A) ?
i.e. an asymmetric distance matrix.

Thank you !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.