コード例 #1
0
ファイル: Task3.py プロジェクト: jayanth99/Machine-Learning-
def classify_example(example, tree):
    question = list(tree.keys())[0]
    if len(question.split()) == 3:
        feature_name = (question.split())[0]
    elif len(question.split()) == 4:
        feature_name = (question.split())[0] + " " + (question.split())[1]
    else:
        feature_name = (question.split())[0] + " " + (
            question.split())[1] + " " + (question.split())[2]

    # asking question
    if example[feature_name] == 0:
        question1 = list(tree.keys())[0]
        answer = tree[question1][0]
    elif example[feature_name] == 1:
        question2 = list(tree.keys())[1]
        answer = tree[question2][0]
    elif example[feature_name] == 2:
        question3 = list(tree.keys())[2]
        answer = tree[question3][0]
    else:
        question4 = list(tree.keys())[3]
        answer = tree[question4][0]

    # base case
    if not isinstance(answer, dict):
        return answer

    # recursive part
    else:
        residual_tree = answer
        return classify_example(example, residual_tree)
コード例 #2
0
ファイル: tree.py プロジェクト: dorian821/mt-opt
def decompressor(direct, symb):
    master_tree = {}
    master_imp = {}
    master_val = {}
    folders = os.listdir(direct.forest_dir + symb + '\\')
    for folder in folders:
        files = os.listdir(direct.forest_dir + symb + '\\' + folder + '\\')
        forest = str([f for f in files if 'crit' in f][0])
        imp = str([f for f in files if 'imp' in f][0])
        val = str([f for f in files if 'val' in f][0])
        forest = load_pickle(direct.forest_dir + symb + '\\' + folder + '\\' +
                             forest)
        imp = load_pickle(direct.forest_dir + symb + '\\' + folder + '\\' +
                          imp)
        val = load_pickle(direct.forest_dir + symb + '\\' + folder + '\\' +
                          val)
        for t in forest.keys():
            tree = forest[t]
            imps = imp[t]
            vals = val[t]
            print(tree.keys(), imps.keys(), vals)
            if len(tree) > 0:
                for b in tree.keys():
                    master_tree[len(master_tree) + 1] = tree[b]
                    master_imp[len(master_imp) + 1] = imps[b]
                    master_val[len(master_val) + 1] = vals[b]
    return master_tree, master_imp, master_val
コード例 #3
0
def predict_example(x, tree):
    
    try:
        len(tree.keys())
        
    except Exception:
        return tree
    
    keys=tree.keys()
    item = list(keys)[0]
    
    if x[item[0]] == item[1]:
        return predict_example(x, tree[(item[0], item[1], True)])
    else:
        return predict_example(x, tree[(item[0], item[1], False)])
コード例 #4
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.
    Returns the predicted label of x according to tree
    """

    if type(tree) != dict:
        return tree

    feature = int(list(tree.keys())[0][0]) - 1
    feature_val = list(tree.keys())[0][1]
    if x[feature] == feature_val:
        return predict_example(x, tree[list(tree.keys())[1]])
    else:
        return predict_example(x, tree[list(tree.keys())[0]])
コード例 #5
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.
    Returns the predicted label of x according to tree
    """

    # INSERT YOUR CODE HERE. NOTE: THIS IS A RECURSIVE FUNCTION.
    # tree[attr,value,t/f]
    #label = list(tree.values())[0]
    #if(label==0 or label==1):
    #    return label
    lst = list(tree.keys())[0]
    i = lst[0]
    v = lst[1]
    if (x[i] == v):
        label = tree.get((i, v, True))
        if (label == 0 or label == 1):
            return label
        return predict_example(x, label)
    else:
        label = tree.get((i, v, False))
        if (label == 0 or label == 1):
            return label
        return predict_example(x, label)
    raise Exception('Function not yet implemented!')
コード例 #6
0
ファイル: decisionTree.py プロジェクト: Plasticinew/AI_lab
def get_num_leafs(tree):
    num_leaves = 0
    for key in tree.keys():
        if type(tree[key]).__name__ == 'dict':
            num_leaves += get_num_leafs(tree[key])
        else:
            num_leaves += 1
    return num_leaves
