Example of using scikit-fuzzy cmeans based on example 10.10 from Fuzzy Logic with Engineering App

2,094 views
Skip to first unread message

Omry Jacobson

unread,
Feb 18, 2014, 12:12:05 AM2/18/14
to scikit...@googlegroups.com
Hello Mr Warner.
my day was saved with cmeans as I need it for my project.
I was wondering if anyone could please explain the use of the function as I'm trying to use
example 10.10 from Fuzzy Logic with Engineering Application 3rd ED to see how I use this wonderful function and I can't seem
to understand How I use the results to determine Fuzzy membership



Listed below are the results:

>>> data
array([[ 1. ,  3. ],
       [ 1.5,  3.2],
       [ 1.3,  2.8],
       [ 3. ,  1. ]])

#by the book [ 1. ,  3. ], [ 1.5,  3.2], [ 1.3,  2.8] are cluster 1, [3,1] is cluster 2


>>> cntr, U, U0, d, Jm, p, fpc=cmeans(data,c=2, m=2, error=None, maxiter=10, U_init=None, seed=None)

>>> cntr
array([[ 3. ,  3.2,  2.8,  1. ],
       [ 1. ,  1.5,  1.3,  3. ]])

>>> U
array([[  3.75219228e-33,   1.00000000e+00],
       [  1.00000000e+00,   3.75219228e-33]])


>>> U0
array([[ 0.70313863,  0.96951609],
       [ 0.29686137,  0.03048391]])


>>> d
array([[  3.62491379e+00,   2.22044605e-16],
       [  2.22044605e-16,   3.62491379e+00]])


>>> Jm
array([  4.26931723e+00,   1.52050181e-01,   2.36002465e-07,
         2.11381080e-30,   1.39431165e-30,   1.39431165e-30,
         1.39431165e-30,   1.39431165e-30,   1.39431165e-30])

>>> fpc
1.0


JDWarner

unread,
Feb 18, 2014, 12:43:38 AM2/18/14
to scikit...@googlegroups.com

Hello Omry,

You have hit upon one of the biggest problems with cmeans at the moment - notably, that the API is overly complex. A longstanding intent of mine is to expose this in a sklearn-like way; perhaps I should move this up in my priority queue. In the meantime I can help you, but keep in mind the API will likely change in the future.

Basically, you need to present the transpose of data to the algorithm. As written the algorithm believes you have two input points instead of four, sees you are asking for two outputs, so it returns your original two points as cntr - this is the trivial case!

If you do this, executing

cntr, U, U0, d, Jm, p, fpc=skfuzzy.cmeans(data.T,c=2, m=2, error=None, maxiter=10, U_init=None, seed=None)

you will find the following:

>>> cntr
array([[ 2.99950869,  1.00061844],
       [ 1.26601391,  2.99910802]])

>>> U
array([[  0.009,   0.013,   0.007,   0.999  ],
       [  0.991,   0.987,  0.993,   0.000]])

Which tells you the locations of the calculated cluster centers in cntr, as well as the final fuzzy partitions for each point in U. In this case, as your book predicts, the first three data points belong to one cluster while the fourth belongs to a different cluster.

Also note that since initialization is random if you ran this algorithm several times you would find nearly identical results - but the identities of the cluster centers could be transposed. Which is why I worded my prior paragraph carefully (“one cluster”, “a different cluster”) rather than outright claiming the first or second cluster. This is typical for unsupervised algorithms.

If you need to then use these results to predict the fuzzy clustering of a different dataset, the function skfuzzy.cmeans_predict will be of assistance.

Again, these are functional at the moment but the API will change to something more akin to Scikit-Learn in the near future.

Best regards,
Josh Warner

Omry Jacobson

unread,
Feb 18, 2014, 1:43:25 AM2/18/14
to scikit...@googlegroups.com
Thank you.
this (and possibly this example in video ) are great for understanding the function and its use.

thanks a lot

Omry Jacobson

unread,
Feb 18, 2014, 1:48:36 AM2/18/14
to scikit...@googlegroups.com
Q: Will I need to transpose the data I will want to "predict membership"  when I send it to skfuzzy.cmeans_predict  as I did for cmeans ?
thanks

Josh Warner

unread,
Feb 18, 2014, 2:01:24 AM2/18/14
to Omry Jacobson, scikit...@googlegroups.com
Both algorithms should use the same convention.
Reply all
Reply to author
Forward
0 new messages