我编写了一个程序,它定义了一个“完整”数据表、一组列标题(特征)、一个聚合函数和聚合表之间的关系。
一个示例查询:
?- data(D), fulltable_aggfunction_sets_aggtable(D,mean,[[a,b,c],[d,e,f]],AggTable),
print_data(D),print_data(AggTable).
[a,b,c,d,e,f,g,class]
[1,1,1,1,1,1,1,0]
[2,3,4,5,4,2,1,0]
[3,1,3,4,6,7,8,1]
[1,2,3,6,1,1,2,1]
[feature(1,mean,[a,b,c]),feature(2,mean,[d,e,f])]
[1,1]
[3,3.6666666666666665]
[2.3333333333333335,5.666666666666667]
[2,2.6666666666666665]
D = [[a, b, c, d, e, f, g, class], [1, 1, 1, 1, 1, 1, 1|...], [2, 3, 4, 5, 4, 2|...], [3, 1, 3, 4, 6|...], [1, 2, 3, 6|...]],
AggTable = [[feature(1, mean, [a, b, c]), feature(2, mean, [d, e, f])], [1, 1], [3, 3.6666666666666665], [2.3333333333333335, 5.666666666666667], [2, 2.6666666666666665]]
以下是我的代码:
fulltable_aggfunction_sets_aggtable(Full,Func,Sets,Aggtable):-
Full =[Features|Data],
flist_sets_indexs(Features,Sets,Indexs),
maplist(indexs_flist_sets(Indexs),Data,Datasplits),
maplist(aggfun_listoflists_values(Func),Datasplits,AggData),
list_indexes(Sets,SetIndex),
maplist(name_set_id_feature(Func),Sets,SetIndex,FeatureNames),
append([FeatureNames],AggData,Aggtable).
name_set_id_feature(Func,Set,Id,feature(Id,Func,Set)).
list_indexes(List,Indexes):-
findall(I,nth1(I,List,_),Indexes).
aggfunc_list_value(sum,List,Value):-
sumlist(List,Value).
aggfunc_list_value(mean,List,Value):-
sumlist(List,Sum),
length(List,L),
Value is Sum/L.
aggfun_listoflists_values(Fun,ListsofLists,Values):-
maplist(aggfunc_list_value(Fun),ListsofLists,Values).
my_nth0(List,Elem,I):- nth0(I,List,Elem).
indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).
flist_sets_indexs(Features,Sets,Indexs):-
maplist(flist_set_indexes(Features),Sets,Indexs).
flist_set_indexes(Features,Set,Indexs):-
maplist(my_nth0(Features),Set,Indexs).
%aux
print_data(Data_set):-
maplist(print_line,Data_set).
print_line(Data_line):-
format("~w ~n",[Data_line]).
data(Data):-
Data =[[a,b,c,d,e,f,g,class],
[1,1,1,1,1,1,1,0],
[2,3,4,5,4,2,1,0],
[3,1,3,4,6,7,8,1],
[1,2,3,6,1,1,2,1]].
在我的代码中,我有这两行只是重新排序参数,以便我可以将这些规则传递到映射列表中。
my_nth0(List,Elem,I):- nth0(I,List,Elem).
indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).
有一个更好的方法吗?在这种情况下如何使用 maplist 以免必须定义这些规则?