{"id":1857,"date":"2018-02-10T04:15:43","date_gmt":"2018-02-10T04:15:43","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?p=1857"},"modified":"2018-02-16T02:42:22","modified_gmt":"2018-02-16T02:42:22","slug":"how-to-create-data-visualization-for-association-rules-in-data-mining","status":"publish","type":"post","link":"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/","title":{"rendered":"How to Create Data Visualization for Association Rules in Data Mining"},"content":{"rendered":"<p><b>Association rule learning<\/b> is used in machine learning for discovering interesting relations between variables. Apriori algorithm is a popular algorithm for association rules mining and extracting frequent itemsets with applications in association rule learning. It has been designed to operate on databases containing transactions, such as purchases by customers of a store (market basket analysis). [1]   Besides market basket analysis this algorithm can be applied to other problems. For example in web user navigation domain we can search for rules like customer who visited web page A and page B also visited page C.<\/p>\n<p>Python sklearn  library does not have Apriori algorithm but recently I come across post [3] where python library <b>MLxtend<\/b> was used for Market Basket Analysis. MLxtend has modules for different tasks. In this post I will share how to create data visualization for association rules in data mining using <b>MLxtend<\/b> for getting association rules and  <b>NetworkX<\/b> module for charting the diagram.   First we need to get association rules.<\/p>\n<h3>Getting Association Rules from Array Data<\/h3>\n<p>To get association rules you can run the following code[4] <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\r\n           ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\r\n           ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],\r\n           ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],\r\n           ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]\r\n           \r\n           \r\nimport pandas as pd\r\nfrom mlxtend.preprocessing import OnehotTransactions\r\nfrom mlxtend.frequent_patterns import apriori\r\n\r\noht = OnehotTransactions()\r\noht_ary = oht.fit(dataset).transform(dataset)\r\ndf = pd.DataFrame(oht_ary, columns=oht.columns_)\r\nprint (df)           \r\n\r\nfrequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)\r\nprint (frequent_itemsets)\r\n\r\nassociation_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.7)\r\nrules = association_rules(frequent_itemsets, metric=&quot;lift&quot;, min_threshold=1.2)\r\nprint (rules)\r\n\r\n&quot;&quot;&quot;\r\nBelow is the output\r\n    support                     itemsets\r\n0       0.8                       [Eggs]\r\n1       1.0               [Kidney Beans]\r\n2       0.6                       [Milk]\r\n3       0.6                      [Onion]\r\n4       0.6                     [Yogurt]\r\n5       0.8         [Eggs, Kidney Beans]\r\n6       0.6                [Eggs, Onion]\r\n7       0.6         [Kidney Beans, Milk]\r\n8       0.6        [Kidney Beans, Onion]\r\n9       0.6       [Kidney Beans, Yogurt]\r\n10      0.6  [Eggs, Kidney Beans, Onion]\r\n\r\n             antecedants            consequents  support  confidence  lift\r\n0  (Kidney Beans, Onion)                 (Eggs)      0.6        1.00  1.25\r\n1   (Kidney Beans, Eggs)                (Onion)      0.8        0.75  1.25\r\n2                (Onion)   (Kidney Beans, Eggs)      0.6        1.00  1.25\r\n3                 (Eggs)  (Kidney Beans, Onion)      0.8        0.75  1.25\r\n4                (Onion)                 (Eggs)      0.6        1.00  1.25\r\n5                 (Eggs)                (Onion)      0.8        0.75  1.25\r\n\r\n&quot;&quot;&quot;\r\n<\/pre>\n<h3>Confidence and Support in Data Mining<\/h3>\n<p>To select interesting rules we can use best-known constraints which are a minimum thresholds  on <b>confidence and support.<\/b><br \/>\n<b>Support<\/b> is an indication of how frequently the itemset appears in the dataset.<br \/>\n<b>Confidence<\/b> is an indication of how often the rule has been found to be true. [5]<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsupport=rules.as_matrix(columns=['support'])\r\nconfidence=rules.as_matrix(columns=['confidence'])\r\n<\/pre>\n<p>Below is the scatter plot for support and confidence:<br \/>\n<figure id=\"attachment_1882\" aria-describedby=\"caption-attachment-1882\" style=\"width: 395px\" class=\"wp-caption alignnone\"><img data-attachment-id=\"1882\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/association_rules_scatter_plot\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png\" data-orig-size=\"405,272\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"association_rules_scatter_plot\" data-image-description=\"&lt;p&gt;Association rules &#8211; scatter plot for example dataset&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Association rules &#8211; scatter plot&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot-300x201.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png\" alt=\"Association rules - scatter plot\" width=\"405\" height=\"272\" class=\"size-full wp-image-1882\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png 405w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot-300x201.png 300w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><figcaption id=\"caption-attachment-1882\" class=\"wp-caption-text\">Association rules &#8211; scatter plot<\/figcaption><\/figure><\/p>\n<p>And here is the python code to build scatter plot. Since few points here have the same values I added small random values to show all points.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport random\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nfor i in range (len(support)):\r\n   support[i] = support[i] + 0.0025 * (random.randint(1,10) - 5) \r\n   confidence[i] = confidence[i] + 0.0025 * (random.randint(1,10) - 5)\r\n\r\nplt.scatter(support, confidence,   alpha=0.5, marker=&quot;*&quot;)\r\nplt.xlabel('support')\r\nplt.ylabel('confidence') \r\nplt.show()\r\n<\/pre>\n<h3>How to Create Data Visualization with NetworkX for Association Rules in Data Mining<\/h3>\n<p>To represent association rules as diagram, <b>NetworkX<\/b> python library is utilized in this post. Here is the association rule example :<br \/>\n<b>(Kidney Beans, Onion)  ==> (Eggs)<\/b>  <\/p>\n<p>Directed graph below is built for this rule and shown below. Arrows are drawn as just thicker blue stubs. The node with R0 identifies one rule, and it will have always incoming and outcoming edges. Incoming edge(s) will represent antecedants and the stub (arrow) will be next to node.  <\/p>\n<p><img data-attachment-id=\"1877\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/association_rules_1_rule\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule.png\" data-orig-size=\"445,270\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"association_rules_1_rule\" data-image-description=\"&lt;p&gt;Association rules &#8211; graph with one rule&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Association rules &#8211; graph with one rule&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule-300x182.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule.png\" alt=\"\" width=\"445\" height=\"270\" class=\"alignnone size-full wp-image-1877\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule.png 445w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_1_rule-300x182.png 300w\" sizes=\"(max-width: 445px) 100vw, 445px\" \/><\/p>\n<p>Below is the example of graph for all rules extracted from example dataset.<br \/>\n<img data-attachment-id=\"1879\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/association_rules_basic_set\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set.png\" data-orig-size=\"931,592\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"association_rules_basic_set\" data-image-description=\"&lt;p&gt;Association rules graph&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Association rules graph&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set-300x191.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set.png\" alt=\"\" width=\"931\" height=\"592\" class=\"alignnone size-full wp-image-1879\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set.png 931w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set-300x191.png 300w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_basic_set-768x488.png 768w\" sizes=\"(max-width: 931px) 100vw, 931px\" \/><\/p>\n<p>Here is the source code to build association rules with NetworkX.   To call function use draw_graph(rules, 6)<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef draw_graph(rules, rules_to_show):\r\n  import networkx as nx  \r\n  G1 = nx.DiGraph()\r\n  \r\n  color_map=[]\r\n  N = 50\r\n  colors = np.random.rand(N)    \r\n  strs=['R0', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11']   \r\n  \r\n  \r\n  for i in range (rules_to_show):      \r\n    G1.add_nodes_from([&quot;R&quot;+str(i)])\r\n   \r\n    \r\n    for a in rules.iloc[i]['antecedants']:\r\n               \r\n        G1.add_nodes_from([a])\r\n       \r\n        G1.add_edge(a, &quot;R&quot;+str(i), color=colors[i] , weight = 2)\r\n      \r\n    for c in rules.iloc[i]['consequents']:\r\n            \r\n            G1.add_nodes_from()\r\n           \r\n            G1.add_edge(&quot;R&quot;+str(i), c, color=colors[i],  weight=2)\r\n\r\n  for node in G1:\r\n       found_a_string = False\r\n       for item in strs: \r\n           if node==item:\r\n                found_a_string = True\r\n       if found_a_string:\r\n            color_map.append('yellow')\r\n       else:\r\n            color_map.append('green')       \r\n\r\n\r\n  \r\n  edges = G1.edges()\r\n  colors = [G1[u][v]['color'] for u,v in edges]\r\n  weights = [G1[u][v]['weight'] for u,v in edges]\r\n\r\n  pos = nx.spring_layout(G1, k=16, scale=1)\r\n  nx.draw(G1, pos, edges=edges, node_color = color_map, edge_color=colors, width=weights, font_size=16, with_labels=False)            \r\n  \r\n  for p in pos:  # raise text positions\r\n           pos[p][1] += 0.07\r\n  nx.draw_networkx_labels(G1, pos)\r\n  plt.show()\r\n<\/pre>\n<h3>Data Visualization for Online Retail Data Set<\/h3>\n<p>To get real feeling and testing on visualization we can take available online retail store dataset[6] and apply the code for association rules graph.  For downloading retail data and formatting some columns the code from [3] was used.<\/p>\n<p>Below are the result of scatter plot for support and confidence. To build the scatter plot seaborn library was used this time. Also you can find below visualization for association rules (first 10 rules) for retail data set.<\/p>\n<p><img data-attachment-id=\"1883\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/association_rules_scatter_plot_for_retailer_dataset\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset.png\" data-orig-size=\"414,297\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"association_rules_scatter_plot_for_retailer_dataset\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset-300x215.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset.png\" alt=\"\" width=\"414\" height=\"297\" class=\"alignnone size-full wp-image-1883\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset.png 414w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot_for_retailer_dataset-300x215.png 300w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/p>\n<p><img data-attachment-id=\"1880\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/association_rules_retail_dataset_2\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_retail_dataset_2-e1518482094275.png\" data-orig-size=\"855,443\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"association_rules_retail_dataset_2\" data-image-description=\"&lt;p&gt;Association rules graph&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Association rules graph&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_retail_dataset_2-300x155.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_retail_dataset_2-1024x531.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_retail_dataset_2.png\" alt=\"\" width=\"1179\" height=\"611\" class=\"alignnone size-full wp-image-1880\" \/><\/p>\n<p>Here is the python full source code for data visualization association rules in data mining.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n\r\ndataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\r\n           ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],\r\n           ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],\r\n           ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],\r\n           ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]\r\n           \r\n           \r\nimport pandas as pd\r\nfrom mlxtend.preprocessing import OnehotTransactions\r\nfrom mlxtend.frequent_patterns import apriori\r\n\r\noht = OnehotTransactions()\r\noht_ary = oht.fit(dataset).transform(dataset)\r\ndf = pd.DataFrame(oht_ary, columns=oht.columns_)\r\nprint (df)           \r\n\r\nfrequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)\r\nprint (frequent_itemsets)\r\n\r\nfrom mlxtend.frequent_patterns import association_rules\r\n\r\nassociation_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.7)\r\nrules = association_rules(frequent_itemsets, metric=&quot;lift&quot;, min_threshold=1.2)\r\nprint (rules)\r\n\r\nsupport=rules.as_matrix(columns=['support'])\r\nconfidence=rules.as_matrix(columns=['confidence'])\r\n\r\n\r\nimport random\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nfor i in range (len(support)):\r\n   support[i] = support[i] + 0.0025 * (random.randint(1,10) - 5) \r\n   confidence[i] = confidence[i] + 0.0025 * (random.randint(1,10) - 5)\r\n\r\nplt.scatter(support, confidence,   alpha=0.5, marker=&quot;*&quot;)\r\nplt.xlabel('support')\r\nplt.ylabel('confidence') \r\nplt.show()\r\n\r\nimport numpy as np\r\n\r\ndef draw_graph(rules, rules_to_show):\r\n  import networkx as nx  \r\n  G1 = nx.DiGraph()\r\n  \r\n  color_map=[]\r\n  N = 50\r\n  colors = np.random.rand(N)    \r\n  strs=['R0', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11']   \r\n  \r\n  \r\n  for i in range (rules_to_show):      \r\n    G1.add_nodes_from([&quot;R&quot;+str(i)])\r\n   \r\n    \r\n    for a in rules.iloc[i]['antecedants']:\r\n               \r\n        G1.add_nodes_from([a])\r\n       \r\n        G1.add_edge(a, &quot;R&quot;+str(i), color=colors[i] , weight = 2)\r\n      \r\n    for c in rules.iloc[i]['consequents']:\r\n            \r\n            G1.add_nodes_from()\r\n           \r\n            G1.add_edge(&quot;R&quot;+str(i), c, color=colors[i],  weight=2)\r\n\r\n  for node in G1:\r\n       found_a_string = False\r\n       for item in strs: \r\n           if node==item:\r\n                found_a_string = True\r\n       if found_a_string:\r\n            color_map.append('yellow')\r\n       else:\r\n            color_map.append('green')       \r\n\r\n\r\n  \r\n  edges = G1.edges()\r\n  colors = [G1[u][v]['color'] for u,v in edges]\r\n  weights = [G1[u][v]['weight'] for u,v in edges]\r\n\r\n  pos = nx.spring_layout(G1, k=16, scale=1)\r\n  nx.draw(G1, pos, edges=edges, node_color = color_map, edge_color=colors, width=weights, font_size=16, with_labels=False)            \r\n  \r\n  for p in pos:  # raise text positions\r\n           pos[p][1] += 0.07\r\n  nx.draw_networkx_labels(G1, pos)\r\n  plt.show()\r\n\r\n    \r\ndraw_graph (rules, 6)   \r\n\r\n\r\ndf = pd.read_excel('http:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/00352\/Online%20Retail.xlsx')\r\n\r\n\r\ndf['Description'] = df['Description'].str.strip()\r\ndf.dropna(axis=0, subset=['InvoiceNo'], inplace=True)\r\ndf['InvoiceNo'] = df['InvoiceNo'].astype('str')\r\ndf = df[~df['InvoiceNo'].str.contains('C')]\r\n\r\nbasket = (df[df['Country'] ==&quot;France&quot;]\r\n          .groupby(['InvoiceNo', 'Description'])['Quantity']\r\n          .sum().unstack().reset_index().fillna(0)\r\n          .set_index('InvoiceNo'))\r\n\r\ndef encode_units(x):\r\n    if x &lt;= 0:\r\n        return 0\r\n    if x &gt;= 1:\r\n        return 1\r\n\r\nbasket_sets = basket.applymap(encode_units)\r\nbasket_sets.drop('POSTAGE', inplace=True, axis=1)\r\n\r\nfrequent_itemsets = apriori(basket_sets, min_support=0.07, use_colnames=True)\r\n\r\nrules = association_rules(frequent_itemsets, metric=&quot;lift&quot;, min_threshold=1)\r\nrules.head()\r\n\r\nprint (rules)\r\n\r\n\r\n\r\nsupport=rules.as_matrix(columns=['support'])\r\nconfidence=rules.as_matrix(columns=['confidence'])\r\n\r\nimport seaborn as sns1\r\n\r\nfor i in range (len(support)):\r\n    support[i] = support[i] \r\n    confidence[i] = confidence[i] \r\n    \r\nplt.title('Association Rules')\r\nplt.xlabel('support')\r\nplt.ylabel('confidence')    \r\nsns1.regplot(x=support, y=confidence, fit_reg=False)\r\n\r\nplt.gcf().clear()\r\ndraw_graph (rules, 10)  \r\n\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p>1. <a href=https:\/\/rasbt.github.io\/mlxtend\/user_guide\/frequent_patterns\/apriori\/ target=\"_blank\">MLxtend Apriori<\/a><br \/>\n2. <a href=https:\/\/sebastianraschka.com\/pdf\/software\/mlxtend-latest.pdf target=\"_blank\">mlxtend-latest<\/a><br \/>\n3. <a href=http:\/\/pbpython.com\/market-basket-analysis.html target=\"_blank\">Introduction to Market Basket Analysis in Python<\/a><br \/>\n4. <a href=http:\/\/rasbt.github.io\/mlxtend\/#welcome-to-mlxtends-documentation target=\"_blank\">MLxtends-documentation<\/a><br \/>\n5. <a href=https:\/\/en.wikipedia.org\/wiki\/Association_rule_learning target=\"_blank\">Association rule learning<\/a><br \/>\n6. <a href=http:\/\/archive.ics.uci.edu\/ml\/datasets\/Online+Retail target=\"_blank\">Online Retail Data Set<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Association rule learning is used in machine learning for discovering interesting relations between variables. Apriori algorithm is a popular algorithm for association rules mining and extracting frequent itemsets with applications in association rule learning. It has been designed to operate on databases containing transactions, such as purchases by customers of a store (market basket analysis). &#8230; <a title=\"How to Create Data Visualization for Association Rules in Data Mining\" class=\"read-more\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[2,6,10],"tags":[59,57,55,58,29,56,54,27],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications<\/title>\n<meta name=\"description\" content=\"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-10T04:15:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-16T02:42:22+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\",\"url\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\",\"name\":\"How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2018-02-10T04:15:43+00:00\",\"dateModified\":\"2018-02-16T02:42:22+00:00\",\"author\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\"},\"description\":\"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.\",\"breadcrumb\":{\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Data Visualization for Association Rules in Data Mining\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/\",\"name\":\"Machine Learning Applications\",\"description\":\"Artificial intelligence, data mining and machine learning for building web based tools and services.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications","description":"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications","og_description":"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.","og_url":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/","og_site_name":"Machine Learning Applications","article_published_time":"2018-02-10T04:15:43+00:00","article_modified_time":"2018-02-16T02:42:22+00:00","og_image":[{"url":"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/association_rules_scatter_plot.png"}],"author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/","url":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/","name":"How to Create Data Visualization for Association Rules in Data Mining - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2018-02-10T04:15:43+00:00","dateModified":"2018-02-16T02:42:22+00:00","author":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478"},"description":"How to Create Data Visualization for Association Rules in Data Mining is showing creation diagram for association rules. Also showing creation of scatter plots for confidence and support in data mining.","breadcrumb":{"@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/02\/10\/how-to-create-data-visualization-for-association-rules-in-data-mining\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Data Visualization for Association Rules in Data Mining"}]},{"@type":"WebSite","@id":"http:\/\/intelligentonlinetools.com\/blog\/#website","url":"http:\/\/intelligentonlinetools.com\/blog\/","name":"Machine Learning Applications","description":"Artificial intelligence, data mining and machine learning for building web based tools and services.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7h1IJ-tX","jetpack-related-posts":[{"id":966,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/02\/18\/building-decision-trees-in-python\/","url_meta":{"origin":1857,"position":0},"title":"Building Decision Trees in Python","date":"February 18, 2017","format":false,"excerpt":"A decision tree is a decision support tool that uses a tree-like graph or model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm. Decision trees are commonly used in operations research, specifically in decision analysis, to\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"Decision Tree","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/02\/dt_post1_N_CTQ_Cost_regr1-2-use-this-300x103.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1446,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/11\/06\/10-new-top-resources-on-machine-learning-from-around-the-web\/","url_meta":{"origin":1857,"position":1},"title":"10 New Top Resources on Machine Learning from Around the Web","date":"November 6, 2017","format":false,"excerpt":"For this post I put new and most interesting machine learning resources that I recently found on the web. This is the list of useful resources in such areas like stock market forecasting, text mining, deep learning, neural networks and getting data from Twitter. Hope you enjoy the reading. 1.\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":256,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/06\/05\/using-python-for-mining-data-from-twitter-visualization-and-other-enchancements\/","url_meta":{"origin":1857,"position":2},"title":"Using Python for Data Visualization of Clustering Results","date":"June 5, 2016","format":false,"excerpt":"In one of the previous post http:\/\/intelligentonlinetools.com\/blog\/2016\/05\/28\/using-python-for-mining-data-from-twitter\/ python source code for mining Twitter data was implemented. Clustering was applied to put tweets in different groups using bag of words representation for the text. The results of clustering were obtained via numerical matrix. Now we will look at visualization of clustering\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"Data Visualization for Clustering Results","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2016\/06\/data-visualization1-300x220.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1516,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/11\/23\/regression-and-classification-decision-trees-building-with-python-and-running-online\/","url_meta":{"origin":1857,"position":3},"title":"Regression and Classification Decision Trees &#8211; Building with Python and Running Online","date":"November 23, 2017","format":false,"excerpt":"According to survey [1] Decision Trees constitute one of the 10 most popular data mining algorithms. Decision trees used in data mining are of two main types: Classification tree analysis is when the predicted outcome is the class to which the data belongs. Regression tree analysis is when the predicted\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/11\/decision_tree_11_2017-300x283.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1289,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/07\/03\/algorithms-metrics-and-online-tool-for-clustering\/","url_meta":{"origin":1857,"position":4},"title":"Algorithms, Metrics and Online Tool for Clustering","date":"July 3, 2017","format":false,"excerpt":"One of the key techniques of exploratory data mining is clustering \u2013 separating instances into distinct groups based on some measure of similarity. [1] In this post we will review how we can do clustering, evaluate and visualize results using online ML Sandbox tool from this website. This tool allows\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/07\/kmeans-clustering-iris-300x286.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":426,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/07\/29\/bio-inspired-optimization-for-text-mining-1\/","url_meta":{"origin":1857,"position":5},"title":"Bio-Inspired Optimization for Text Mining-1","date":"July 29, 2016","format":false,"excerpt":"Motivation Optimization problem studies maximizing or minimizing some function y=f(x) with some range of choices available for x. Biologically inspired (bio-inspired) algorithms for optimization problems are now widely used. A few examples of such optimization are: particle swarm optimization (PSO) that is based on the swarming behavior of fish and\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1857"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=1857"}],"version-history":[{"count":23,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"predecessor-version":[{"id":1902,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions\/1902"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}