precision score (numpy.float64' object is not callable)

I don't know how to fix this problem, can anyone explain me?

Im truying to get best precision_score in loop, by changing the parameter of DecisionTreeClassifier

import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import precision_score

from sklearn.model_selection import train_test_split
    
    df = pd.read_csv('songs.csv')
    
    X = df.drop(['song','artist','genre','lyrics'],axis=1)
    y = df.artist
    
    X_train,X_test,y_train,y_test = train_test_split(X,y)
    
    scores_data = pd.DataFrame()
    for depth in range(1,100):
        clf = DecisionTreeClassifier(max_depth=depth,criterion='entropy').fit(X_train,y_train)
        train_score = clf.score(X_train,y_train)
        test_score = clf.score(X_test,y_test)
        preds = clf.predict(X_test)
        precision_score = precision_score(y_test,preds,average='micro')
        
        temp_scores = pd.DataFrame({'depth':[depth],
                                    'test_score':[test_score],
                                     'train_score':[train_score],
                                     'precision_score:':[precision_score]})
        scores_data = scores_data.append(temp_scores)
        

This is my error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-f4a4eaa48ce6> in <module>
     17     test_score = clf.score(X_test,y_test)
     18     preds = clf.predict(X_test)
---> 19     precision_score = precision_score(y_test,preds,average='micro')
     20 
     21     temp_scores = pd.DataFrame({'depth':[depth],

**TypeError: 'numpy.float64' object is not callable**

This is the dataset

enter image description here

Upvotes: 1

Views: 751

Answers (1)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25189

Your last lines in the cycle:

precision_score = precision_score(y_test,preds,average='micro')

temp_scores = pd.DataFrame({'depth':[depth],
                            'test_score':[test_score],
                             'train_score':[train_score],
                             'precision_score:':[precision_score]})
scores_data = scores_data.append(temp_scores)

should be changed to:

precision_score_ = precision_score(y_test,preds,average='micro')

temp_scores = pd.DataFrame({'depth':[depth],
                            'test_score':[test_score],
                             'train_score':[train_score],
                             'precision_score:':[precision_score_]})
scores_data = scores_data.append(temp_scores)

You're defining precision_score as numpy array and then calling it (next cycle) as if being a function.

Upvotes: 1

Related Questions