报的错是:Syntax error, insert "}" to complete RecordBody
Syntax error, insert "AssignmentOperator Expression" to complete Expression
请你使用代码段粘贴出来,可以吗?
重新起一楼,不要回复我,回复我就没有这个选项了
// 正确
public void test() {
System.out.println("关联规则(强规则):"+assiciationRules);
}
// 错误
public class Test {
System.out.println("关联规则(强规则):"+assiciationRules);
}
你这个代码,我已经不知道改怎么说了,我怀疑你用的是文本编辑器,没有用 IDE,报错的地方简直不要太多,至于你的 System.out.println("关联规则(强规则):"+ assiciationRules); 为什么报错,我也知道了,是你的写的位置有问题,你必须要把它写在方法里面。
你把代码用代码段贴出来呢,最好是完整的
package testapr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class apriori {
private Map<Integer, Set<String>> txDatabase;
private Float minSup;
private Float minConf;
private Integer txDatabaseCount;
private Map<Integer, Set<Set<String>>> freqItemSet;
private Map<Set<String>, Set<Set<String>>> assiciationRules;
public apriori(Map<Integer, Set<String>> txDatabase, Float minSup, Float minConf)
{
this.txDatabase = txDatabase;
this.minSup = minSup;
this.minConf = minConf;
this.txDatabaseCount = this.txDatabase.size();
freqItemSet = new TreeMap<Integer, Set<Set<String>>>();
assiciationRules = new HashMap<Set<String>, Set<Set<String>>>();
}
public static void main(String[] args) {
String fn = "D:/360extremebrowserdownload/Weka-3-8-5/data/weather.nominal.arff";
float minSup = 0.5f;
float minConf = 0.6f;
File file = new File(fn);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
Map<Integer, Set<String>> DB = new HashMap<Integer, Set<String>>();
String line;
String sp = ",";
int num = 0;
while ((line = br.readLine()) != null) {
String[] temp = line.trim().split(sp);
Set<String> set = new TreeSet<String>();
for (int i = 1; i < temp.length; i++) {
set.add(temp[i].trim());
}
num++;
DB.put(num,set);
}
apriori apr = new apriori(DB,minSup,minConf);
apr.findAllFreqItemSet();
apr.findAssociationRules();
}
public void findAllFreqItemSet(){
Map<Set<String>, Float> freqOneItemSet = this.find_Frequent_One_Itemsets();
freqItemSets.put(1, freqOneItemSet);
System.out.println("频繁1-项集:" + freqOneItemSet);
int k = 2;
while(true) {
Set<Set<String>> candFreItemsets = apriori_Gen(k, freqItemSets.get(k-1).keySet());
Map<Set<String>, Float> freqKItemSetMap = getFreqKItemSet(k,candFreItemsets);
if(!freqKItemSetMap.isEmpty()) {
freqItemSets.put(k, freqKItemSetMap);
}else {
break;
}
System.out.println("频繁"+k+"-项集" + freqKItemSetMap);
k++;
}
}
public Map<Set<String>,Float> find_Frequent_One_Itemsets(){
Map<Set<String>, Float> L1 = new HashMap<Set<String>,Float>();
Map<Set<String>,Integer> item1SetMap = new HashMap<Set<String>, Integer>();
Iterator<Map.Entry<Integer, Set<String>>> it = DB.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Set<String>> entry = it.next();
Set<String> itemSet = entry.getValue();
for (String item : itemSet) {
Set<String> key = new HashSet<String>();
key.add(item.trim());
if(!item1SetMap.containsKey(key)) {
item1SetMap.put(key, 1);
}else {
int value = 1 + item1SetMap.get(key);
item1SetMap.put(key, value);
}
}
}
Iterator<Map.Entry<Set<String>,Integer>> iter = item1SetMap.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry<Set<String>, Integer> entry = iter.next();
Float support = new Float(entry.getValue().toString())/new Float(num);
if (support >=minSup)
L1.put(entry, gerKey(),support);
}
return L1;
}
public Set<Set<String>> apriori_Gen(int k, Set<Set<String>> freqKItemSet){
Set<Set<String>> candFreqKItemSet = new HashSet<Set<String>>();
Iterator<Set<String>> it1 = freqKItemSet.iterator();
while (it1.hasNext()){
Set<String> itemSet1 = it1.next();
Iterator<Set<String>> it2 = freqKItemSet.iterator();
while (it2.hasNext()) {
Set<String> itemSet2 = it2.next();
if(!itemSet1.equals(itemSet2)) {
Set<String> commItems = new HashSet<String>();
commItems.addAll(itemSet1);
commItems.retainAll(itemSet2);
if (commItems.size() == k-2) {
Set<String> candiItems = new HashSet<String>();
candiItems.addAll(itemSet1);
candiItems.removeAll(itemSet2);
candiItems.addAll(itemSet2);
if(!has_infrequent_subset(candiItems,freqKItemSet)) {
candFreqKItemSet.add(candiItems);
}
}
}
}
}
return candFreqKItemSet;
}
private boolean has_infrequent_subset(Set<String> itemSet,Set<Set<String>> freqKItemSet) {
Set<Set<String>> subItemSet = new HashSet<Set<String>>();
Iterator<String> itr = itemSet.iterator();
while (itr.hasNext()) {
Set<String> subItem = new HashSet<String>();
Iterator<String> it = itemSet.iterator();
while(it.hasNext()) {
subItem.add(it.next());
}
subItem.remove(itr.next());
subItemSet.add(subItem);
}
Iterator<Set<String>> it = subItemSet.iterator();
while (it.hasNext()) {
if(!freqKIemSet.contains(it.next()))
return true;
}
return false;
}
public Map<Set<String>, Float> getfreqKItemSet(int k,Set<Set<String>> candFreqKItemSet){
Map<Set<String>, Integer> candFreqKItemSetMap = new HashMap<Set<String>, Integer>();
Iterator<Map.Entry<Integer, Set<String>>> it = DB.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer, Set<String>> entry = it.next();
Iterator<Set<String>> iter = candFreqKItemSet.iterator();
while (iter.hasNext()) {
Set<String> s = iter.next();
if(entry.getValue().containsAll(s)) {
if(!candFreqKItemSetMap.containsKey(s)) {
candFreqKItemSetMap.put(s, 1);
}
}else {
int value = 1 + candFreqKItemSetMap.get(s);
candFreqKItemSetMap.put(s, value);
}
}
}
Map<Set<String>, Float> freqKItemSetMap = new HashMap<Set<String>, Float>();
Iterator<Map.Entry<Set<String>, Integer>> itr = candFreqKItemSetMap.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry< Set<String>, Integer> entry = itr.next();
float support = new Float(entry.getValue().toString())/num;
if(support < minSup) {
itr.remove();
}else {
freqKItemSetMap.put(entry.getKey(), support);
}
}
return freqKItemSetMap;
}
public void findAssociationRules(){
Iterator<Map.Entry<Integer,Map<Set<String>, Float>>> it = freqItemSets.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Map<Set<String>, Float>> entry = it.next();
for (Set<String> itemSet : entry.getValue().keySet()) {
int n = itemSet.size()/2;
for (int i=1; i<=n; i++) {
Set<Set<String>> subset = ProperSubsetCombination.getProperSubset(i, itemSet);
for (Set<String> conditionSet : subset)
{
Set<String> conclusionSet = new HashSet<String>();
conclusionSet.addAll(itemSet);
conclusionSet.removeAll(conditionSet);
int s1 = conditionSet.size();
int s2 = conclusionSet.size();
}
float sup1 = freqItemSets.get(s1).get(conditionSet);
float sup2 = freqItemSets.get(s2).get(conclusionSet);
float sup = freqItemSets.get(s1+s2).get(itemSet);
float conf1 = sup/sup1;
float conf2 = sup/sup2;
if (conf1 >= minConf) {
if (assiciationRules.get(conditionSet) == null) {
Set<Map<Set<String>, Float>> conclusionSetSet = new HashSet<Map<Set<String>,Float>>();
Map<Set<String>, Float> sets = new HashMap<Set<String>, Float>();
sets.put(conclusionSet, conf1);
conclusionSetSet.add(sets);
assiciationRules.put(conditionSet, conclusionSetSet);
}else
{
Map<Set<String>,Float> sets = new HashMap<Set<String>, Float>();
sets.put(conclusionSet, conf1);
assiciationRules.get(conditionSet).add(sets);
}
}
if(conf2 >= minConf)
{
if(assiciationRules.get(conclusionSet) == null)
{
Set<Map<Set<String>, Float>> conclusionSetSet =
new HashSet<Map<Set<String>,Float>>();
Map<Set<String>,Float> sets = new HashMap<Set<String>, Float>();
sets.put(conclusionSet, conf2);
conclusionSetSet.add(sets);
assiciationRules.get(conclusionSet, conclusionSetSet);
}else
{
Map<Set<String>, Float> sets = new HashMap<Set<String>, Float>();
sets.put(conditionSet, conf2);
assiciationRules.get(conclusionSet).add(sets);
}
}
}
}
}
}
System.out.println("关联规则(强规则):"+ assiciationRules);
}
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632