def enter(): global forest, retsim, ui retsim = Retsim() forest = Forest() ui = Ui() forest.set_center_object(retsim)
class Map(): #translates the strings we receive as returns to which function to call next scenes = { 'birth': Birth(), 'grove': Grove(), 'metropolis': Metropolis(), 'death': Death(), 'doorway': Doorway(), 'forest': Forest(), 'plains': Plains(), 'elfkingdom': ElfKingdom() } #we provide a place to start def __init__(self, start_scene): self.start_scene = start_scene def next_scene(self, scene_name): val = Map.scenes.get(scene_name) return val #plays the first scene in the game def opening_scene(self): return self.next_scene(self.start_scene)
def test5(): f = Forest([11,5, 9,7]) f.rotate(1 ) # for i in range (3): f.rotate( 2 ) # f.draw(bg=" ") pass
def main(): debug = False threshold = 1 for i in range(0, len(sys.argv)): if sys.argv[i] == '-d': debug = True elif sys.argv[i] == '-t': threshold = sys.argv[i+1] elif sys.argv[i] == '-h': print "Usage: python main.py [OPTIONS]\n" print "--------- Options -------------" print "'-d' : debug mode" print "'-t [VALUE]' : set mean square error threshold to value" return (data, training, test) = readData( filename = 'whitewine.csv', debug = False, label_index = 11, variable_index = (0,10), separator= ';') forest = Forest(filename = 'whitewine.csv', label_index = 11, variable_index = (0,10), separator=';', mse_threshold= 0.02, debug = debug, num_trees=5) forest.build() best = forest.predict(test) print best
def format_service(path, idata, mon_data=None, discard_disabled=False, nodename=None): name, namespace, kind = split_path(path) svc_notice = get_svc_notice(idata) tree = Forest( separator=" ", widths=( (14, None), None, 10, None, ), ) node_name = tree.add_node() node_name.add_column(strip_path(path, os.environ.get("OSVC_NAMESPACE")), color.BOLD) node_name.add_column() if "cluster" in idata: node_name.add_column(idata["cluster"].get("avail", "n/a"), STATUS_COLOR[idata["cluster"].get("avail", "n/a")]) else: node_name.add_column() node_name.add_column(svc_notice) node_instances = node_name.add_node() node_instances.add_column("instances") add_instances(node_instances, path, nodename, mon_data) if nodename in service_nodes(path, mon_data): add_node_node(node_instances, nodename, idata, mon_data, discard_disabled=discard_disabled) add_parents(node_name, idata, mon_data, namespace) add_children(node_name, idata, mon_data, namespace) add_scaler_slaves(node_name, idata, mon_data, namespace) add_slaves(node_name, idata, mon_data, namespace) tree.out()
def test_forest_class(): """ Forest class """ tree = Forest() tree.load({}) tree.out() overall_node = tree.add_node() overall_node.add_column("overall") node = overall_node.add_node() node.add_column("avail") node.add_column() node.add_column("up", color.GREEN) node = node.add_node() node.add_column("res#id") node.add_column("....") node.add_column("up", color.GREEN) col = node.add_column( "docker container [email protected]" "nsvc.com/busybox:latest") col.add_text("warn", color.BROWN) col.add_text("err", color.RED) node = overall_node.add_node() node.add_column("accessory") node = overall_node.add_node() node.load("loaded text", title="loaded title") node = overall_node.add_node() node.load({"text": "loaded dict"}) node = overall_node.add_node() node.load([{"text": "loaded list"}]) buff = str(tree) assert "loaded" in buff
def _print_schedule_default(self): """ Print the scheduling table in normal or detailed mode. """ from forest import Forest from rcColor import color tree = Forest() head_node = tree.add_node() head_node.add_column("Action", color.BOLD) head_node.add_column("Last Run", color.BOLD) if self.options.verbose: head_node.add_column("Next Run", color.BOLD) head_node.add_column("Config Parameter", color.BOLD) head_node.add_column("Schedule Definition", color.BOLD) for data in self._print_schedule_data(): node = head_node.add_node() node.add_column(data["action"], color.LIGHTBLUE) node.add_column(data["last_run"]) if self.options.verbose: node.add_column(data["next_run"]) node.add_column(data["config_parameter"]) node.add_column(data["schedule_definition"]) tree.out()
def do_forest(self): if self.flag == 0: start = time.time() forest_instance = Forest(self.train, None) forest_instance.build_forest() forest_instance.write_model(self.model_file) end = time.time() print 'Training Time :', (end - start) / 60, 'mins' else: start = time.time() forest_instance = Forest(None, self.test) forest_instance.load_model(self.model_file) test_output = forest_instance.test_forest(self.test, self.output_file) print test_output['accuracy'], '%' end = time.time() print 'Testing Time :', (end - start) / 60, 'mins'
def main(): (X_train, y_train), (X_test, y_test) = tf.contrib.keras.datasets.mnist.load_data() X_train = (X_train / 255.).reshape(-1, 28*28) X_test = (X_test / 255.).reshape(-1, 28*28) forest = Forest(28*28, 10) forest.fit(X_train, y_train) print("final testing accuracy: %.4f" % (forest.predict(X_test) == y_test).mean())
def predict(): trainingRowIds = random.sample(range(1, len(data)), int(.8 * len(data))) forest = Forest(data, outcomeLabel, continuousColumns, trainingRowIds, columnsToIgnore) correct = sum(1 for rowId, row in enumerate(data) if rowId > 0 and rowId not in trainingRowIds and forest.get_prediction(row) == row[1]) return 100 * correct / (len(data) - 1 - len(trainingRowIds))
def main(args): parser = argparse.ArgumentParser(description='Random forest classifier') parser.add_argument( 'training_dataset', help='CSV file containing the training dataset file', metavar='training_dataset', type=argparse.FileType('r'), ) parser.add_argument( 'test_dataset', help='CSV file containing the test dataset file', metavar='test_dataset', type=argparse.FileType('r'), ) parser.add_argument( '-t', '--target_column', help='Name of the target dataset column (default: last column)', metavar='target_column', type=str, ) args = parser.parse_args(args) training_rows = [ row for row in csv.DictReader( args.training_dataset, delimiter=',', quotechar='"') ] assert len(training_rows) > 0 training_columns = list(training_rows[0].keys()) target_column = args.target_column or training_columns[-1] assert target_column in training_columns type_cast(training_rows, training_columns) test_rows = [ row for row in csv.DictReader( args.test_dataset, delimiter=',', quotechar='"') ] test_columns = list(test_rows[0].keys()) type_cast(test_rows, test_columns) forest = Forest( columns=training_columns, target_column=target_column, rows=training_rows, ) # TODO CSV output for row in test_rows: print('{} -> {}'.format( ', '.join([ ':'.join((key, str(value))) for (key, value) in list(row.items()) ]), forest.classify(row)))
def to_forest(self): """ Returns a L{Forest} of graphs. @rtype: L{Forest} @return: a forest containing (weakly) disconnected graph components """ from forest import Forest graphs = [self.induced_graph(component) for component in self.weak_components()] return Forest(graphs, 'iter_nodes')
def train_forest(self, sample_indices, training_context, training_parameters): forest = Forest() for i in xrange(training_parameters.numOfTrees): # TODO: perform bagging on the samples tree = ArrayTree(training_parameters.maximumDepth) self.train_tree(tree, sample_indices, training_context, training_parameters) forest.append(tree) return forest
def __init__(self): self.mr = MountainRange(MOUNTAIN, 330, 330, 60, 0.25) self.bgd = MountainRange(BACKGROUND_TREES, 310, 310, 10, 0.5) self.bg_trees = Forest(BACKGROUND_TREES, 310, 10, 40, 40, 50, 0.5) self.lake = MountainRange(LAKE, 290, 290, 0, 0) self.mg = MountainRange(MIDGROUND_TREES, 60, 60, 10, 1.5) self.mg_trees = Forest(MIDGROUND_TREES, 60, 5, 40, 55, 75, 1.5) self.fg = MountainRange(FOREGROUND_TREES, 0, 0, 10, 2.5) self.fg_trees = Forest(FOREGROUND_TREES, 10, 30, 90, 90, 170, 2.5) self.side_checkpoint_marker = SideCheckPointMarker(0) #these could be done after construction of the Stage object self.bg_trees.load_tree_image('./resources/trees/small_trees_bg.png') self.mg_trees.load_tree_image('./resources/trees/med_trees_mg.png') self.fg_trees.load_tree_image('./resources/trees/large_trees_fg.png') #lane buoys could be processed by this class too self.bg = pygame.Surface((32, 32)) self.bg.convert() self.bg.fill(pygame.Color("#FFE5C1")) self.bg.fill(pygame.Color("#dff1ff"))
def kruskal(G): forest = Forest(map(lambda x: Graph([x]),G.V)) edges = sorted(G.E) for e in edges: if len(forest) == 1: break t1 = forest.find_tree(e.origin) t2 = forest.find_tree(e.destination) if t1 == t2: continue forest.merge_trees(t1, t2) t1.add_edge(e) return forest[0]
def network_show(self): data = {} for name, netdata in self.networks_data().items(): if self.options.name and name != self.options.name: continue data[name] = netdata if self.options.format in ("json", "flat_json"): return data if not data: return from forest import Forest from rcColor import color tree = Forest() tree.load(data, title="networks") print(tree)
def fit(self, trajectories, targets, n_estimators, max_radius, min_trajectories, sample_share=0.66, processes=1): """ Fit random forest :param trajectories: list of trajectories :param targets: list of targets :param n_estimators: the size of the forest :param max_radius: maximum radius in searching for decision point in trees :param min_trajectories: minimum number of trajectories to split further in trees :param sample_share: share of sample size :return: the builded forest """ forest = Forest() if processes == 1: #serial option trees = [ self.tree_fit(trajectories, targets, max_radius, min_trajectories, sample_share) for i in range(n_estimators) ] else: #several processess option pool = mp.Pool(processes=processes) results = [ pool.apply_async(self.tree_fit, args=(trajectories, targets, max_radius, min_trajectories, sample_share)) for i in range(n_estimators) ] trees = [p.get() for p in results] #add trees to the forest for tree in trees: forest.add(tree) return forest
def cross_valid_values(list_of_specimen, num_chunks, n_estimators, max_features_select): """ A function to perform cross validation of the model :param list_of_specimen: a list of training examples :param num_chunks: a K number in K-fold cross validation :param n_estimators: a number of trees in RF :param max_features_select: a number of features per tree :return: an mean accuracy of the model :rtype: int """ if (num_chunks <= 1): print("number of chunks has to be greater than 1") return -1 scores = np.array([]) loss = [0 for i in range(num_chunks)] list_of_specimen = list(np.random.permutation(list_of_specimen)) for i in range(num_chunks): begin, end = int(i * len(list_of_specimen) / num_chunks), int( (i + 1) * len(list_of_specimen) / num_chunks) list_of_testing_specimen = list_of_specimen[begin:end].copy() testing_predictions = list_of_testing_specimen list_of_training_specimen = list_of_specimen.copy( )[:begin] + list_of_specimen.copy()[end:] classifier = Forest(n_estimators, list_of_training_specimen, max_feat_select=max_features_select) testing_predictions = classifier.predict(testing_predictions) acc = accuracy(list_of_testing_specimen, testing_predictions) loss[i] = 1 - acc scores = np.append(scores, acc) avg_acc = sum(scores) / len(scores) return avg_acc
def print_checks(self, data): from forest import Forest from rcColor import color tree = Forest() head_node = tree.add_node() head_node.add_column(rcEnv.nodename, color.BOLD) for chk_type, instances in data.items(): node = head_node.add_node() node.add_column(chk_type, color.BROWN) for instance in instances: _node = node.add_node() _node.add_column(str(instance["instance"]), color.LIGHTBLUE) _node.add_column(instance["path"]) _node.add_column(str(instance["value"])) if instance["driver"] == "generic": _node.add_column() else: _node.add_column(instance["driver"]) tree.out()
def print_tree(self, devices=None, verbose=False): ftree = Forest() node = ftree.add_node() node.add_column(rcEnv.nodename, color.BOLD) node.add_column("Type", color.BOLD) node.add_column("Size", color.BOLD, align="right") node.add_column("Pct of Parent", color.BOLD, align="right") filtered = devices is not None and len(devices) > 0 if filtered: devs = [self.get_dev_by_devpath(devpath) for devpath in devices] else: devs = [self.dev[r.child] for r in self.root] for dev in devs: if dev is None or (not filtered and dev.parents != []): continue dev.print_dev(node=node, highlight=devices, verbose=verbose) ftree.out()
def network_status(self): data = self.network_status_data(self.options.name) if self.options.format in ("json", "flat_json"): return data from forest import Forest from rcColor import color tree = Forest() head = tree.add_node() head.add_column("name", color.BOLD) head.add_column("type", color.BOLD) head.add_column("network", color.BOLD) head.add_column("size", color.BOLD) head.add_column("used", color.BOLD) head.add_column("free", color.BOLD) head.add_column("pct", color.BOLD) for name in sorted(data): ndata = data[name] net_node = head.add_node() net_node.add_column(name, color.BROWN) net_node.add_column(data[name]["type"]) net_node.add_column(data[name]["network"]) net_node.add_column("%d" % data[name]["size"]) net_node.add_column("%d" % data[name]["used"]) net_node.add_column("%d" % data[name]["free"]) net_node.add_column("%.2f%%" % data[name]["pct"]) if not self.options.verbose: continue ips_node = net_node.add_node() ips_node.add_column("ip", color.BOLD) ips_node.add_column("node", color.BOLD) ips_node.add_column("service", color.BOLD) ips_node.add_column("resource", color.BOLD) for ip in sorted(ndata.get("ips", []), key=lambda x: (x["ip"], x["node"], x["path"], x["rid"])): ip_node = ips_node.add_node() ip_node.add_column(ip["ip"]) ip_node.add_column(ip["node"]) ip_node.add_column(ip["path"]) ip_node.add_column(ip["rid"]) print(tree)
def print_tree_bottom_up(self, devices=None, verbose=False): ftree = Forest() node = ftree.add_node() node.add_column(rcEnv.nodename, color.BOLD) node.add_column("Type", color.BOLD) node.add_column("Parent Use", color.BOLD, align="right") node.add_column("Size", color.BOLD, align="right") node.add_column("Ratio", color.BOLD, align="right") if devices is None: devices = set() else: devices = set(devices) for dev in self.get_bottom_devs(): if len(devices) > 0 and len(set(dev.devpath) & devices) == 0: continue dev.print_dev_bottom_up(node=node, highlight=devices, verbose=verbose) ftree.out()
def __init__(self, n, stocha, obs): self.gold_mines = [] self.forests = [] self.obstacles = [] self.board = [] self.quotas = [False for k in range(NB_RESOURCES)] self.n= n self.time = 0 self.reward = 0 self.stocha = stocha # Board instantiation for i in range(n): for j in range(n): if (i, j) in OBSTACLES and obs: obstacle = Obstacle([i, j]) self.board.append(obstacle) self.obstacles.append(obstacle) elif (i,j) in GOLD_MINES: gold_mine = GoldMine([i,j]) self.board.append(gold_mine) self.gold_mines.append(gold_mine) elif (i,j) in FORESTS: forest = Forest([i,j]) self.board.append(forest) self.forests.append(forest) elif (i,j) == PLAYER: self.player = Player([i,j], NOTHING) self.board.append(FreeTile([i,j])) elif (i,j) == CHEST: self.chest = Chest([i,j]) self.chest_next = True self.board.append(self.chest) else: self.board.append(FreeTile([i,j])) self.gold_mines_next = [False for k in self.gold_mines] self.forests_next = [False for k in self.forests]
def test4(): f = Forest([5,2,3,7], widen=2, margin=10 ) # f.rotate() # f.draw(bg=".") pass
def test3(): f = Forest([5,2,3,7], widen=2, margin=10 ) # f.draw() pass
def test2(): f = Forest([5,2,3,7] ) # a forest of tree with layers = 5,2,3, and 7 as shown below f.draw() pass
def __init__(self, forest, treat50=False, record_stats=False): self.forest = forest self.treat50 = treat50 self.record_stats = record_stats # def create_csv_statfile(self): def record_data(self, runs, years): timestamp = time.strftime("%Y-%m-%d-%H%M") name = "BlightStats-" + timestamp with open(name, mode='w') as csv_file: fieldnames = ['run', 'year', 'num_healthy', 'num_viru', 'num_hypo'] writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() for run in range(0, runs): self.forest.set_random_grid() for year in range(0, years): healthy = self.forest.num_healthy viru = self.forest.num_viru hypo = self.forest.num_hypo writer.writerow({'run': run, 'year': year, 'num_healthy': \ healthy, 'num_viru': viru, 'num_hypo': hypo }) self.forest.set_next_year() # TODO: command line args forest = Forest(50, 50) recorder = DataRecorder(forest, False, True) recorder.record_data(10, 10)
# define circle radius based on tree stage mrad = tree.stage * 0.24 * rad xe = x + ((cell_w - mrad) / 2) ye = y + ((cell_h - mrad) / 2) scene.addEllipse(xe, ye, mrad, mrad, QPen(trans), QBrush(qcol)) def colorFromTree(self, tree): col = "transparent" if tree != None: col_switch = { config.V: "red", config.HV: "turquoise", config.HEALTHY: "green" } col = col_switch.get(tree.rating) return QColor(col) if __name__ == '__main__': import sys app = QApplication(sys.argv) test_forest = Forest(50, 50) test_forest.set_random_grid() fv = ForestViewer(test_forest) fv.show() # Use this when debugging w/ IPython in Spyder IDE app.aboutToQuit.connect(app.deleteLater) sys.exit(app.exec_())
print(f"Bayesian Average Error: {np.round(model5._error.mean()[0] * 100, 2)}%") print("---- PLS ----") model4 = PLS(**config) model4.roll(verbose=True) print(f"PLS Average Error: {np.round(model4._error.mean()[0] * 100, 2)}%") # produce a neural network rolling forecast print("---- Neural Network ----") model3 = MLP(**config) model3.roll(verbose=True) print(f"NNet Average Error: {np.round(model3._error.mean()[0] * 100, 2)}%") # produce a random forest rolling forecast print("---- Random Forest ----") model2 = Forest(**config) model2.roll(verbose=True) print(f"Forest Average Error: {np.round(model2._error.mean()[0] * 100, 2)}%") # produce a lasso regression rolling forecast print("---- Lasso Regression ----") model1 = Regression(**config) model1.roll(verbose=True) print(f"Lasso Average Error: {np.round(model1._error.mean()[0] * 100, 2)}%") # produce a baseline rolling forecast (exponential smoothing) print("---- Exponential Smoothing ----") baseline_model = Forecasting(**config) baseline_model.roll(verbose=True) print(f"Baseline Average Error: {np.round(baseline_model._error.mean()[0] * 100, 2)}%")
# You may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. See the License for the specific language governing # permissions and limitations under the License. from collections import Counter from forest import Forest import dtree data = dtree.read_csv('..\ch04\census.csv') continuousColumns = ['Age'] data = dtree.prepare_data(data, continuousColumns) outcomeLabel = 'Born' forest = Forest(data, outcomeLabel, continuousColumns) testData = ['Elizabeth', 'female', 'Married', 16, 'Daughter'] predicted = forest.get_prediction(testData) print("predicted: {}".format(predicted)) forest = Forest(data, outcomeLabel, continuousColumns) predictions = [] for _ in range(0, 100): predictions.append(forest.get_prediction(testData)) forest.populate() counts = Counter(predictions) print("predictions: {}".format(counts.most_common()))