3

「完全な」データ テーブルと一連の列ヘッダー (機能) のセット、集計関数、および集計テーブルの間の関係を定義するプログラムを作成しました。

クエリの例:

 ?- 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]].

私のコードでは、これらのルールをマップリストに渡すことができるように、引数を並べ替えるだけのこれらの 2 行があります。

my_nth0(List,Elem,I):- nth0(I,List,Elem).

indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).

これを行うより良い方法はありますか?これらのルールを定義する必要がないように、この場合に maplist を使用するにはどうすればよいですか?

4

2 に答える 2

2

SWI-Prolog には、ライブラリ ( yall ) とライブラリ ( lambda ) があります。

たとえば、ライブラリ(yall)を使用する

flist_set_indexes(Features,Set,Indexs):-
 maplist({Features}/[Elem,I]>>nth0(I,Features,Elem),Set,Indexs).

print_data(Data_set):-
 maplist([Data_line]>>format("~w ~n",[Data_line]),Data_set).

2番目を使用している間

:- use_module(library(lambda)).

flist_set_indexes(Features,Set,Indexs):-
 maplist(\Elem^I^nth0(I,Features,Elem),Set,Indexs).

%aux
print_data(Data_set):-
 maplist(\Data_line^format("~w ~n",[Data_line]),Data_set).

ライブラリには同様の機能があります。ただし、yall には の「キャプチャ」が必要であることに注意してくださいFeatures。ライブラリ(ラムダ)は後に利用可能です

?- pack_install(lambda).

ライブラリ(yall)が自動ロードされている間

于 2016-04-12T19:43:26.893 に答える