/* public static void readMovieName() * { * StreamReader rs = new StreamReader("u.item", Encoding.Default); * string sLine = ""; * int count = 0; * * while (sLine != null) * { * sLine = rs.ReadLine(); * if (sLine == null) * break; * * int start = sLine.IndexOf('|') + 1; * int end = sLine.IndexOf('('); * if (end > start) * { * string name = sLine.Substring(start, end - start); * if (name.EndsWith("The ")) * { * Console.WriteLine("yes"); * name = "The " + name.Substring(0, name.Length - 6); * * } * movieNames[count++] = name; * } * else * { * movieNames[count++] = "unknown"; * } * * } * }*/ public int CompareTo(object other) { AssociationRule otherTemperature = other as AssociationRule; if (this.Reco_degrees == otherTemperature.Reco_degrees) { return(0); } if (this.Reco_degrees < otherTemperature.Reco_degrees) { return(1); } return(-1); }
/// <summary> /// 获得目标用户支持的关联规则 /// </summary> /// <param name="itemid">目标用户的用户id</param> /// <returns>按照关联度排序后的关联规则集合</returns> public static AssociationRule[] getSupport_AssRules(int userid) { // 存储该用户支持的关联规则 ArrayList support_AssocRules = new ArrayList(); AssociationRule temp_AR; int count_id = 0; // 用户评价过且喜欢的项目id数组 int[] love_items_id = sourceUsers[userid].getUserLoveItems_id(); // 循环搜索关联规则库 for (int i = 0; i < association_Rules.Count; i++) { temp_AR = (AssociationRule)association_Rules[i]; int first_itemid = temp_AR._itemid_1; int second_itemid = temp_AR._itemid_2; // 该用户喜欢的项目出现在该关联规则的左部 // && 该用户对规则右部项目未评分 if ((love_items_id.Contains(first_itemid)) && (sourceUsers[userid].Ratings[second_itemid] == 0)) { support_AssocRules.Add(temp_AR); } } // 按照关联度对其所支持的关联规则排序 AssociationRule[] assRules = new AssociationRule[support_AssocRules.Count]; int count = 0; foreach (AssociationRule obj in support_AssocRules) { assRules[count++] = obj; } Array.Sort(assRules); return(assRules); }
/// <summary> /// 生成关联规则 /// </summary> public void genAssoc_rules() { AssociationRule obj_tempAR; float total_confidence = 0; ArrayList temp_AR = new ArrayList(); for (int count_ar = 0; count_ar < freq_itemsets.Count; count_ar++) { Frequent_Itemset obj = (Frequent_Itemset)freq_itemsets[count_ar]; int first_id = obj._itemid_1; // 第一个项目id int second_id = obj._itemid_2; // 第二个项目id float confidence_1 = (float)((float)obj.support_count / (float)getSupport_count(first_id)); // 置信度1 total_confidence += confidence_1; obj_tempAR = new AssociationRule(first_id, second_id, obj.Support, confidence_1); temp_AR.Add(obj_tempAR); float confidence_2 = (float)((float)obj.support_count / (float)getSupport_count(second_id)); // 置信度2 total_confidence += confidence_2; obj_tempAR = new AssociationRule(second_id, first_id, obj.Support, confidence_2); temp_AR.Add(obj_tempAR); } // 置信度阈值,取平均值 float min_confidence = (float)total_confidence / (float)temp_AR.Count; AssociationRule.min_confidence = min_confidence; // 生成关联规则 foreach (AssociationRule obj in temp_AR) { if (obj.confidence > min_confidence) { association_Rules.Add(obj); } } }
/// <summary> /// 根据用户支持的关联规则得到依据推荐度排序的推荐项目 /// </summary> /// <param name="Supp_AssRules">用户所支持的关联规则集合</param> /// <param name="userid">用户id</param> /// <returns>推荐项目id&推荐度</returns> public static RecItemid_Degree[] getRecItems(AssociationRule[] Supp_AssRules, int userid) { // 每个项目的推荐度 RecItemid_Degree[] temp_recDegree = new RecItemid_Degree[sourceUsers[userid].Ratings.Length]; int count_non_zero = 1; for (int ItemID = 1; ItemID < sourceUsers[userid].Ratings.Length; ItemID++) { temp_recDegree[ItemID] = new RecItemid_Degree(); temp_recDegree[ItemID].recItems_id = ItemID; temp_recDegree[ItemID].rec_Degree = 0; // 项目推荐度初始为零 // 循环扫描关联规则集,积累项目推荐度 for (int count_AR = 0; count_AR < Supp_AssRules.Length; count_AR++) { if (Supp_AssRules[count_AR]._itemid_2 == ItemID) { temp_recDegree[ItemID].rec_Degree += Supp_AssRules[count_AR].Reco_degrees; } } if(temp_recDegree[ItemID].rec_Degree != 0) { count_non_zero++; } } // 依据推荐度排序 Array.Sort(temp_recDegree); recItems = new RecItemid_Degree[count_non_zero]; // 得到推荐度不为零的项,返回之 for (int count_rd = 1; count_rd < recItems.Length; count_rd ++ ) { recItems[count_rd] = temp_recDegree[count_rd]; } return recItems; }
/// <summary> /// 生成关联规则 /// </summary> public void genAssoc_rules() { AssociationRule obj_tempAR; float total_confidence = 0; ArrayList temp_AR = new ArrayList(); for (int count_ar = 0; count_ar < freq_itemsets.Count; count_ar++) { Frequent_Itemset obj = (Frequent_Itemset)freq_itemsets[count_ar]; int first_id = obj._itemid_1; // 第一个项目id int second_id = obj._itemid_2; // 第二个项目id float confidence_1 = (float)((float)obj.support_count / (float)getSupport_count(first_id)); // 置信度1 total_confidence += confidence_1; obj_tempAR = new AssociationRule(first_id, second_id, obj.Support, confidence_1); temp_AR.Add(obj_tempAR); float confidence_2 = (float)((float)obj.support_count / (float)getSupport_count(second_id)); // 置信度2 total_confidence += confidence_2; obj_tempAR = new AssociationRule(second_id, first_id, obj.Support, confidence_2); temp_AR.Add(obj_tempAR); } // 置信度阈值,取平均值 float min_confidence = (float)total_confidence / (float)temp_AR.Count; AssociationRule.min_confidence = min_confidence; // 生成关联规则 foreach (AssociationRule obj in temp_AR) { if (obj.confidence > min_confidence) association_Rules.Add(obj); } }
/// <summary> /// 获得目标用户支持的关联规则 /// </summary> /// <param name="itemid">目标用户的用户id</param> /// <returns>按照关联度排序后的关联规则集合</returns> public static AssociationRule[] getSupport_AssRules(int userid) { // 存储该用户支持的关联规则 ArrayList support_AssocRules = new ArrayList(); AssociationRule temp_AR; int count_id = 0; // 用户评价过且喜欢的项目id数组 int[] love_items_id = sourceUsers[userid].getUserLoveItems_id(); // 循环搜索关联规则库 for (int i = 0; i < association_Rules.Count; i++) { temp_AR = (AssociationRule)association_Rules[i]; int first_itemid = temp_AR._itemid_1; int second_itemid = temp_AR._itemid_2; // 该用户喜欢的项目出现在该关联规则的左部 // && 该用户对规则右部项目未评分 if( (love_items_id.Contains(first_itemid)) && (sourceUsers[userid].Ratings[second_itemid] == 0) ) { support_AssocRules.Add(temp_AR); } } // 按照关联度对其所支持的关联规则排序 AssociationRule[] assRules = new AssociationRule[support_AssocRules.Count]; int count = 0; foreach (AssociationRule obj in support_AssocRules) { assRules[count++] = obj; } Array.Sort(assRules); return assRules; }