def test_forest_function(): """ Forest function """ data = { "data": [ { "text": "node1", "color": color.BOLD }, ], "children": [{ "data": [[ { "text": "node11" }, { "text": None }, {}, ], { "text": "down", "color": color.RED }, {}, { "text": "10 MB", "align": "right" }], "children": [{ "data": { "text": "node111", } }] }, { "data": [{ "text": "node12 blah blah blah" }, { "text": "up", "color": color.GREEN }, {}, { "text": "10 MB", "align": "right" }] }] } widths = [ (6, 10), # min 0, max 10 chars None, # no constraints, auto detect None, # no constraints, auto detect 10 # exactly 10 chars ] buff = forest(data, columns=4, widths=widths, force_width=20) assert "blah" in buff with pytest.raises(IndexError): forest(data, columns=3, widths=[1])
def __init__(self, parent=None): self.forest = forest.forest() self.stage = 0 super(Forest, self).__init__(parent) self.setWindowTitle(self.tr("电力负荷预测系统")) mainSplitter = QSplitter(Qt.Horizontal) mainSplitter.setOpaqueResize(True) self.listWidget = QListWidget(mainSplitter) self.listWidget.insertItem(0, self.tr("预测流程")) self.listWidget.insertItem(1, self.tr("选择预测日期")) self.listWidget.insertItem(2, self.tr("计算相似度")) self.listWidget.insertItem(3, self.tr("智能预测")) frame = QFrame(mainSplitter) self.stack = QStackedWidget() self.stack.setFrameStyle(QFrame.Panel | QFrame.Raised) setProcess = SetProcess() setProcess.setPredict(self.forest) setDate = SetDate() setDate.setDate(self.forest) self.countSimilarity = CountSimilarity() self.countSimilarity.setData(self.forest) doPredict = DoPredict() doPredict.setData(self.forest) self.stack.addWidget(setProcess) self.stack.addWidget(setDate) self.stack.addWidget(self.countSimilarity) self.stack.addWidget(doPredict) self.amendPushButton = QPushButton(self.tr("确定")) self.amendPushButton.clicked.connect(self.amendButtonEvent) self.nextPushButton = QPushButton(self.tr("下一步")) self.nextPushButton.clicked.connect(self.nextStageEvent) buttonLayout = QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(self.amendPushButton) buttonLayout.addWidget(self.nextPushButton) mainLayout = QVBoxLayout(frame) mainLayout.setMargin(10) mainLayout.setSpacing(6) mainLayout.addWidget(self.stack) mainLayout.addLayout(buttonLayout) self.connect(self.listWidget, SIGNAL( "currentRowChanged(int)"), self.stack, SLOT("setCurrentIndex(int)")) self.connect(self.listWidget, SIGNAL( "currentRowChanged(int)"), self, SLOT("buttonStatus()")) layout = QHBoxLayout(self) layout.addWidget(mainSplitter) self.setLayout(layout)
def train(self, TrainXmatrix, TrainY): self.trainXMatrix = TrainXmatrix self.trainYLabels = TrainY uniqueYLables = set(TrainY) numYLabels = len(uniqueYLables) weightsForForests = col.defaultdict(int) numTrainObs = self.trainXMatrix.shape[0] obsWeights = np.array([1 / numTrainObs for i in range(numTrainObs)]) ##initialise ##create forests and train them trainedForests = [] ##multiple forests self.verbosePrint("\nNumber of forest building : ", self.nTrees) for i in range( self.nTrees): ##do not confuse with trees, they are forest ForestObj = forest(**self.kwargs) ForestObj.trainForest(TrainXmatrix, TrainY) trainedForests.append(ForestObj) #entire foreest self.verbosePrint("\nNumber of forests built : ", i + 1) self.hypothesis = trainedForests ##assempble of forests self.verbosePrint("\nCalculating weights ....") for forestNum, ForestObj in enumerate( self.hypothesis): ##feature pair as tuple trainPredictionList = ForestObj.predict(TrainXmatrix) forestError = self._calcError(obsWeights, trainPredictionList, self.trainYLabels) if forestError > 1 - ( 1 / numYLabels): #not better than random guessing continue # numSatisifyingStumps+=1 forestWeight = math.log( (1 - forestError) / forestError) + math.log(numYLabels - 1) obsWeights = self._adjustNormWeights(obsWeights, forestWeight, trainPredictionList, self.trainYLabels) weightsForForests[forestNum] = forestWeight self._weightsForHypothesis = weightsForForests
def __init__(self, data, labels, num_trees=10, num_iter=10): self.alphas = [] self.trees = [] weights = array(ones(len(data))) / float(len(data)) for i in range(num_iter): print 'iteration', i tree = forest.forest(data, labels, num_trees, weights) self.trees.append(tree) err = error(tree, data, labels) alpha = .5 * math.log((1 - err) / err) self.alphas.append(alpha) #check if each point was classified correctly, and update point's weight for d in range(len(data)): if tree.classify(data[d,:]) == labels[d][0]: weights[d] *= math.exp(-alpha) else: weights[d] *= math.exp(alpha) weights /= sum(weights)
def doPredict(): f = forest.forest() f.setData('2007-1-21', 345) f.normalize() f.countSimilarity() f.predict() predict_plan = {"description": "test", "datetime": datetime.today().strftime("%Y-%m-%d %H:%M:%S"), "predict_rate": f.predict_rate, "minRel": f.minRel, "predictNum": f.predictNum} predict_plan_id = db.insertGetId("predict_plan", **predict_plan) predict_plan["plan_id"] = predict_plan_id for comparation in f.forestSimilarity: base = comparation.base base_info = {"pc_id": base.pc_id, "ah_id": base.ah_id, "predict_pc": base.powerConsume['predict'], "date": base.date} comparation_id = db.insertGetId("comparation", **base_info) for similarity in comparation.similarityEntitys: ap = similarity.ap ap_info = {"comparation_id": comparation_id, "pc_id": ap.pc_id, "ah_id": ap.ah_id, "similarity": similarity.similarity, "date": ap.date} db.insertGetId("similarity", **ap_info) db.commit()
#!/usr/bin/python3 """ jeu de la foret pour la méthode de résolution de l'article de j. Ulehla. """ from winningStratUle import iaUlehla from forest import forest print("hackendot") #on crée une nouvelle foret f = forest() #on s'assure que f est bien une foret try : assert type(f) == forest except AssertionError : print("erreur création arbre") quit() #on crée une nouvelle ia pour le calcul du coup gagnant si il existe ia = iaUlehla(f) #on s'assure que ia est bien une ia try : assert type(ia) == iaUlehla except AssertionError : print("erreur création ia") quit() joueur = 1
def forestReset(self): self.forest = forest.forest(self.numToLight, self.burnChance) self.genNumber = 0 self.timePassed = 0 self.paused = True self.drawGraph()
def __init__(self): self.numToLight = input("Enter the number of trees to start on fire!") self.numToLight = max(0, min(int(self.numToLight), 40000)) self.burnChance = input("Enter a % chance for trees to burn! (Between 0% and 100%)") self.burnChance = max(0, min(float(self.burnChance), 100)) / 100 self.forest = forest.forest(self.numToLight, self.burnChance) self.window = pygame.display.set_mode((1000,830)) self.done = False # Background and images self.bg = pygame.image.load("GUI.png") self.buttonImage = pygame.image.load("button.png") self.buttonMDImage = pygame.image.load("buttonDown.png") self.buttonMOImage = pygame.image.load("buttonMouseOver.png") self.plateImage = pygame.image.load("plate.png") self.gridImage = pygame.image.load("grid.png") self.gridImage.set_colorkey((255,255,255)) # Button attributes self.button1pos = (832, 472) self.button2pos = (832, 539) self.button3pos = (832, 605) self.buttonHeight = 51 self.buttonWidth = 152 # Font attributes self.genNumberPos = (848, 190) self.timePassedPos = (848, 285) self.stepPos = (870, 550) self.resetPos = (865, 618) self.pausedPos = (860, 485) self.playPos = (873, 485) # Grid attributes self.gridStart = (17, 16) self.gridSize = (800, 800) self.cellWidth = self.gridSize[0] / (self.forest.forestSize - 2) self.cellHeight = self.gridSize[1] / (self.forest.forestSize - 2) self.colors = [(100, 100, 100), (255, 255, 255), (0, 200, 0), (200, 0, 0)] # Extras variables self.white = (255,255,255) self.black = (0,0,0) self.genNumber = 0 self.timePassed = 0 self.paused = True self.flag = False self.takestep = False self.timestep = 1000 self.timeSinceLastUpdate = 0 # Font initialization self.font = pygame.font.Font(None, 44) backFont = pygame.font.Font(None, 45) self.genNumberFont = self.font.render(str(self.genNumber), True, self.white) self.timePassedFont = self.font.render(str(self.timePassed), True, self.white) self.pauseButtonFont = self.font.render("Pause", True, self.white) self.playButtonFont = self.font.render("Play", True, self.white) self.stepButtonFont = self.font.render("Step", True, self.white) self.resetButtonFont = self.font.render("Reset", True, self.white) self.pauseButtonFont2 = backFont.render("Pause", True, self.black) self.playButtonFont2 = backFont.render("Play", True, self.black) self.stepButtonFont2 = backFont.render("Step", True, self.black) self.resetButtonFont2 = backFont.render("Reset", True, self.black) self.drawGraph()
#!/usr/bin/python3 """ jeu de la foret pour la méthode de résolution de l'article de j. Ulehla. """ from winningStratUle import iaUlehla from forest import forest print("hackendot") #on crée une nouvelle foret f = forest() #on s'assure que f est bien une foret try: assert type(f) == forest except AssertionError: print("erreur création arbre") quit() #on crée une nouvelle ia pour le calcul du coup gagnant si il existe ia = iaUlehla(f) #on s'assure que ia est bien une ia try: assert type(ia) == iaUlehla except AssertionError: print("erreur création ia") quit() joueur = 1 #tant que f n'est pas vide, le jeu n'est pas fini
weights[d] *= math.exp(-alpha) else: weights[d] *= math.exp(alpha) weights /= sum(weights) def classify(self, observation): final_hyp = 0 for i in range(len(self.trees)): tree = self.trees[i] alpha = self.alphas[i] hyp = tree.classify(observation) if hyp == 0: hyp = -1 final_hyp += alpha * hyp return int(final_hyp > 0) if __name__ == '__main__': all_data = sio.loadmat('spam.mat') data = all_data['Xtrain'] labels = all_data['ytrain'] ada_boost = ada_boost_forest(data[:-250,:], labels[:-250]) f = forest.forest(data[:-250,:], labels[:-250], 10) print 'ada', error(ada_boost, data[-250:,:], labels[-250:]) print 'stand forest', error(f, data[-250:,:], labels[-250:])
import pickle as pk if len(sys.argv) < 5: print("Usage: \n./orient.py train train_file.txt model_file.txt [model]") sys.exit() #train train_file.txt model_file.txt [model] #nearest, adaboost, forest, or best. (trainOrTest, train_test_file, model_file, model) = sys.argv[1:5] #train train-data.txt forest_model.txt forest #test test-data.txt forest_model.txt forest if model == 'forest' : if trainOrTest == 'train': myForest = forest(maxDepth=5,numTrees= 20,verbose = False) TrainX,TrainY,TrainXID= myForest.getDataFromFile(train_test_file) myForest.trainForest(TrainX,TrainY) pk.dump(myForest,open(model_file,'wb')) if trainOrTest == 'test': try: myForest = pk.load(open(model_file,'rb')) except: print("output file has not been generated") if myForest.isTrained: Xtest,yTest,XtestID = myForest.getDataFromFile(train_test_file) finalPredictions = myForest.predict(Xtest) myForest.writeToFile(XtestID,finalPredictions,'output.txt') print("Accuracy is: " ,sum(finalPredictions==yTest)/len(yTest))
def __init__(self): self.numToLight = input("Enter the number of trees to start on fire!") self.numToLight = max(0, min(int(self.numToLight), 40000)) self.burnChance = input( "Enter a % chance for trees to burn! (Between 0% and 100%)") self.burnChance = max(0, min(float(self.burnChance), 100)) / 100 self.forest = forest.forest(self.numToLight, self.burnChance) self.window = pygame.display.set_mode((1000, 830)) self.done = False # Background and images self.bg = pygame.image.load("GUI.png") self.buttonImage = pygame.image.load("button.png") self.buttonMDImage = pygame.image.load("buttonDown.png") self.buttonMOImage = pygame.image.load("buttonMouseOver.png") self.plateImage = pygame.image.load("plate.png") self.gridImage = pygame.image.load("grid.png") self.gridImage.set_colorkey((255, 255, 255)) # Button attributes self.button1pos = (832, 472) self.button2pos = (832, 539) self.button3pos = (832, 605) self.buttonHeight = 51 self.buttonWidth = 152 # Font attributes self.genNumberPos = (848, 190) self.timePassedPos = (848, 285) self.stepPos = (870, 550) self.resetPos = (865, 618) self.pausedPos = (860, 485) self.playPos = (873, 485) # Grid attributes self.gridStart = (17, 16) self.gridSize = (800, 800) self.cellWidth = self.gridSize[0] / (self.forest.forestSize - 2) self.cellHeight = self.gridSize[1] / (self.forest.forestSize - 2) self.colors = [(100, 100, 100), (255, 255, 255), (0, 200, 0), (200, 0, 0)] # Extras variables self.white = (255, 255, 255) self.black = (0, 0, 0) self.genNumber = 0 self.timePassed = 0 self.paused = True self.flag = False self.takestep = False self.timestep = 1000 self.timeSinceLastUpdate = 0 # Font initialization self.font = pygame.font.Font(None, 44) backFont = pygame.font.Font(None, 45) self.genNumberFont = self.font.render(str(self.genNumber), True, self.white) self.timePassedFont = self.font.render(str(self.timePassed), True, self.white) self.pauseButtonFont = self.font.render("Pause", True, self.white) self.playButtonFont = self.font.render("Play", True, self.white) self.stepButtonFont = self.font.render("Step", True, self.white) self.resetButtonFont = self.font.render("Reset", True, self.white) self.pauseButtonFont2 = backFont.render("Pause", True, self.black) self.playButtonFont2 = backFont.render("Play", True, self.black) self.stepButtonFont2 = backFont.render("Step", True, self.black) self.resetButtonFont2 = backFont.render("Reset", True, self.black) self.drawGraph()
if __name__ == '__main__': def sampleit(train, train_Y, k): whole_train = np.column_stack((train, train_Y)) r = random.sample([i for i in range(train.shape[0])], k) whole_sample = whole_train[r, :] train_sample = whole_sample[:, 0:train.shape[1]] train_y_sample = whole_sample[:, train.shape[1]] return train_sample, train_y_sample ####num trees numTreeAccu = [] numTrees = list(range(5, 35, 5)) for numtree in numTrees: myForest = forest(maxDepth=5, numTrees=numtree, verbose=True) TrainX, TrainY, TrainXID = myForest.getDataFromFile('train-data.txt') myForest.trainForest(TrainX, TrainY) Xtest, yTest = myForest.getDataFromFile('test-data.txt') finalPredictions = myForest.predict(Xtest) numTreeAccu.append(sum(finalPredictions == yTest) / len(yTest)) # myForest._getDecisionFromTree(rootNode,X[60,:]) # myForest.exploreTree(rootNode) plt.plot(numTrees, numTreeAccu) plt.xlabel("Number of Trees") plt.ylabel("Accuracy") plt.title("Accuracy vs No of Trees") ##max depth maxDepthsAcc = []
def __init__(self): self.forest = forest.forest(from_pikl=True) pass
from sklearn.metrics import f1_score import pandas as pd import forest airlineData = pd.read_csv("AirlineReduced") ##exclude not required columns airlineData = airlineData.loc[:, ~airlineData.columns.isin([ 'DepTime', 'ArrTime', 'CRSArrTime', 'CRSDepTime', 'ActualElapsedTime', 'ArrTimeInMins', 'ArrDelay' ])] print(airlineData.head()) X_train, X_test, y_train, y_test = train_test_split( airlineData.loc[:, ~airlineData.columns.isin(['IsDelayed'])], airlineData['IsDelayed'], test_size=0.25, random_state=42) X_train, X_test, y_train, y_test = np.array(X_train), np.array( X_test), np.array(y_train), np.array(y_test) myForest = forest.forest(maxDepth=5, numTrees=10, verbose=True) start = time.time() myForest.trainForest(X_train, y_train) finalPredictions = myForest.predict(X_test) print("Time elapsed is ", time.time() - start) print("Accuracy is: ", f1_score(y_test, finalPredictions))
# -*- coding: utf-8 -*- """ Created on Mon Apr 04 15:48:30 2016 @author: Coco """ import forest import db from datetime import datetime f = forest.forest() f.setData('2007-1-21', 345) f.normalize() f.countSimilarity() f.predict() predict_plan = {"description": "test", i"datetime": datetime.today().strftime("%Y-%m-%d %H:%M:%S"), "predict_rate": f.predict_rate, "minRel": f.minRel, "predictNum": f.predictNum} predict_plan_id = db.insertGetId("predict_plan", **predict_plan) predict_plan["plan_id"] = predict_plan_id for comparation in f.forestSimilarity: base = comparation.base base_info = {"pc_id": base.pc_id, "ah_id": base.ah_id, "predict_pc": base.powerConsume['predict'], "date": base.date} comparation_id = db.insertGetId("comparation", **base_info) for similarity in comparation.similarityEntitys: ap = similarity.ap ap_info = {"comparation_id": comparation_id, "pc_id": ap.pc_id, "ah_id": ap.ah_id, "similarity": similarity.similarity, "date": ap.date}
n_image = len(file_names) for i, file_name in enumerate(file_names): img = img_as_float(imread(os.path.join(image_dir, file_name))) edge = np.array(model.run(img)) edge /= np.max(abs(edge)) edge = img_as_ubyte(edge) # print edge if not os.path.exists(os.path.join(output_dir, file_name[:-3] + "png")): imsave(os.path.join(output_dir, file_name[:-3] + "png"), edge) if __name__ == "__main__": forest = forest.forest() if not os.path.exists("./model/forests/forest.h5"): print "Weights not present, training using mini-dataset" train_data = get_train_data() forest.train(train_data) if not os.path.exists(os.path.dirname("./model/forests/")): os.makedirs(os.path.dirname("./model/forests/")) print "Saving Trained Model" with open("./model/forests/forest.h5", "wb") as f: pickle.dump(forest, f, pickle.HIGHEST_PROTOCOL) else: try: with open("./model/forests/forest.h5", "rb") as f: forest = pickle.load(f) except: