How to use _node_to_string method in localstack

Best Python code snippet using localstack_python

__init__.py

Source:__init__.py Github

copy

Full Screen

...109def test_tree_build(DecisionTree, entropy_func):110 tester = Tester()111 correct_tree = np.load(os.path.join(current_folder, "tree_depth3_min2_entropy.npy"), allow_pickle=True)[0]112 topic = "Testing DecisionTree build with depth 3 and min_samples_split 2"113 expected_tree = "\n".join(_node_to_string(correct_tree.root, features_names)[0])114 ins = ", using Problem1 features, and labels"115 def test_build():116 tree = DecisionTree(max_depth=3, min_samples_split=2, impurity_measure=entropy_func).fit(features, labels)117 obtained_tree = "\n".join(_node_to_string(tree.root, features_names)[0])118 comment = topic + ins + "\n expected output: \n" + expected_tree + "\n obtained: " + obtained_tree119 if compare_trees(correct_tree, tree):120 return True, comment121 return False, comment122 tester.add_test("1.6", test_build)123 tester.run()124def _node_to_string(root_node, features_names):125 if root_node is None:126 return "None"127 if 'leaf' in str(type(root_node)).lower():128 return [VERTICAL + "label: %i" % root_node.label], 0129 else:130 string = ["%s" % features_names[root_node.feature_id], "%.2f" % root_node.threshold]131 max_len = max([len(s) for s in string])132 string[0] = "|" + string[0] + " " * (max_len - len(string[0])) + VERTICAL + OUT_UP133 string[1] = "|" + string[1] + " " * (max_len - len(string[1])) + VERTICAL + OUT_DOWN134 left, left_pos = _node_to_string(root_node.left_child, features_names)135 right, right_pos = _node_to_string(root_node.right_child, features_names)136 for i in range(0, left_pos):137 left[i] = " " + left[i]138 left[left_pos] = UP_IN + left[left_pos]139 for i in range(left_pos + 1, len(left)):140 left[i] = VERTICAL + left[i]141 for i in range(0, right_pos):142 right[i] = VERTICAL + right[i]143 right[right_pos] = DOWN_IN + right[right_pos]144 for i in range(right_pos + 1, len(right)):145 right[i] = " " + right[i]146 left = [" " * (max_len + 2) + l for l in left]147 right = [" " * (max_len + 2) + r for r in right]148 return left + string + right, len(left)149def _compare_node(node1, node2):150 # Not equal if one is leaf and another is parent151 if type(node1) != type(node2):152 return False153 # leaf nodes are equal if they have the same label154 if 'leaf' in str(type(node1)).lower():155 return node1.label == node2.label156 # parent nodes are equal if they have the same feature_id, threshold, and equal children157 compare_values = (node1.feature_id == node2.feature_id) and np.isclose(node1.threshold, node2.threshold, atol=1e-5)158 if not compare_values:159 return False160 return _compare_node(node1.left_child, node2.left_child) and _compare_node(node1.right_child, node2.right_child)161def compare_trees(tree1, tree2):162 return _compare_node(tree1.root, tree2.root)163def print_tree(decision_tree, features_names=None):164 if features_names is None:165 features_names= ["feat_%i" % i for i in range(decision_tree.num_features)]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful