---
title: "Apriori 购物栏挖掘算法的C#实现。原创代码"
author: "hermitte"
date: "2007-02-01T23:09:29Z"
tags: []
slug: "apriori-c"
draft: false
lastmod: "2007-02-01T23:09:29Z"
id: 51687
url: "https://hermitte.iteye.com/blog/51687"
---

c# 代码

c# 代码

001. ﻿using System;
002. using System.Collections.Generic;
003. using System.Text;
004. using System.Collections;
005. class Program
006. {
007. static void Main(string\[] args)
008. {
009. string file = @"c:\\test.csv";
010. string sup = "2";
011. if (args.Length &gt; 0) {
012. file = args\[0];
013. }
014. if (args.Length== 2)
015. {
016. sup= args\[1];
017. }
018. double support = double.Parse(sup);
019. CSVReader cr = new CSVReader();
020. ItemSet data = cr.Read(file);
021. Program p = new Program();
022. ItemSet a= p.apriori( data, support);
023. for (int i = 0; i &lt; a.Count;i++ )
024. {
025. ItemSet cur = (ItemSet)a\[i];
026. for (int j = 0; j &lt; cur.Count; j++) {
027. ItemSet now = (ItemSet)cur\[j];
028. foreach (DataItem item in now)
029. {
030. Console.Write("编号" + item.Id + ":" + item.ItemName+"  ");
031. }
032. Console.WriteLine("  支持度:"+now.ICount);
033. }
034. }
035. Console.Read();
036. }
037. private ItemSet FindOneColSet(ItemSet data, double support)
038. {
039. ItemSet cur=null;
040. ItemSet result = new ItemSet();
041. ItemSet set=null;
042. ItemSet newset=null;
043. DataItem cd=null;
044. DataItem td=null;
045. bool flag = true;
046. for (int i = 0; i &lt; data.Count; i++) {
047. cur = (ItemSet)data\[i];
048. for (int j = 0; j &lt; cur.Count; j++) {
049. cd = (DataItem)cur\[j];
050. for (int n = 0; n &lt; result.Count; n++) {
051. set = (ItemSet)result\[n];
052. td= (DataItem)set\[0];
053. if (cd.Id == td.Id)
054. {
055. set.ICount++;
056. flag = false;
057. break;
058. }
059. flag=true;
060. }
061. if (flag) {
062. newset = new ItemSet();
063. newset.Add(cd);
064. result.Add(newset);
065. newset.ICount = 1;
066. }
067. }
068. }
069. ItemSet finalResult = new ItemSet();
070. for (int i = 0; i &lt; result.Count; i++)
071. {
072. ItemSet con = (ItemSet)result\[i];
073. if (con.ICount &gt;= support)
074. {
075. finalResult.Add(con);
076. }
077. }
078. //finalResult.Sort();
079. return finalResult;
080. }
081. private ItemSet apriori(  ItemSet data, double support)
082. {
083. ItemSet result = new ItemSet();
084. ItemSet li = new ItemSet();
085. ItemSet conList = new ItemSet();
086. ItemSet subConList = new ItemSet();
087. ItemSet subDataList = new ItemSet();
088. int k = 2;
089. li.Add( new ItemSet());
090. li.Add(this.FindOneColSet(data,support));
091. while (((ItemSet)li\[k-1]).Count != 0)
092. {
093. conList = AprioriGenerate((ItemSet)li\[k - 1],k-1, support);
094. for (int i = 0; i &lt; data.Count; i++)
095. {
096. subDataList = SubSet((ItemSet)data\[i], k);
097. for (int j = 0; j &lt; subDataList.Count; j++)
098. {
099. for (int n = 0; n &lt; conList.Count; n++)
100. {
101. ((ItemSet)subDataList\[j]).Sort();
102. ((ItemSet)conList\[n]).Sort();
103. if (((ItemSet)subDataList\[j]).Equals(conList\[n]))
104. {
105. ((ItemSet)conList\[n]).ICount++;
106. }
107. }
108. }
109. }
110. li.Add(new ItemSet());
111. for (int i = 0; i &lt; conList.Count; i++)
112. {
113. ItemSet con = (ItemSet)conList\[i];
114. if (con.ICount &gt;= support)
115. {
116. ((ItemSet)li\[k]).Add(con);
117. }
118. }
119. k++;
120. }
121. for (int i = 0; i &lt; li.Count; i++)
122. {
123. result.Add(li\[i]);
124. }
125. return result;
126. }
127. private ItemSet AprioriGenerate(ItemSet li,int k, double support)
128. {
129. ItemSet curList = null;
130. ItemSet durList = null;
131. ItemSet candi = null;
132. ItemSet result = new ItemSet();
133. for (int i = 0; i &lt; li.Count; i++)
134. {
135. for (int j = 0; j &lt; li.Count; j++)
136. {
137. bool flag = true ;
138. curList = (ItemSet)li\[i];
139. durList = (ItemSet)li\[j];
140. for (int n = 2; n &lt; k; n++)
141. {
142. if (((DataItem)curList\[n - 2]).Id == ((DataItem)durList\[n - 2]).Id)
143. {
144. flag = true;
145. }
146. else {
147. break;
148. flag = false;
149. }
150. }
151. if (flag && ((DataItem)curList\[k - 1] ).Id&lt; ((DataItem)durList\[k - 1]).Id)
152. {
153. flag = true;
154. }
155. else {
156. flag = false;
157. }
158. if (flag)
159. {
160. candi = new ItemSet();
161. for(int m=0;m&lt;k;m++){
162. candi.Add(durList\[m]);
163. }
164. candi.Add(curList\[k-1]);
165. if (HasInFrequentSubset(candi, li,k))
166. {
167. candi.Clear();
168. }
169. else
170. {
171. result.Add(candi);
172. }
173. }
174. }
175. }
176. return result;
177. }
178. private bool HasInFrequentSubset(ItemSet candidate, ItemSet li,int k)
179. {
180. ItemSet subSet = SubSet(candidate,k);
181. ItemSet curList = null;
182. ItemSet liCurList = null;
183. for (int i = 0; i &lt; subSet.Count; i++)
184. {
185. curList = (ItemSet)subSet\[i];
186. for (int j = 0; j &lt; li.Count; j++)
187. {
188. liCurList = (ItemSet)li\[j];
189. if (liCurList.Equals(curList))
190. {
191. return false;
192. }
193. }
194. }
195. return true;;
196. }
197. //划分子集
198. private ItemSet SubSet(ItemSet set)
199. {
200. ItemSet subSet = new ItemSet();
201. ItemSet itemSet = new ItemSet();
202. //移位求2n次访
203. int num = 1 &lt;&lt; set.Count;
204. int bit;
205. int mask = 0; ;
206. for (int i = 0; i &lt; num; i++)
207. {
208. itemSet = new ItemSet();
209. for (int j = 0; j &lt; set.Count; j++)
210. {
211. //mask与i可以得出某位是否为零
212. mask = 1 &lt;&lt; j;
213. bit = i & mask;
214. if (bit &gt; 0)
215. {
216. itemSet.Add(set\[j]);
217. }
218. }
219. if (itemSet.Count &gt; 0)
220. {
221. subSet.Add(itemSet);
222. }
223. }
224. return subSet;
225. }
226. //划分子集
227. private ItemSet SubSet(ItemSet set, int t)
228. {
229. ItemSet subSet = new ItemSet();
230. ItemSet itemSet = new ItemSet();
231. //移位求2n次访
232. int num = 1 &lt;&lt; set.Count;
233. int bit;
234. int mask = 0; ;
235. for (int i = 0; i &lt; num; i++)
236. {
237. itemSet = new ItemSet();
238. for (int j = 0; j &lt; set.Count; j++)
239. {
240. //mask与i可以得出某位是否为零
241. mask = 1 &lt;&lt; j;
242. bit = i & mask;
243. if (bit &gt; 0)
244. {
245. itemSet.Add(set\[j]);
246. }
247. }
248. if (itemSet.Count == t)
249. {
250. subSet.Add(itemSet);
251. }
252. }
253. return subSet;
254. }
255. }