【问题标题】:Visualize Apriori Algorithm可视化 Apriori 算法
【发布时间】:2020-04-29 06:48:55
【问题描述】:

我正在尝试从某个数据集中查找购买模式。现在我正在对我从 Apriori 获得的结果进行可视化,关联规则。但是,我在代码中遇到了这个 TypeError 问题。

我尝试定义一个draw_graph函数,然后调用graph函数来绘制我的规则。

def draw_graph(rules, rules_to_show):
import networkx as nx
G1 = nx.DiGraph()

color_map = []
N = 50
colors = np.random.rand(N)
strs=['R0','R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11']

for i in range(rules_to_show):
    G1.add_nodes_from(["R"+str(i)])

    for a in rules.iloc[i]['antecedants']:
        G1.add_nodes_from([a])
        G1.add_edge(a,"R"+str(i),color=colors[i],weight = 2)

    for c in rules.iloc[i]['consequents']:
        G1.add_node_from([c])
        G1.add_edge("R"+str(i),c,color=colors[i], weight = 2)
for node in G1:
    found_a_string=False
    for item in strs:
        if node==item:
            found_a_string = True
    if found_a_string:
        color_map.append('yellow')
    else:
        color_map.append('green')

edges = G1.edges()
colors = [G1[u][v]['color'] for u,v in edges]
weights = [G1[u][v]['weight'] for u,v in edges]

pos = nx.spring_layout(G1, k=16, scale=1)
nx.draw(G1, pos, edges = edges, node_color = color_map, edge_color = colors, width=weights, font_size=16, with_labels=False)

for p in pos:
    pos[p][1] += 0.07
nx.draw_networkx_labels(G1,pos)
plt.show()

support = rules.values(columns=['support'])
confidence = rules.values(columns=['confidence'])

import seaborn as sns1

for i in range (len(support)):
    support[i] = support[i]
    confidence[i] = confidence[i]

plt.title('Association Rules')
plt.xlabel('support')
plt.ylabel('confidence')
sns1.regplot(x=support,y=confidence,fit_reg=False)

plt.gcf().clear()
draw_graph(rules,10)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-279d477166f6> in <module>
----> 1 support = rules.values(columns=['support'])
      2 confidence = rules.values(columns=['confidence'])
      3 
      4 import seaborn as sns1
      5 

TypeError: 'numpy.ndarray' object is not callable

如果对此有任何帮助,那就太好了。谢谢

【问题讨论】:

    标签: python jupyter-notebook apriori


    【解决方案1】:

    代替

    support = rules.values(columns=['support'])
    confidence = rules.values(columns=['confidence'])
    

    试试

     support = rules['support'].values
     confidence = rules['confidence'].values
    

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多