Commit 54c4fc34 authored by Fabrizio Detassis's avatar Fabrizio Detassis
Browse files

.

parents bd470d3d 9b5100e0
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
import importlib
import numpy as np
import sklearn
......@@ -12,8 +13,34 @@ from aiddl_core.parser.parser import parse_term
from aiddl_core.representation.symbolic import Symbolic
from aiddl_core.representation.integer import Integer
from aiddl_core.representation.list import List
from aiddl_core.representation.collection import Collection
from aiddl_core.function.function import Function
class SciKitLearnFunction(Function):
def __init__(self, cfg):
self.configure(cfg, None)
def configure(self, cfg, freg):
py_module = cfg[Symbolic("py_module")]
py_class = cfg[Symbolic("py_class")]
args = {}
for e in cfg:
if isinstance(e, Collection):
if e.get_key() != Symbolic("py_module") and e.get_key() != Symbolic("py_class"):
args[e.get_key().unpack()] = e.get_value().unpack()
m = importlib.import_module(str(py_module))
self.model = getattr(m, str(py_class))(**args)
def apply(self, mlProblem):
x, y = splitAiddlMlProblem(mlProblem)
print(y)
self.model.fit(x, y)
y_p = self.model.predict(x)
r = []
for e in y_p:
r.append(parse_term(str(e)))
return List(r)
class RandomForestLearner(Function):
def __init__(self):
self.n_estimators = 50
......@@ -73,9 +100,12 @@ def splitAiddlMlProblem(mlProblem):
x = []
labelIdx = -1
for i in range(len(atts)):
# print(atts[i][0], type(atts[i][0]), label, type(label), atts[i][0] == label)
if atts[i][0] == label:
labelIdx = i
break
if labelIdx == -1:
raise ValueError("Label not found.")
c = 0
for row in data:
x_row = []
......@@ -85,7 +115,7 @@ def splitAiddlMlProblem(mlProblem):
else:
x_row.append(row[i].unpack())
print(c, row[i], type(row[i]), type(row[i].unpack()))
# print(c, row[i], type(row[i]), type(row[i].unpack()))
c += 1
x.append(x_row)
return x, y
from aiddl_core.parser.parser import parse_term
from aiddl_core.representation.key_value import KeyValue
from aiddl_core.representation.symbolic import Symbolic
from aiddl_core.representation.string import String
from aiddl_core.representation.integer import Integer
from aiddl_core.representation.tuple import Tuple
from aiddl_core.representation.list import List
from aiddl_core.representation.collection import Collection
from aiddl_core.function.function import Function
class CsvLoader(Function):
def apply(self, args):
fName = str(args[0].get_string_value())
sep = str(args[1].get_string_value())
label = args[2]
f = open(fName, "r")
lines = f.readlines()
f.close()
atts = []
for att in lines[0].split(sep):
if att.strip() != "":
atts.append(Tuple([String(att.strip()), Symbolic("org.aiddl.term")]))
data = []
for row in lines[1:]:
if row.strip() != "":
data_row = []
for e in row.split(sep):
if e.strip() != "":
data_row.append(parse_term(e.strip()))
data.append(List(data_row))
f.close()
return Tuple([
KeyValue(Symbolic("attributes"), List(atts)),
KeyValue(Symbolic("label"), label),
KeyValue(Symbolic("data"), List(data))])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment