模块'numpy'没有属性'集合',绘制决策树

时间:2018-04-16 12:06:19

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

我有一个numpy维度数组(81,38)作为训练数据特征,另一个长度为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']

2 个答案:

答案 0 :(得分:2)

因为numpy没有收藏......让它像

import collections
edges = collections.defaultdict(list)

答案 1 :(得分:-1)

Numpy没有名为' collections'因为集合是一个不同的包。 但请将您的代码更改为:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let uierPickedInfo = info[UIImagePickerControllerOriginalImage]as? UIImage {

        guard CIImage(image: userPickedInfo) != nil else {
            fatalError("Cannot convert")
        }
        imageView.image = userPickedInfo
    }
    imagePicker.dismiss(animated: true, completion: nil)
}