精度分数(numpy.float64'对象不可调用)

我不知道如何解决这个问题,有人能给我解释一下吗?


我正在努力通过更改 DecisionTreeClassifier 的参数来在循环中获得最佳 precision_score


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)

        

这是我的错误:


---------------------------------------------------------------------------

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**

这是数据集

https://i.stack.imgur.com/QJXSY.png


江户川乱折腾
浏览 124回答 1
1回答

MMTTMM

循环中的最后一行:precision_score = precision_score(y_test,preds,average='micro')temp_scores = pd.DataFrame({'depth':[depth],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'test_score':[test_score],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'train_score':[train_score],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'precision_score:':[precision_score]})scores_data = scores_data.append(temp_scores)应改为:precision_score_ = precision_score(y_test,preds,average='micro')temp_scores = pd.DataFrame({'depth':[depth],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'test_score':[test_score],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'train_score':[train_score],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'precision_score:':[precision_score_]})scores_data = scores_data.append(temp_scores)您定义precision_score为 numpy 数组,然后像函数一样调用它(下一个周期)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python