Skip to content
Snippets Groups Projects
Commit 4810bdc3 authored by aastorg2's avatar aastorg2
Browse files

can add positive and negative examples to learner files

parent 74c5446a
No related branches found
No related tags found
No related merge requests found
from typing import List, Tuple
import numpy as np
import os
from learner_base import LearnerBase
def construct_sample_to_feature_func(a_mat: np.ndarray, b_vec: np.ndarray):
perc_dim, state_dim = a_mat.shape
def sample_to_feature_vec(sample):
assert len(sample) == state_dim + perc_dim
state = sample[0: state_dim]
perc = sample[state_dim: state_dim+perc_dim]
perc_bar = perc - (np.dot(state, a_mat.T) + b_vec)
return perc_bar
return sample_to_feature_vec
import csv
class DTreeLearner(LearnerBase):
def __init__(self, state_dim: int, perc_dim: int,
timeout: int = 10000) -> None:
super().__init__()
#learners/C50exact/c5.0dbg -I 1 -m 1 -f FileLocation/pre
self._state_dim: int = state_dim
self._perc_dim: int = perc_dim
self._s2f_func_list: List = []
dir_name = "tempLocation"
check_temp_dir: bool = os.path.isdir(dir_name)
if not check_temp_dir:
os.makedirs(dir_name)
self.data_file = dir_name+"/pre.data"
self.names_file = dir_name+"/pre.names"
open(self.data_file,'w').close() # create empty file
open(self.names_file,'w').close()
@property
def state_dim(self) -> int:
return self._state_dim
......@@ -51,8 +49,7 @@ class DTreeLearner(LearnerBase):
feature_vec_list.append(feature_vec)
print("Positive feature vectors (dbar, psibar):", feature_vec_list)
# TODO save list of feature vectors to data as positive examples
self.write_to_file(self.data_file,feature_vec_list, "true")
def add_negative_examples(self, *args) -> None:
feature_vec_list = []
......@@ -63,14 +60,54 @@ class DTreeLearner(LearnerBase):
feature_vec_list.append(feature_vec)
print("Negative feature vectors (dbar, psibar):", feature_vec_list)
self.write_to_file(self.data_file, feature_vec_list, "false")
def write_to_file(self, file:str, feature_vec_list, label:str ):
with open(file, 'a') as d_file:
data_out = csv.writer(d_file)
for f in feature_vec_list:
print(f)
data_out.writerow(f+[label])
# TODO save list of feature vectors to data as negative examples
def learn(self) -> Tuple:
# TODO read result from Dtree learner
pass
def construct_sample_to_feature_func(a_mat: np.ndarray, b_vec: np.ndarray):
perc_dim, state_dim = a_mat.shape
def sample_to_feature_vec(sample):
assert len(sample) == state_dim + perc_dim
state = sample[0: state_dim]
perc = sample[state_dim: state_dim+perc_dim]
perc_bar = perc - (np.dot(state, a_mat.T) + b_vec)
return perc_bar
return sample_to_feature_vec
def test_sample_to_feature():
# tuple
a_mat = np.array([[0., -1., 0.],
[0., 0., -1]])
b_vec = np.zeros(2)
#construct_sample_to_feature_func: returns a function
#map: lin_trans(a_mat and b_vec pair) -> func
sample_to_feature_func = construct_sample_to_feature_func(a_mat, b_vec)
#map = {name1:sample_to_feature_func}
sample = np.array([1., 2., 3., -2., -3.])
# sample_to_feature_func will compute dBar and psiBar
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([0., 0.]))
sample = np.array([1., 2., 3., -1., -2.])
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([1., 1.]))
def test_dtree_learner():
a_mat = np.array([[0., -1., 0.],
......@@ -85,7 +122,7 @@ def test_dtree_learner():
(1., 2., 3., -1., -2.)
]
learner.add_positive_examples(*pos_examples)
neg_examples = [
(10., 1.0, 1.0, 0.5, 0.5),
(10., 1.0, 1.0, 1.5, 1.5),
......@@ -93,23 +130,9 @@ def test_dtree_learner():
]
learner.add_negative_examples(*neg_examples)
learner.learn()
def test_sample_to_feature():
a_mat = np.array([[0., -1., 0.],
[0., 0., -1]])
b_vec = np.zeros(2)
sample_to_feature_func = construct_sample_to_feature_func(a_mat, b_vec)
sample = np.array([1., 2., 3., -2., -3.])
feature_vec = sample_to_feature_func(sample)
assert np.array_equal(feature_vec, np.array([0., 0.]))
sample = np.array([1., 2., 3., -1., -2.])
feature_vec = sample_to_feature_func(sample)
assert np.array_equal(feature_vec, np.array([1., 1.]))
# learner.learn()
if __name__ == "__main__":
#test_sample_to_feature()
test_dtree_learner()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment