數據挖掘—Apriori算法(Java實現)

算法描述

(1)掃描全部數據,產生候選1-項集的集合C1;
(2)根據最小支持度,由候選1-項集的集合C1產生頻繁1-項集的集合L1;
(3)對k>1,重復執行步驟(4)、(5)、(6);
(4)由Lk執行連接和剪枝操作,產生候選(k+l)-項集的集合Ck+1;
(5)根據最小支持度,由候選(k+l)-項集的集合Ck+1,產生頻繁(k+1)-項集的集合Lk+1;
(6)若L≠Φ,則k=k+1,跳往步驟(4);否則,跳往步驟(7);
(7)根據最小置信度,由頻繁項集產生強關聯規則,結束。
在這里插入圖片描述

代碼

public class Apriori2
{private final static int SUPPORT = 8; // 支持度閾值private final static double CONFIDENCE = 0.6; // 置信度閾值private final static String ITEM_SPLIT = " "; // 項之間的分隔符private final static String CON = "->"; // 項之間的分隔符/*** 算法主程序* @param dataList* @return Map<String, Integer>*/public Map<String, Integer> apriori(ArrayList<String> dataList){Map<String, Integer> stepFrequentSetMap = new HashMap<>(findFrequentOneSets(dataList));//找出候選// 1-項集//頻繁項集Map<String, Integer> frequentSetMap = new HashMap<String, Integer>(stepFrequentSetMap);while(stepFrequentSetMap.size() > 0)//上一步的頻繁項集不為空{Map<String, Integer> candidateSetMap = aprioriGen(stepFrequentSetMap);//生成候選集Set<String> candidateKeySet = candidateSetMap.keySet();//掃描D,進行計數,計算候選集的支持度計數for(String data:dataList){for(String candidate:candidateKeySet){boolean flag = true;String[] strings = candidate.split(ITEM_SPLIT);for(String string:strings){if(!data.contains(string + ITEM_SPLIT))//找不出候選項集中的元素,退出{flag = false;break;}}if(flag)candidateSetMap.put(candidate, candidateSetMap.get(candidate)+1);//支持度加一}}//從候選集中找到符合支持度的頻繁項集stepFrequentSetMap.clear();for(String candidate:candidateKeySet){Integer count = candidateSetMap.get(candidate);if(count>=SUPPORT)stepFrequentSetMap.put(candidate, count);}// 合并所有頻繁集frequentSetMap.putAll(stepFrequentSetMap);}return frequentSetMap;}/*** 找出候選K-項集* @param dataList* @return Map<String, Integer>*/private Map<String, Integer> findFrequentOneSets(ArrayList<String> dataList){Map<String, Integer> resultSetMap = new HashMap<>();for(String data:dataList){String[] strings = data.split(ITEM_SPLIT);for(String string:strings){string += ITEM_SPLIT;if(resultSetMap.get(string)==null){resultSetMap.put(string, 1);}else {resultSetMap.put(string, resultSetMap.get(string)+1);}}}return resultSetMap;}/*** 根據上一步的頻繁項集的集合選出候選集* @param setMap* @return*/private Map<String, Integer> aprioriGen(Map<String, Integer> setMap){Map<String, Integer> candidateSetMap = new HashMap<>();Set<String> candidateSet = setMap.keySet();for(String s1:candidateSet){String[] strings1 = s1.split(ITEM_SPLIT);String s1String = "";for(String temp:strings1)s1String += temp+ITEM_SPLIT;for(String s2:candidateSet){String[] strings2 = s2.split(ITEM_SPLIT);boolean flag = true;for(int i=0;i<strings1.length-1;i++){if(strings1[i].compareTo(strings2[i])!=0)//存在不同元素,不能連接{flag = false;break;}}if(flag && strings1[strings1.length-1].compareTo(strings2[strings1.length-1])<0){//連接步:產生候選String c = s1String+strings2[strings2.length-1]+ITEM_SPLIT;if(hasInfrequentSubset(c, setMap)){//剪枝步:刪除非頻繁的候選}else {candidateSetMap.put(c, 0);}}}}return candidateSetMap;}/*** 使用先驗知識,判斷候選集是否是頻繁項集* @param candidateSet* @param setMap* @return boolean*/private boolean hasInfrequentSubset(String candidateSet, Map<String, Integer> setMap){String[] strings = candidateSet.split(ITEM_SPLIT);//候選集//找出候選集所有的子集,并判斷每個子集是否屬于頻繁子集for(int i=0;i<strings.length;i++){String subString = "";for(int j=0;j<strings.length;j++)//遍歷所有子集{if(j!=i){subString += strings[j]+ITEM_SPLIT;}}if(setMap.get(subString)==null)return true;}return false;}/*** 由頻繁項集產生關聯規則* @param frequentSetMap* @return*/public Map<String, Double> getRelationRules(Map<String, Integer> frequentSetMap){Map<String, Double> relationsMap = new HashMap<>();Set<String> keySet = frequentSetMap.keySet();for(String key:keySet)//遍歷所有頻繁項集{List<String> keySubset = subset(key);//頻繁項的所有子集for(String keySubsetItem:keySubset)//遍歷所有子集{//子集keySubsetItem也是頻繁項Integer count = frequentSetMap.get(keySubsetItem);if(count!=null)//計算置信度{Double confidence = (1.0*frequentSetMap.get(key))/(1.0*frequentSetMap.get(keySubsetItem));if(confidence>CONFIDENCE)relationsMap.put(keySubsetItem+CON+expect(key, keySubsetItem), confidence);}}}return relationsMap;}/*** 求一個集合所有的非空真子集** @param sourceSet* @return* 為了以后可以用在其他地方,這里我們不是用遞歸的方法** 參考:http://blog.163.com/xiaohui_1123@126/blog/static/3980524020109784356915/* 思路:假設集合S(A,B,C,D),其大小為4,擁有2的4次方個子集,即0-15,二進制表示為0000,0001,...,1111。* 對應的子集為空集,{D},...,{A,B,C,D}。*/private List<String> subset(String sourceSet){List<String> result = new ArrayList<>();String[] strings = sourceSet.split(ITEM_SPLIT);//非空真子集for(int i=1;i<(int)(Math.pow(2, strings.length))-1;i++){String item = "";String flag = "";int ii=i;do{flag += ""+ii%2;ii = ii/2;} while (ii>0);for(int j=flag.length()-1;j>=0;j--){if(flag.charAt(j)=='1'){item = strings[j]+ITEM_SPLIT+item;}}result.add(item);}return result;}/*** 集合運算,A/B* @param A* @param B* @return*/private String expect(String stringA,String stringB){String result = "";String[] stringAs = stringA.split(ITEM_SPLIT);String[] stringBs = stringB.split(ITEM_SPLIT);for(int i=0;i<stringAs.length;i++){boolean flag = true;for(int j=0;j<stringBs.length;j++){if(stringAs[i].compareTo(stringBs[j])==0){flag = false;break;}}if(flag)result += stringAs[i]+ITEM_SPLIT;}return result;}public static void readTxt(String fileName){try { // 防止文件建立或讀取失敗,用catch捕捉錯誤并打印,也可以throw/* 讀入TXT文件 */File filename = new File(fileName); // 要讀取以上路徑的input。txt文件InputStreamReader reader = new InputStreamReader(new FileInputStream(filename)); // 建立一個輸入流對象readerBufferedReader br = new BufferedReader(reader); // 建立一個對象,它把文件內容轉成計算機能讀懂的語言String line = "";line = br.readLine();while (line != null) {dataList.add(line);line = br.readLine();}} catch (Exception e) {e.printStackTrace();}}public static ArrayList<String> dataList = new ArrayList<>();public static void main(String[] args){readTxt("test.txt");System.out.println("=數據集合==========");for(String string:dataList){System.out.println(string);}Apriori2 apriori2 = new Apriori2();System.out.println("=頻繁項集==========");Map<String, Integer> frequentSetMap = apriori2.apriori(dataList);Set<String> keySet = frequentSetMap.keySet();for(String key:keySet){System.out.println(key+" : "+frequentSetMap.get(key));}System.out.println("=關聯規則==========");Map<String, Double> relationRulesMap = apriori2.getRelationRules(frequentSetMap);Set<String> rrKeySet = relationRulesMap.keySet();for (String rrKey : rrKeySet){System.out.println(rrKey + "  :  " + relationRulesMap.get(rrKey));}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392120.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392120.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392120.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

怎么匯報一周開發工作情況_如何在沒有經驗的情況下獲得第一份開發人員工作

怎么匯報一周開發工作情況Whether you’ve done a coding bootcamp or taught yourself, getting your first developer job with only a few months of coding under your belt is hard.無論您是完成了編碼訓練營還是自學了&#xff0c;僅靠幾個月的編碼就很難拿到第一份開發人…

vue.js的認知

Vue.js&#xff08;讀音 /vju?/, 類似于 view&#xff09; 是一套構建用戶界面的漸進式框架。 Vue 只關注視圖層&#xff0c; 采用自底向上增量開發的設計。 Vue 的目標是通過盡可能簡單的 API 實現響應的數據綁定和組合的視圖組件。 Vue 學習起來非常簡單&#xff0c;。轉載于…

c語言中的無符號字節,C語言之有符號數和無符號數

我們知道&#xff0c;在C語言中存在無符號數和有符號數(一些高級語言如Java里面是沒有無符號數的)&#xff0c;但是對于計算機而言&#xff0c;其本身并不區別有符號數和無符號數&#xff0c;因為在計算機里面都是0或者1&#xff0c;但是在我們的實際使用中有時候需要使用有符號…

8種排序算法比較

8種排序算法&#xff0c;各算法名稱見下表或見源碼。運行程序時&#xff0c;將需要你輸入一數值&#xff0c;以確定對多少隨機數進行排序。然后將會顯示各排序算法的耗時。并且你可選擇時否進行正序和反序測試。 由于水平有限&#xff0c;可能存在一些錯誤&#xff0c;還請各位…

兩個問題,關于XP進程優化及SVSP虛擬存儲平臺

這兩個問題讓我有點頭痛&#xff0c;是Boss這陣子布置給我的&#xff0c;都一段時間了&#xff0c;我還是沒找出合適的解決方案來答復Boss.第一個問題是&#xff1a;查查X200或X61中的進程&#xff0c;看哪些是可以不要的&#xff0c;停掉&#xff0c;但又不影響用戶使用。&…

數據挖掘—樸素貝葉斯分類算法(Java實現)

算法描述 &#xff08;1&#xff09;掃描訓練樣本數據集&#xff0c;分別統計訓練集中類別 Ci 的個數 Di 和屬于類別Ci 的樣本中屬性Ak取值Xk為 Dik 的實例樣本個數&#xff0c;構成統計表&#xff1b; &#xff08;2&#xff09;計算先驗概率和條件概率&#xff0c;構成概率表…

net core 獲取網站目錄

AppContext.BaseDirectory 獲取項目的根目錄轉載于:https://www.cnblogs.com/zxs-onestar/p/7147265.html

泰晤士報下載_《泰晤士報》和《星期日泰晤士報》新聞編輯室中具有指標的冒險活動-第1部分:問題

泰晤士報下載TLDR: Designing metrics that help you make better decisions is hard. In The Times and The Sunday Times newsrooms, we have spent a lot of time trying to tackle three particular problems.TLDR &#xff1a;設計度量標準以幫助您做出更好的決策非常困難…

速度一半永遠追不上_您將永遠不會知道自己應該怎么做的一半-沒關系。

速度一半永遠追不上by Ken Gilb肯吉爾伯(Ken Gilb) 您將永遠不會知道自己應該怎么做的一半-沒關系。 (You will never know half of what you think you should — and that’s ok.) Impostor syndrome is a real thing in software development. After 20 years in the indus…

c語言自學門檻,初學C語言的人最常問的幾個問題

初學C語言的人最常問的幾個問題C語言是一門通用計算機編程語言&#xff0c;應用廣泛。對于新手來說學習C語言并不是那么容易&#xff0c;下面是C語言初學者最常問的幾個問題&#xff0c;歡迎閱讀!1.多久能學會編程?這是一個沒有答案的問題。每個人投入的時間、學習效率和基礎都…

背景消除的魔力

圖片的功能非常強大&#xff0c;有一圖勝千言的效果&#xff0c;所以在文檔或演示文稿中使用圖片來增加趣味性是一種很棒的想法。但問題是&#xff0c;圖片通常會變為文字中間的獨立矩形&#xff0c;而不是真正與內容融合在一起。您可以在圖片中放置邊框或效果&#xff0c;使其…

Puppet 之 模板和模塊

1 概述模板文件是在puppet模塊下面templates目錄中以”.erb”結尾的文件&#xff0c;puppet模板主要用于文件&#xff0c;例如各種服務的配置文件&#xff0c;相同的服務&#xff0c;不同的配置就可以考慮使用模板文件。模塊是Puppet自包含的代碼和數據集合。絕大多數的清單都…

java異步io_Java中的異步IO與異步請求處理

java異步ioIn this article, I am trying to explain the difference between Async-IO and Async-Request processing in the HTTP request in the Java world.在本文中&#xff0c;我試圖解釋Java世界中HTTP請求中Async-IO和Async-Request處理之間的區別。 In the pre-Java …

異常檢測機器學習_使用機器學習檢測異常

異常檢測機器學習什么是異常檢測&#xff1f; (What is Anomaly Detection?) The anomaly detection problem has been a problem that has been frequently explored in the field of machine learning and has become a classic problem. Anomalies are any unusual sequenc…

數據挖掘—BP神經網絡(Java實現)

public class Test {public static void main(String args[]) throws Exception {ArrayList<ArrayList<Double>> alllist new ArrayList<ArrayList<Double>>(); // 存放所有數據ArrayList<String> outlist new ArrayList<String>(); // …

c語言掌握常用函數,c語言一些常用函數.pdf

c語言一些常用函數C 語言程序設計(常用函數說明)C 語言是 1972 年由美國的 Dennis Ritchie 設計發明的,并首次在 UNIX 操作系統的 DEC PDP-11 計算機上使用。它由早期的編程語言 BCPL(Basic Combind ProgrammingLanguage)發展演變而來。在 1970 年,AT&T 貝爾實驗室的 Ken T…

高階函數 - 函數節流

/*** 函數節流 - 限制函數被頻繁調用* param {Function} fn [需要執行的函數]* param {[type]} interval [限制多長的時間再重復執行fn]*/var throttle function(fn, interval) {var __self fn,timer,firstTime true;return function() {var args arguments,__me…

[CareerCup] 8.7 Chat Server 聊天服務器

8.7 Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve? 這個簡易的聊天服務器功能十分的有限&#xff0c;畢竟只是針對面試題的&…

react hooks使用_如何開始使用React Hooks:受控表格

react hooks使用by Kevin Okeh由Kevin Okeh 如何開始使用React Hooks&#xff1a;受控表格 (How to Get Started With React Hooks: Controlled Forms) React Hooks are a shiny new proposal that will allow you to write 90% cleaner React. According to Dan Abramov, Hoo…

特征工程tf-idf_特征工程-保留和刪除的內容

特征工程tf-idfThe next step after exploring the patterns in data is feature engineering. Any operation performed on the features/columns which could help us in making a prediction from the data could be termed as Feature Engineering. This would include the…