Beispiel #1
0
def main():
    '''main method'''
    targets = argv[argv.index("-target") + 1][1:-1].split(
        ',')  #read targets from input
    regression, advice = False, False
    if "-reg" in argv:
        regression = True
    if "-expAdvice" in argv:
        advice = True
    for target in targets:
        data = Utils.readTrainingData(target, regression,
                                      advice)  #read training data
        numberOfTrees = 10  #number of trees for boosting
        trees = []  #initialize place holder for trees
        for i in range(numberOfTrees):  #learn each tree and update gradient
            print('=' * 20, "learning tree", str(i), '=' * 20)
            node.setMaxDepth(2)
            node.learnTree(data)  #learn RRT
            trees.append(node.learnedDecisionTree)
            Boosting.updateGradients(data, trees)
        for tree in trees:
            print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
            for clause in tree:
                print(clause)
        testData = Utils.readTestData(target, regression)  #read testing data
        Boosting.performInference(testData,
                                  trees)  #get probability of test examples
Beispiel #2
0
    def learn_clf(self, facts, pos, neg, bk):
        '''learns classifier'''

        self.trees = {}
        for target in self.targets:
            data = Utils.setTrainingData(target=target,
                                         facts=facts,
                                         pos=pos,
                                         neg=neg,
                                         bk=bk,
                                         regression=self.regression,
                                         sampling_rate=self.sampling_rate)
            trees = []
            for i in range(self.numberOfTrees):
                print('=' * 20, "learning tree", str(i + 1), '=' * 20)
                node.setMaxDepth(self.treeDepth)
                node.learnTree(data)
                trees.append(node.learnedDecisionTree)
                Boosting.updateGradients(data, trees)
            self.trees[target] = trees
        for target in self.targets:
            if target in self.trees:
                number_of_trees = len(self.trees[target])
                counter = 1
                for tree in self.trees[target]:
                    print('=' * 30, "tree", str(counter), '=' * 30)
                    for clause in tree:
                        print(clause)
                    counter += 1
 def learn(self, facts, examples, bk):
     """Learn the clause."""
     for target in self.targets:
         data = Utils.setTrainingData(target=target,
                                      facts=facts,
                                      examples=examples,
                                      bk=bk,
                                      regression=self.regression,
                                      sampling_rate=self.sampling_rate)
         trees = []
         treeLCAs = []
         for i in range(self.numberOfTrees):
             print('=' * 20, "learning tree", str(i), '=' * 20)
             node.setMaxDepth(self.treeDepth)
             node.learnTree(data)
             trees.append(node.learnedDecisionTree)
             treeLCAs.append(node.LCAS)
             Boosting.updateGradients(data, trees, loss=self.loss)
     self.trees[target] = trees
     self.treeLCAs[target] = treeLCAs
     for tree in trees:
         print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
         for clause in tree:
             print(clause)
     '''
Beispiel #4
0
    def learn(self, facts=None, examples=None, bk=None, pos=None, neg=None):
        '''learns the regression tree per target
           before learning, make sure that the training and test
           and bk files are all in the default format.
           change all formats as required as required during runtime.
	'''
        for target in self.targets:  #for every target learn
            data = Utils.setTrainingData(target=target,
                                         facts=facts,
                                         examples=examples,
                                         bk=bk,
                                         regression=self.regression,
                                         sampling_rate=self.sampling_rate,
                                         pos=pos,
                                         neg=neg)
            trees = []  #initialize place holder for trees
            for i in range(self.numberOfTrees):
                print('=' * 20, "learning tree", str(i), '=' * 20)
                node.setMaxDepth(
                    self.treeDepth)  #set max depth of individual tree learned
                node.learnTree(data)  #learn the regression tree
                trees.append(node.learnedDecisionTree)
                Boosting.updateGradients(data, trees, loss=self.loss)
        self.trees[target] = trees
        for tree in trees:  #print each tree learned
            print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
            for clause in tree:
                print(clause)
 def infer(self,facts,examples):
     self.testExamples = {}
     for target in self.targets:
         data = Utils.setTestData(target=target,facts=facts,examples=examples,regression=self.regression)
         Boosting.performInference(data,self.trees[target])
         self.testExamples[target] = data.examples
         valueset = set(list(data.examples.values()))
         print (data.examples)
Beispiel #6
0
 def infer(self, facts, examples):
     '''perform inference on learned trees'''
     self.testExamples = {}
     for target in self.targets:
         data = Utils.setTestData(target=target,
                                  facts=facts,
                                  examples=examples,
                                  regression=self.regression)
         Boosting.performInference(data, self.trees[target])
         self.testExamples[target] = data.examples
         print(data.examples)
Beispiel #7
0
def main():
    '''main method'''
    parameters = Arguments().args
    #print(parameters)

    for target in parameters.target:

        # Read the training data.
        data = Utils.readTrainingData(target,
                                      path=parameters.train,
                                      regression=parameters.reg,
                                      advice=parameters.expAdvice)

        # Initialize an empty place holder for the trees.
        trees = []

        # Learn each tree and update the gradient.
        for i in range(parameters.trees):

            if parameters.verbose:
                print('=' * 20, "learning tree", str(i), '=' * 20)

            node.setMaxDepth(2)
            node.learnTree(data)  # Learn relational regression tree
            trees.append(node.learnedDecisionTree)
            Boosting.updateGradients(data, trees)

        for tree in trees:

            if parameters.verbose:
                print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)

            for clause in tree:
                print(clause)

        # Read the testing data.
        testData = Utils.readTestData(target,
                                      path=parameters.test,
                                      regression=parameters.reg)
        # Get the probability of the test examples.
        Boosting.performInference(testData, trees)

        if parameters.reg:
            # View test example values (for regression)
            print(testData.examples)
        else:
            # View test query probabilities (for classification)
            print(testData.pos)
            print(testData.neg)
Beispiel #8
0
def grad_boost():
    classification_data, classification_labels = datasets.make_classification(
        n_samples=1000,
        n_features=2,
        n_informative=2,
        n_classes=2,
        n_redundant=0,
        n_clusters_per_class=1,
        random_state=23)
    train_data, test_data, train_labels, test_labels = model_selection.train_test_split(
        classification_data,
        classification_labels,
        test_size=0.3,
        random_state=1)

    n_trees = 10
    max_depth = 60
    num_leaves = 5
    min_samples_leaf = 5
    criteria = 'gini'

    model_b = Boosting(n_trees=n_trees,
                       max_depth=max_depth,
                       num_leaves=num_leaves,
                       min_samples_leaf=min_samples_leaf,
                       criteria=criteria)
    model_b.fit(train_data, train_labels)
    train_answers = model_b.predict(train_data)
    test_answers = model_b.predict(test_data)

    # Точность на обучающей выборке
    train_accuracy = accuracy_metric(train_labels, train_answers)
    print('\n\nГрадиентный бустинг\n')
    print(f'n_trees={n_trees}, max_depth={max_depth}, \
            num_leaves={num_leaves}, \
            min_samples_leaf={min_samples_leaf}, \
            criteria={criteria}')
    print(f' Точность на обучающей выборке: {train_accuracy:.3f}')

    # Точность на тестовой выборке
    test_accuracy = accuracy_metric(test_labels, test_answers)
    print(f' Точность на тестовой выборке: {test_accuracy:.3f}')
Beispiel #9
0
if __name__ == '__main__':
    print(
        "Please select the number against the learner you want to build the model for datasets:"
    )
    print("1 -> Decision Tree")
    print("2 -> K-NN")
    print("3 -> Neural Networks")
    print("4 -> Boosting")
    print("5 -> SVM")

    switcher = {
        1: DecisionTree(),
        2: KNN(),
        3: NeuralNetworks(),
        4: Boosting(),
        5: SVM(),
    }
    datasets = dataset_loader.load_datasets()
    model_number = input("Enter your value: ")

    model = switcher.get(int(model_number), "Invalid learner")

    print("Please select the dataset:")
    print("1 -> Diabetes Retinatherapy")
    print("2 -> Phishing Website")
    dataset_number = input("Enter your value: ")
    if int(dataset_number) == 1:
        item = datasets[0]
    else:
        item = datasets[1]