コード例 #7
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.

    Returns the predicted label of x according to tree
    """

    # INSERT YOUR CODE HERE. NOTE: THIS IS A RECURSIVE FUNCTION.
    try:
        len(list(tree.keys()))
    except Exception:
        return tree
    cur = list(tree.keys())[0]
    if x[cur[0]] == cur[1]:
        return predict_example(x, tree[(cur[0], cur[1], True)])
    else:
        return predict_example(x, tree[(cur[0], cur[1], False)])
コード例 #8
0
ファイル: ID3.py プロジェクト: abegjeff/ID3
def printTree(tree,name):
	if type(tree) == dict:
		keys = list(tree.keys())
		values = list(tree.values())
		print(name, keys[0])
		for item in values[0]:
			print(name, item)
			printTree(values[0][item], name + "\t")
	else:
		print(name, "\t->\t", tree)
コード例 #9
0
ファイル: decisionTree.py プロジェクト: Plasticinew/AI_lab
def get_tree_depth(tree):
    max_depth = 0
    for key in tree.keys():
        if type(tree[key]).__name__ == 'dict':
            this_depth = 1 + get_tree_depth(tree[key])
        else:
            this_depth = 1
        if this_depth > max_depth:
            max_depth = this_depth
    return max_depth
コード例 #10
0
def get_tree_height(tree):
    if not isinstance(tree, dict):
        return 1
    maxheight = 0
    keys = [k for k in tree.keys()]
    childrentrees = [tree[key] for key in keys]
    for childtree in childrentrees:
        childheight = 1 + get_tree_height(childtree)
        if childheight > maxheight:
            maxheight = childheight
    return maxheight
コード例 #11
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.

    Returns the predicted label of x according to tree
    """

    try:
        l = len(tree.keys())

    except Exception as e:
        return tree

    term = list(tree.keys())[0]

    if x[term[0]] == term[1]:
        return predict_example(x, tree[(term[0], term[1], True)])
    else:
        return predict_example(x, tree[(term[0], term[1], False)])
コード例 #12
0
def print_tree(tree, str=''):
    """
    This function recursively crawls through the d-tree and prints it out in a
    more readable format than a straight print of the Python dict object.  
    """
    if type(tree) == dict:
        print "%s%s" % (str, tree.keys()[0])
        for item in tree.values()[0].keys():
            print "%s\t%s" % (str, item)
            print_tree(tree.values()[0][item], str + "\t")
    else:
        print "%s\t->\t%s" % (str, tree)
コード例 #13
0
def getNodeCount(tree):

    keys = list(tree.keys())
    # print(keys)
    if type(tree) is dict:
        nodes = 1
        if type(tree[keys[0]]) is dict:
            nodes += getNodeCount(tree[keys[0]])

        if type(tree[keys[1]]) is dict:
            nodes += getNodeCount(tree[keys[1]])
    return nodes
コード例 #14
0
def getTreeDepth(tree):
    maxDepth = 0
    rootNode = list(tree.keys())[0]
    leftTree = tree[rootNode]
    # single node
    if type(leftTree).__name__ != 'dict':
        return 1
    for key in leftTree:
        if type(leftTree[key]).__name__ == 'dict':
            if getTreeDepth(leftTree) + 1 > maxDepth:
                maxDepth = getTreeDepth(leftTree) + 1
    return maxDepth
コード例 #15
0
def predict(query,tree,default = 1):
    for key in list(query.keys()):
        if key in list(tree.keys()):
            try:
                result = tree[key][query[key]] 
            except:
                return default
            result = tree[key][query[key]]           
            if isinstance(result,dict):
                return predict(query,result)
            else:
                return result
コード例 #16
0
ファイル: DescisionTree.py プロジェクト: shubhscoder/lp3
def predict_class(sample, tree):
    condition = list(tree.keys())[0]
    attribute, val = condition.split("<=")
    attribute = attribute[:-1]
    val = val[1:]
    if sample[attribute] <= float(val):
        ans = tree[condition][0]
    else:
        ans = tree[condition][1]
    if isinstance(ans, dict) == False:
        return ans
    return predict_class(sample, ans)
コード例 #17
0
def getNumLeafs(tree):
    numLeafs = 0
    rootNode = list(tree.keys())[0]
    leftTree = tree[rootNode]
    # single node
    if type(leftTree).__name__ != 'dict':
        return 1
    for key in leftTree.keys():
        if type(leftTree[key]).__name__ == 'dict':
            numLeafs += getNumLeafs(leftTree[key])
        else:
            numLeafs += 1
    return numLeafs
