Skip to content
Snippets Groups Projects
Commit 308d0afc authored by Jakhes's avatar Jakhes
Browse files

Moving all extra predicates into a helper file

parent 1fe22107
No related branches found
No related tags found
No related merge requests found
:- module(helper, [convert_list_to_float_array/3, convert_list_to_float_array/2, fill_float_array/3, convert_float_array_to_list/3 , convert_float_array_to_list/4, len/2, convert_record_to_arr/2, take_csv_row/1]).
:- use_module(library(structs)).
:- use_module(library(csv)).
%% Functions for editing float arrays
convert_list_to_float_array(Arr, Row_num, array(Size, Row_num,Mem)) :-
len(Arr, Size),
Size >= Row_num,
Row_num > 0,
mod(Size, Row_num) =:= 0,
new(float_array, Size, Mem),
fill_float_array(Arr, 0, Mem).
convert_list_to_float_array(Arr, array(Size,Mem)) :-
len(Arr, Size),
new(float_array, Size, Mem),
fill_float_array(Arr, 0, Mem).
fill_float_array([], _, _):- !.
fill_float_array([H|Tail], Index, Mem) :-
put_contents(Mem, Index, H),
New_index is Index + 1,
fill_float_array(Tail, New_index, Mem).
convert_float_array_to_list(Mem, Count, Out) :-
convert_float_array_to_list(Mem, 0, Count, Out).
convert_float_array_to_list(_, Count, Count, []) :- !.
convert_float_array_to_list(Mem, Index, Count, [Val|Rest]) :-
NewIndex is Index + 1,
convert_float_array_to_list(Mem, NewIndex, Count, Rest),
get_contents(Mem, Index , Val).
%% returns the length of a list
len([], 0):- !.
len([_|Tail], List_L) :-
len(Tail, Tail_L),
List_L is Tail_L + 1.
%% Funktions for reading the csv Files
%% take the elements in a csv record and put them in a list
convert_record_to_arr([], []):- !.
convert_record_to_arr([float(Num,_)|Tail], [Num|Rest]) :-
convert_record_to_arr(Tail, Rest).
convert_record_to_arr([string(_)|Tail], Rest) :-
convert_record_to_arr(Tail, Rest).
take_csv_row(Out) :-
open('/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv', read, A),
read_record(A, _),
read_record(A, Rec),
convert_record_to_arr(Rec, Out).
\ No newline at end of file
:- module(bayesian_linear_regression, [initModel/4, alpha/1, beta/1, dataOffset/2, dataScale/2, omega/2, predict/5, predictWithStd/7, rmse/6, train/5, variance/1]).
:- load_files(library(str_decl), :- load_files(library(str_decl),
[when(compile_time), if(changed)]). [when(compile_time), if(changed)]).
%% needed for using the array type and for reading from csv %% needed for using the array type and for reading from csv
:- use_module(library(structs)). :- use_module(library(structs)).
:- use_module(library(csv)). :- use_module(library(csv)).
:- use_module('/home/afkjakhes/git/prolog-mlpack-libary/src/helper_files/helper').
%% type definitions for the float array %% type definitions for the float array
:- foreign_type :- foreign_type
...@@ -29,60 +32,10 @@ foreign_resource(bayesian_linear_regression, [initModel, alpha, beta, dataOffset ...@@ -29,60 +32,10 @@ foreign_resource(bayesian_linear_regression, [initModel, alpha, beta, dataOffset
:- load_foreign_resource(bayesian_linear_regression). :- load_foreign_resource(bayesian_linear_regression).
%% Functions for editing float arrays
convert_list_to_float_array(Arr, Row_num, array(Size, Row_num,Mem)) :-
len(Arr, Size),
new(float_array, Size, Mem),
fill_float_array(Arr, 0, Mem).
convert_list_to_float_array(Arr, array(Size,Mem)) :-
len(Arr, Size),
new(float_array, Size, Mem),
fill_float_array(Arr, 0, Mem).
fill_float_array([], _, _):- !.
fill_float_array([H|Tail], Index, Mem) :-
put_contents(Mem, Index, H),
New_index is Index + 1,
fill_float_array(Tail, New_index, Mem).
convert_float_array_to_list(Mem, Count, Out) :-
convert_float_array_to_list(Mem, 0, Count, Out).
convert_float_array_to_list(_, Count, Count, []) :- !.
convert_float_array_to_list(Mem, Index, Count, [Val|Rest]) :-
NewIndex is Index + 1,
convert_float_array_to_list(Mem, NewIndex, Count, Rest),
get_contents(Mem, Index , Val).
%% Funktions for reading the csv Files
%% returns the length of a list
len([], 0):- !.
len([_|Tail], List_L) :-
len(Tail, Tail_L),
List_L is Tail_L + 1.
%% take the elements in a csv record and put them in a list
convert_record_to_arr([], []):- !.
convert_record_to_arr([float(Num,_)|Tail], [Num|Rest]) :-
convert_record_to_arr(Tail, Rest).
convert_record_to_arr([string(_)|Tail], Rest) :-
convert_record_to_arr(Tail, Rest).
take_csv_row(Out) :-
open('/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv', read, A),
read_record(A, _),
read_record(A, Rec),
convert_record_to_arr(Rec, Out).
%% Some funktions that use the BayesianLinearRegression %% Some funktions that use the BayesianLinearRegression
%% First trains the model and then tries to predict the targets and the std for the given data
predict(PredictList, StdList) :- predict(PredictList, StdList) :-
train, train,
convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)), convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)),
...@@ -90,6 +43,7 @@ predict(PredictList, StdList) :- ...@@ -90,6 +43,7 @@ predict(PredictList, StdList) :-
convert_float_array_to_list(Predic, PredicSize, PredictList), convert_float_array_to_list(Predic, PredicSize, PredictList),
convert_float_array_to_list(Std, StdSize, StdList). convert_float_array_to_list(Std, StdSize, StdList).
%% Trains the model with the given data and targets
train :- train :-
convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)), convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)),
convert_list_to_float_array([0.2,0.2,0.2,0.2], array(Ysize, Y)), convert_list_to_float_array([0.2,0.2,0.2,0.2], array(Ysize, Y)),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment