Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the accuracy score through the meanshift algorithm?

I want to get the accuracy score for meanshift based on the code below.

I tried with a classification algorithm from sklearn and the "score" was able to produce the accuracy, but when it comes to clustering algorithm such as Birch, kmeans, meanshift, algglomerative, it seems like I’m not able to get the accuracy score through the same code.

from sklearn.cluster import MeanShift
kfold = 5

mean = MeanShift()
Count1 = 1
Aa1 = 0

Cnt1 = len(X)
kf = KFold(n_splits=kfold)
            
for train_index,test_index in kf.split(X):
    X_train,X_test = X[train_index],X[test_index]
    Y_train,Y_test = Y[train_index],Y[test_index]
    
    model1 = mean
    model1.fit(X_train, Y_train)
    Pa_1=model1.predict(X_test)
    AC1=model1.score(X_test,Y_test)
    
    Aa1 += AC1
       
print()
print("Accuracy for DT GA: %f" % (Aa1/kfold))

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---------------------------------------------------------------------------
AttributeError
  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_33368/2687219843.py in <module>
     12     model1.fit(X_train, Y_train)
     13     Pa_1=model1.predict(X_test)
---> 14     AC1=model1.score(X_test,Y_test)
     15 
     16     Aa1 += AC1

AttributeError: 'MeanShift' object has no attribute 'score'

>Solution :

Yes, the error is true. MeanShift really doesn’t have any score attribute.
As you wamted to get the accuracy, you need to use matrics from sklearn.

I am giving you a small snippet.

from sklearn.metrics import accuracy_score

...
...

Pa_1=model1.predict(X_test)
AC1=accuracy_score(Y_test, Pa_1)    #<--- See this line
...

See the accuracy_score documentation:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html

You can check more matrices as per your need: https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading