【问题标题】:Module 'numpy' has no attribute 'collections', plotting a decision tree模块“numpy”没有属性“集合”,绘制决策树
【发布时间】:2018-09-26 04:06:21
【问题描述】:

我有一个维度为 (81,38) 的 numpy 数组作为训练数据特征,另一个长度为 82 的列表作为标签。

我使用它们训练了我的 DecisionTreeClassifier,我想可视化树,但出现了以下错误:

edges = np.collections.defaultdict(list)
AttributeError: module 'numpy' has no attribute 'collections'

这是我的代码:

from sklearn import tree
df = df.from_csv("file_path.csv")
clf = tree.DecisionTreeClassifier()
df = df.as_matrix()
print(df.shape)
print(len(train_df['label'].values.tolist()))
clf = clf.fit(df,train_df['label'].values.tolist())

data_feature_names = [ 'goEmbed1', 'goalexa1', 'goscapy1',
                       'goEmbed2', 'goalexa2', 'goscapy2',
                       'goEmbed3', 'goalexa3', 'goscapy3',
                       'goEmbed4', 'goalexa4', 'goscapy4',
                       'goEmbed5', 'goalexa5', 'goscapy5',
                       'goStdAlex', 'goAvgAlexa', 'goStdCos','goAvgCos',

                       'bingEmbed1', 'bingalexa1', 'bingscapy1',
                       'bingEmbed2', 'bingalexa2', 'bingscapy2',
                       'bingEmbed3', 'bingalexa3', 'bingscapy3',
                       'bingEmbed4', 'bingalexa4', 'bingscapy4',
                       'bingEmbed5', 'bingalexa5', 'bingscapy5',
                       'bingStdAlex', 'bingAvgAlexa', 'bingStdCos','bingAvgCos']

# Visualize data
dot_data = tree.export_graphviz(clf,
                                feature_names=data_feature_names,
                                out_file=None,
                                filled=True,
                                rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)

colors = ('turquoise', 'orange', 'red', 'yellow','blue','purple')
edges = np.collections.defaultdict(list)

for edge in graph.get_edge_list():
    edges[edge.get_source()].append(int(edge.get_destination()))

for edge in edges:
    edges[edge].sort()
    for i in range(2):
        dest = graph.get_node(str(edges[edge][i]))[0]
        dest.set_fillcolor(colors[i])

graph.write_png('tree2.png')

我在另一个数据示例上测试了我的代码,它运行良好,但我不知道这是这里的问题。

这是我使用的测试数据:

X = [ [180, 15,0],
      [177, 42,0],
      [136, 35,1],
      [174, 65,0],
      [141, 28,1]]

Y = ['man', 'woman', 'woman', 'man', 'woman']

【问题讨论】:

    标签: python numpy scikit-learn data-visualization decision-tree


    【解决方案1】:

    因为 numpy 没有集合......让它喜欢

    import collections
    edges = collections.defaultdict(list)
    

    【讨论】:

      【解决方案2】:

      Numpy 没有名为“集合”的属性,因为集合是一个不同的包。 但是请将您的代码更改为:

      import collections
      edges = collections.defaultdict(list)
      

      【讨论】:

        猜你喜欢
        • 2020-08-27
        • 2022-01-12
        • 2021-12-01
        • 2019-08-02
        • 1970-01-01
        • 2017-09-29
        • 2017-02-21
        • 2014-08-30
        • 2016-11-09
        相关资源
        最近更新 更多