コード例 #18
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.

    Returns the predicted label of x according to tree
    """
    if tree == 0 or tree == 1 or tree == 2:
        return tree
    attr_pair = list(tree.keys())[0]
    key, val = attr_pair[0], attr_pair[1]
    res = bool(x[key] == val)
    return predict_example(x, tree[key, val, res])
コード例 #19
0
def predict_example(x, tree):

    if type(tree) != dict:
        return tree

    key = list(tree.keys())[0]
    node = key[0]
    value = key[1]
    if value == x[node]:
        tree = predict_example(x, tree[(node, value, True)])
    else:
        tree = predict_example(x, tree[(node, value, False)])
    return tree
    """
コード例 #20
0
 def predict(self, X):
     if self.tree is None:
         raise RuntimeError('cant predict before fit')
     y_pred = []
     for i in range(X.shape[0]):
         tree = self.tree
         x = X[i]
         while True:
             if not isinstance(tree, dict):
                 y_pred.append(tree)
                 break
             a = list(tree.keys())[0]
             tree = tree[a]
             if isinstance(tree, dict):
                 val = x[a]
                 split_val=float(list(tree.keys())[0][1:])
                 if val<=split_val:
                     tree=tree[list(tree.keys())[0]]
                 else:
                     tree=tree[list(tree.keys())[1]]
             else:
                 y_pred.append(tree)
                 break
     return np.array(y_pred)
コード例 #21
0
def predict(inst,tree):
    for nodes in tree.keys():             
        value = inst[nodes]
        keys=list(tree[nodes].keys())
        for key in keys:
            if value >= key[0] and value <key[1]:
                tree=tree[nodes][key]
                break
        prediction=0
        if type(tree) is dict:
            prediction = predict(inst, tree)
        else:
            prediction = tree
            break;                            
        
    return prediction
コード例 #22
0
    def predict(self, tree, features, x):
        '''
        预测x 属于哪一个类别

        '''
        # 由根节点不断向下匹配,直到匹配叶子结点返回类别
        for key1 in tree.keys():
            secondDict = tree[key1]
            featIndex = features.index(key1)

            for key2 in secondDict.keys():
                if x[featIndex] == key2:
                    if type(secondDict[key2]).__name__ == 'dict':
                        classLabel = self.predict(secondDict[key2], features,
                                                  x)
                    else:
                        classLabel = secondDict[key2]
        return classLabel
コード例 #23
0
ファイル: ID3.py プロジェクト: abegjeff/ID3
def classify(tree,datapoint):
	#featureNames = ['setosa', 'versicolour', 'virginica']
	#featureNames = ['0', '1', '2']
	if type(tree) == type("string"):
		# Have reached a leaf
		return tree
	else:
		keys = list(tree.keys())
		a = keys[0]
		for i in range(len(featureNames)):
			if featureNames[i]==a:
				break
		
		try:
			t = tree[a][datapoint[i]]
			return classify(t,datapoint)
		except:
			print(sys.exc_info()[0])
			return None
コード例 #24
0
def predict_example(x, tree):
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.

    Returns the predicted label of x according to tree
    """

    # INSERT YOUR CODE HERE. NOTE: THIS IS A RECURSIVE FUNCTION.
    # print(tree.keys())
    keys = list(tree.keys())
    attribute = keys[0][0]
    value = keys[0][1]
    # print("attribute = {}\n".format(attribute))
    if (x[attribute - 1] == value):  # true
        result = tree[keys[1]]
    if (x[attribute - 1] != value):  # false
        result = tree[keys[0]]

    if type(result) is dict:
        return predict_example(x, result)
    else:
        return result
コード例 #25
0
            else:
                weights[index] = weights[index] * np.exp(-weighted_alpha)
        weights = weights / (2 * np.sqrt(weighted_error * (1 - weighted_error)))
        h_ens.append((weighted_alpha, model))
    return h_ens

def predict_example(x, h ens),
    """
    Predicts the classification label for a single example x using tree by recursively descending the tree until
    a label/leaf node is reached.

    Returns the predicted label of x according to tree
    """

    try:
        l = len(tree.keys())
        
    except Exception as e:
        return tree
    
    term = list(tree.keys())[0]
    
    # Traversing the tree based on the test value
    if x[term[0]] == term[1]:
        return predict_example(x, tree[(term[0], term[1], True)])
    else:
        return predict_example(x, tree[(term[0], term[1], False)])


def compute_error(y_true, y_pred, w=None):
    """