def _nextToPac(self, gameState): if gameState.getAgentState(self.index).scaredTimer > 0: return False Pos = gameState.getAgentState(self.index).getPosition() enemyPacmanPossiblePositions = {} #Find closest enemy and best position to intercept him for agent in self.getOpponents(gameState): # Add opponents to list of enemies enemy = gameState.getAgentState(agent) if (enemy.isPacman and enemy.getPosition() != None): enemyPacmanPossiblePositions[agent] = map( lambda a: gameState.generateSuccessor(agent, a), gameState.getLegalActions(agent)) PacmanFollowing = -1 distanceToEnemyPacman = 999 goTo = None for id in enemyPacmanPossiblePositions: #print id for enemyP in enemyPacmanPossiblePositions[id]: if self.getMazeDistance( Pos, enemyP.getAgentPosition(id)) < distanceToEnemyPacman: #pacmanFollowing = id #distanceToEnemyPacman = self.getMazeDistance(Pos, enemyP.getAgentPosition(id)) distanceToEnemyPacman = util.manhattanDistance( Pos, enemyP.getAgentPosition(id)) goTo = enemyP.getAgentPosition(id) if distanceToEnemyPacman < 1: #jonky util.pause() return True return False
def run_cost_analysis(alphas, cost_hist, dataset_title): """Run Cost analysis based on learnt values of theta.""" min_cost = np.zeros(shape=(1, np.size(alphas))) if cost_hist is not None: fig = None subplot = None colors = get_colors(np.shape(alphas)[1]) for index in range(0, np.shape(alphas)[1]): min_cost[0, index] = np.min(cost_hist[:, index]) fig, subplot = \ line_plot(np.reshape(range(1, np.shape(cost_hist)[0] + 1), newshape=(np.shape(cost_hist)[0], 1)), cost_hist[:, index], xlabel='Number Of Iterations', ylabel='Cost J', marker='x', markersize=2, title=f'{dataset_title}\nConvergence Graph', color=colors[index], label=f'alpha={alphas[0, index]}', linewidth=1, fig=fig, subplot=subplot) util.pause('Program paused. Press enter to continue.') close_plot(fig) fig, subplot = \ line_plot(alphas.transpose(), min_cost.transpose(), xlabel='alpha', ylabel='cost', marker='x', markersize=2, color='r', title=f'{dataset_title}\n Alphas Vs Cost', linewidth=2) util.pause('Program paused. Press enter to continue.') close_plot(fig)
def nextToPac(self, gameState): enemies = [] for agent in self.getOpponents(gameState): enemies.append(gameState.getAgentState(agent)) pacStates = [] for enemy in enemies: if enemy.isPacman and enemy.getPosition() != None: pacStates.append(enemy) pacPositions = map(lambda g: g.getPosition(), pacStates) if (len(pacPositions) > 0): distanceToClosestPac = min( map( lambda x: self.getMazeDistance( gameState.getAgentState(self.index).getPosition(), x), pacPositions)) else: distanceToClosestPac = 100 if distanceToClosestPac < 1: print "dist=0" util.pause() return True elif distanceToClosestPac < 6: ghostScore = (1. / distanceToClosestPac) else: ghostScore = 0 return False
def run_dataset(dataset_name, dataset_title, dataset_type='txt', dataset_xlabel='X', dataset_ylabel='Y', normalize=False, print_data=False, label=None, predict_func=None, add_features=False, degree=None, regularization_param=0, plot_image=False, image_size=None, num_images_per_row=None, num_images_per_col=None): """Run Logistic Regression For single class classification problem.""" _, feature_matrix, output_colvec, \ num_examples, num_features, mu_rowvec, sigma_rowvec = \ load_data(dataset_name, dataset_type, normalize, print_data) fig, subplot = \ plot_dataset(feature_matrix, output_colvec, dataset_title, dataset_xlabel, dataset_ylabel, label, plot_image, image_size, num_images_per_row, num_images_per_col) if add_features and np.shape(feature_matrix)[1] >= 2: if not degree: degree = 6 print('Improve Training Accuracy By Adding New Features...') feature_matrix = util.add_features(feature_matrix[:, 1], feature_matrix[:, 2], degree) num_examples, num_features = np.shape(feature_matrix) num_features -= 1 print('num_features={num_features}, num_examples={num_examples}') theta_colvec, alpha, cost, \ _, alphas, cost_hist = \ run_logistic_regression(feature_matrix, output_colvec, num_examples, num_features, num_iters=1500, fig=fig, subplot=subplot, theta_colvec=None, debug=True, degree=degree, regularization_param=regularization_param) if predict_func: predict_func(theta_colvec, num_features, mu_rowvec, sigma_rowvec) accuracy = training_accuracy(feature_matrix, output_colvec, theta_colvec) print(f'Train Accuracy(alpha={alpha}, cost={cost}): {accuracy}') util.pause('Program paused. Press enter to continue.') run_cost_analysis(alphas, cost_hist, dataset_title) close_plot(fig)
def _provider_add_addr(web, prv_name, prv_addr, kms_addr_spec, prv_agt): pnbox = web.find('elmtNm') pnbox.clear() web.sendkeys(pnbox, prv_name) stale = web.check_stale(_PRV_NAME_IN_LIST.format(prv_name), SW.CSS) # maybe target PRV is already listed web.click('searchBtn') # Search for target PRV will obsolete old element if stale: web.wait_detach( stale ) # Make sure to pick a fresh element after the old one got detached try: web.clickp(_PRV_NAME_IN_LIST.format(prv_name), SW.CSS) addrs_exist = [] _walkthrough_kms_addrs( web, lambda re, ka, pa: addrs_exist.append( (ka, pa)) is 'Return False') pinf('add provider addresses for Provider[{}]...', prv_addr) for i, kms_addr in enumerate(Conf.expand_kms_addr(kms_addr_spec), start=1): if (kms_addr, prv_addr) in addrs_exist: perr(' {}: {} exist/skipped', i, kms_addr) continue web.click('protIntfInsert') web.sendkeys(web.find('kmsProtocolId'), kms_addr) web.sendkeys(web.find('provProtocolId'), prv_addr) web.click('app') web.click(f'#app option[value="{prv_agt}"]', SW.CSS) web.click('protIntfSaveBtn') row_add = _walkthrough_kms_addrs( web, lambda re, ka, pa: ka == kms_addr and pa == prv_addr) web.click(row_add) prt(' {}: {}/{} added', i, kms_addr, prv_agt) else: # save to DB if 'row_add' in dir(): web.click('btnPrvdrSave') # save all prt(' committed') # accept modal dialogue web.click('div.modal-content button.bootbox-accept', SW.CSS) return perr(' aborted !') except TimeoutException: perr('provider: {} not found', prv_name) except StaleElementReferenceException as e: pause(2) raise e
def evaluate(inp, client, l, DBlocation): inp = inp.split(' ') status = client.status() if len(inp) > 1 and not str(inp[1]): inp.pop() if inp[0] == 'p' or 'play' == (inp[0]): try: if not status['state'] == 'stop': if len(inp) == 1: util.pause(client) else: util.play(client, int(inp[1])) else: if len(inp) == 1: util.play(client, 0) else: util.play(client, int(inp[1])) except: print('mpd error: bad song index') elif inp[0] == 'pause': util.pause(client) elif inp[0] == 'next' or inp[0] == 'n': util.next(client) elif inp[0] == 'previous' or inp[0] == 'ps': util.previous(client) elif inp[0] == 'stop': util.stop(client) elif inp[0] == 'pl' or inp[0] == 'playlist': util.print_playlist(client) elif inp[0] == 'update' or inp[0] == 'u': util.update(client) elif inp[0] == 'clear': util.clear(client) elif inp[0] == 'random': util.mpdrandom(client, inp[1]) elif inp[0] == 'shuffle': util.shuffle(client) elif inp[0] == 'consume': util.consume(client, inp[1]) elif inp[0] == 'swap': util.swap(client, int(inp[1]) - 1, int(inp[2]) - 1) elif inp[0] == 'single': util.single(client, inp[1]) elif inp[0] == 'search' or inp[0] == 's': if '-f' in inp or '--filter' in inp: l = util.mpdsearch(inp[1], inp, DBlocation, True) else: l = util.mpdsearch(inp[1], inp, DBlocation, False) elif inp[0] == 'a' or inp[0] == 'add': if l: for line in l: client.add(line) else: print('You have to search first!') elif inp[0] == 'q' or inp[0] == 'quit': quit() return l
def status(self): util.printGameText("You are level {}\n".format(self.level)) print(" * Current HP: {} /".format(self.hp), "{}\n".format(self.maxHp)) print(" * Attack Power: {}\n".format(self.attackPower)) print(" * Total XP: {}\n".format(self.experience)) print(" * XP until next level up: {} XP\n".format(self.nextLevelUp - self.experience)) util.pause()
def predict_dataset2(theta_colvec, num_features, mu_rowvec, sigma_rowvec): """Predict profits based on the trained theta vals.""" num_bedrooms = (3 - mu_rowvec[0, 1]) / sigma_rowvec[0, 1] sq_footage = (1650 - mu_rowvec[0, 0]) / sigma_rowvec[0, 0] predict1 = np.reshape([1, num_bedrooms, sq_footage], newshape=(1, num_features + 1)) @ theta_colvec print('For a 1650 sq-ft 3 br house, ' f'we predict an estmated house price of {predict1[0, 0]}') util.pause('Program paused. Press enter to continue.')
def tinyMazeSearch(problem): """ Returns a sequence of moves that solves tinyMaze. For any other maze, the sequence of moves will be incorrect, so only use this for tinyMaze. """ from game import Directions s = Directions.SOUTH w = Directions.WEST util.pause() return [s, s, w, s, w, w, s, w]
def _provider_del_addr(web, prv_name, prv_addr, kms_addr_spec, prv_agt): pnbox = web.find('elmtNm') pnbox.clear() web.sendkeys(pnbox, prv_name) stale = web.check_stale(_PRV_NAME_IN_LIST.format(prv_name), SW.CSS) # maybe target PRV is already listed web.click('searchBtn') # Search for target PRV will obsolete old element if stale: web.wait_detach( stale ) # Make sure to pick a fresh element after the old one got detached try: web.clickp(_PRV_NAME_IN_LIST.format(prv_name), SW.CSS) pinf('delete provider addresses for Provider[{}]...', prv_addr) kaddrs2del = list(Conf.expand_kms_addr(kms_addr_spec)) bDeleteDirty = False def walkthrough_callback(re, ka, pa): nonlocal bDeleteDirty if pa == prv_addr and ka in kaddrs2del: chkbox = re.find_element_by_tag_name('input') web.click(chkbox) # check row to delete prt(' {} : {} checked ', ka, pa) kaddrs2del.remove(ka) bDeleteDirty = True return False _walkthrough_kms_addrs(web, walkthrough_callback) if len(kaddrs2del) > 0: for ka in kaddrs2del: perr(' {} : {} not exist/skipped', ka, prv_addr) # commit if bDeleteDirty: web.click('protIntfDelete') web.click('protIntfDelOK') prt(' deleted') web.click('btnPrvdrSave') prt(' committed') # accept modal dialogue web.click('div.modal-content button.bootbox-accept', SW.CSS) except StaleElementReferenceException as e: pause(2) raise e
def run_gradient_descent(feature_matrix, output_colvec, num_examples, num_features, alpha, num_iters, fig, subplot, theta_colvec=None, normal_eq=False, debug=False): """Run Gradient Descent/Normal Equation. 1) num_examples - number of training samples 2) num_features - number of features 3) feature_matrix - num_examples x (num_features + 1) 4) output_colvec - num_examples x 1 col vector 5) alpha - alpha value for gradient descent 6) num_iters - number of iterations 7) theta_colvec - (num_features + 1) x 1 col vector initial values of theta 8) debug - print debug info """ print('Running Gradient Descent ...') if not theta_colvec: theta_colvec = np.zeros(shape=(num_features + 1, 1)) cost_hist = None if normal_eq: theta_colvec = \ normal_equation(feature_matrix, output_colvec) print(f'Theta found by normal equation : {theta_colvec}') else: theta_colvec, cost_hist = \ gradient_descent(feature_matrix, output_colvec, num_examples, num_features, alpha, num_iters, theta_colvec, debug=debug) print(f'Theta found by gradient descent: {theta_colvec}') if num_features == 1: line_plot(feature_matrix[:, 1], feature_matrix @ theta_colvec, marker='x', label='Linear regression', color='b', markersize=2, fig=fig, subplot=subplot) util.pause('Program paused. Press enter to continue.') return theta_colvec, cost_hist
def chooseAction(self, gameState): """ Picks among the actions with the highest Q(s,a). """ start = time.time() _, action = self.value(gameState, 0, 0) t = time.time() - start if t >= 1.0: util.pause() return action
def predict_dataset1(theta_colvec, num_features, mu_rowvec, sigma_rowvec): """Predict admission probability based on the trained theta vals.""" exam1_score = (45 - mu_rowvec[0, 0]) / sigma_rowvec[0, 0] exam2_score = (85 - mu_rowvec[0, 1]) / sigma_rowvec[0, 1] predict1 = sigmoid( np.reshape([1, exam1_score, exam2_score], newshape=(1, num_features + 1)) @ theta_colvec) print('For a student with scores 45 and 85, we predict an admission ' f'probability of {predict1[0, 0]}') util.pause('Program paused. Press enter to continue.')
def foodHeuristic(state, problem): """ Your heuristic for the FoodSearchProblem goes here. This heuristic must be consistent to ensure correctness. First, try to come up with an admissible heuristic; almost all admissible heuristics will be consistent as well. If using A* ever finds a solution that is worse uniform cost search finds, your heuristic is *not* consistent, and probably not admissible! On the other hand, inadmissible or inconsistent heuristics may find optimal solutions, so be careful. The state is a tuple ( pacmanPosition, foodGrid ) where foodGrid is a Grid (see game.py) of either True or False. You can call foodGrid.asList() to get a list of food coordinates instead. If you want access to info like walls, capsules, etc., you can query the problem. For example, problem.walls gives you a Grid of where the walls are. If you want to *store* information to be reused in other calls to the heuristic, there is a dictionary called problem.heuristicInfo that you can use. For example, if you only want to count the walls once and store that value, try: problem.heuristicInfo['wallCount'] = problem.walls.count() Subsequent calls to this heuristic can access problem.heuristicInfo['wallCount'] """ position, foodGrid = state "*** YOUR CODE HERE ***" debug = 0 sol = 2 strComidas = str(foodGrid) if debug == 1: print position print foodGrid print foodGrid.asList() print strComidas.count("T") if sol == 1: custo = strComidas.count("T") if sol == 2: if foodGrid.asList() == []: custo = 0 else: custo = util.manhattanDistance(position, foodGrid.asList()[-1]) if debug == 1: print custo util.pause() return custo
def updateGridMotion(self, selfcopy, gameState): #print "x: {}".format(len(self.grid)) #print "y: {}".format(len(self.grid[0])) prevgrid = deepcopy(self.grid) dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] walls = gameState.getWalls() #print walls #print "type: {}".format(getattr(walls)) minimum = 9001 for row in self.grid: for i in range(0, len(self.grid[0])): if row[i] > 0 and row[i] < minimum: minimum = row[i] #print minimum for x in range(0, len(self.grid)): #util.pause() for y in range(0, len(self.grid[0])): #print "x: {}, y: {}".format(x, y) #print "ghost: {}, location: [{},{}], value: {}".format(self.id, x, y, self.grid[x][y]) if prevgrid[x][y] > 0: for i in range(0, 4): #if x+dx[i] >= len(self.grid) or x+dx[i] < 0 or y+dy[i] >= len(self.grid[0]) or y+dy[i] < 0: if not walls.data[x + dx[i]][y + dy[i]]: try: #if not self.grid[x+dx[i]][y+dy[i]] > 0.75: self.grid[x + dx[i]][y + dy[i]] = minimum except Exception: print "terrible, terrible problem" print "Agent: {}, location: [{},{}], value: {}".format( self.id, x, y, self.grid[x][y]) print "[x+dx,y+dy]: [{},{}], isWall: {}".format( x + dx[i], y + dy[i], walls.data[x + dx[i]][y + dy[i]]) raise util.pause() else: #print "wall: {}, location: [{},{}], value: {}".format(self.id, x, y, self.grid[x][y]) #selfcopy.debugDraw((x+dx[i], y+dy[i]), [1,0,0],False) continue totalsum = 0 for row in self.grid: totalsum += sum(row) #print "chugalug" #print totalsum for row in self.grid: if sum(row) > 0: row = [float(i) / totalsum for i in row]
def chooseAction(self, gameState): """ Picks among the actions with the highest Q(s,a). """ start = time.time() _, action = self.value(gameState, 0, 0) t = time.time() - start if t >= 1.0: util.pause() # print ('eval time for agent %d: %.4f' % (self.index, t)) return action
def scrape(url): r = requests.get(url) util.pause() page = BeautifulSoup(r.text, 'html.parser') return { 'url': url, 'price': get_price(page), 'title': get_title(page), 'neighborhood': get_neighborhood(page), 'attributes': get_attributes(page), 'postid': get_postid(page), 'postDatetime': get_postDatetime(page) }
def runClassifier(): # Set up variables according to the command line inputs featureFunction = basicFeatureExtractorDigit legalLabels = range(10) # number of labels # Select classifier classifier = perceptron.PerceptronClassifier(legalLabels) # Load data numTraining = 100 rawTrainingData = samples.loadDataFile("digitdata/trainingimages", numTraining, DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) trainingLabels = samples.loadLabelsFile("digitdata/traininglabels", numTraining) rawValidationData = samples.loadDataFile("digitdata/validationimages", TEST_SET_SIZE, DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) validationLabels = samples.loadLabelsFile("digitdata/validationlabels", TEST_SET_SIZE) rawTestData = samples.loadDataFile("digitdata/testimages", TEST_SET_SIZE, DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT) testLabels = samples.loadLabelsFile("digitdata/testlabels", TEST_SET_SIZE) # Extract features trainingData = map(basicFeatureExtractorDigit, rawTrainingData) validationData = map(basicFeatureExtractorDigit, rawValidationData) testData = map(basicFeatureExtractorDigit, rawTestData) # Conduct training and testing print "Training..." classifier.train(trainingData, trainingLabels, validationData, validationLabels) # print "Validating..." # guesses = classifier.classify(validationData) # correct = [guesses[i] == validationLabels[i] for i in range(len(validationLabels))].count(True) # print str(correct), ("correct out of " + str(len(validationLabels)) + " (%.1f%%).") % (100.0 * correct / len(validationLabels)) print "Testing..." guesses = classifier.classify(testData) correct = [guesses[i] == testLabels[i] for i in range(len(testLabels))].count(True) print str(correct), ("correct out of " + str(len(testLabels)) + " (%.1f%%).") % (100.0 * correct / len(testLabels)) util.pause() analysis(classifier, guesses, testLabels, rawTestData)
def plot_dataset(feature_matrix, output_colvec, dataset_title, dataset_xlabel='X', dataset_ylabel='Y', label=None, plot_image=False, img_size=None, num_images_per_row=None, num_images_per_col=None): """Plot data as a scatter diagram.""" fig = None subplot = None print('Plotting Data ...') yvals = np.unique(output_colvec) markers = get_markers(len(yvals)) colors = get_colors(len(yvals)) if plot_image: random_rows = np.random.randint( 0, len(output_colvec), num_images_per_row * num_images_per_col) def get_img(index): return np.reshape(feature_matrix[random_rows[index], 0:-1], newshape=img_size) fig, subplot = image_plot(num_images_per_row, num_images_per_col, get_img) else: for index, yval in enumerate(yvals): fig, subplot = \ scatter_plot(feature_matrix[(output_colvec == yval). nonzero(), 1], feature_matrix[ (output_colvec == yval).nonzero(), 2], xlabel=dataset_xlabel, ylabel=dataset_ylabel, title=dataset_title, marker=markers[index], color=colors[index], label=label[index], linewidths=None, fig=fig, subplot=subplot) util.pause('Program paused. Press enter to continue.\n') return fig, subplot
def analysis(classifier, guesses, testLabels, testData, rawTestData, printImage): """ This function is called after learning. Include any code that you want here to help you analyze your results. Use the printImage(<list of pixels>) function to visualize features. An example of use has been given to you. - classifier is the trained classifier - guesses is the list of labels predicted by your classifier on the test set - testLabels is the list of true labels - testData is the list of training datapoints (as util.Counter of features) - rawTestData is the list of training datapoints (as samples.Datum) - printImage is a method to visualize the features (see its use in the odds ratio part in runClassifier method) This code won't be evaluated. It is for your own optional use (and you can modify the signature if you want). """ # Put any code here... # Example of use: for i in range(len(guesses)): prediction = guesses[i] truth = testLabels[i] if (prediction != truth): print "===================================" print "Mistake on example %d" % i print "Predicted %d; truth is %d" % (prediction, truth) print "Image: " print rawTestData[i] print 'no circles: ', enhancedFeatureExtractorDigit( rawTestData[i])['no_circles-0'] print 'one circle: ', enhancedFeatureExtractorDigit( rawTestData[i])['one_circle-0'] print 'two circles: ', enhancedFeatureExtractorDigit( rawTestData[i])['two_circles-0'] print 'clusters: ', enhancedFeatureExtractorDigit( rawTestData[i])['clusters_1-0'] #break util.pause()
def chooseAction(self, gameState): """ Picks among the actions with the highest Q(s,a). """ # # You can profile your evaluation time by uncommenting these lines # values = [self.evaluate(gameState, a) for a in actions] # maxValue = max(values) # bestActions = [a for a, v in zip(actions, values) if v == maxValue] start = time.time() _, action = self.value(gameState, 0, 0, None, None) t = time.time() - start if t >= 1.0: util.pause() # print ('eval time for agent %d: %.4f' % (self.index, t)) return action
def bread(start): directions = [] goalFound = False frontier.push(start) #explored.append(start) while frontier and not goalFound: #while correctPath is not empty and goal not found x = frontier.pop() explored.append(x) print "Explored: ",explored print "directions: ",directions util.pause()#so that I can see what is happenin #directions.append(x) if problem.isGoalState(x): print "Found it!" goalFound = True return frontier for i in problem.getSuccessors(x): print "i = ", i, "\n" if i[0] not in explored: frontier.push(i[0])
def bread(start): directions = [] goalFound = False frontier.push(start) #explored.append(start) while frontier and not goalFound: #while correctPath is not empty and goal not found x = frontier.pop() explored.append(x) print "Explored: ", explored print "directions: ", directions util.pause() #so that I can see what is happenin #directions.append(x) if problem.isGoalState(x): print "Found it!" goalFound = True return frontier for i in problem.getSuccessors(x): print "i = ", i, "\n" if i[0] not in explored: frontier.push(i[0])
def chooseAction(self, gameState): """ Picks among actions randomly. """ actions = gameState.getLegalActions(self.index) #values = [self.evaluateState(gameState,a) for a in actions] values = [ self.minmax(gameState.generateSuccessor(self.index, a), self.index, 0) for a in actions ] maxValue = max(values) bestActions = [a for a, v in zip(actions, values) if v == maxValue] print(bestActions) #minimax(self, gameState, agentIndex, depth) ''' You should change this in your own agent. ''' util.pause() #return random.choice(actions) return random.choice(bestActions)
def main_wrapper(): freeze_support() logger.info( color("bold_green") + f"已将工作目录设置为小助手所在目录:{dirpath},之前为:{old_path}") try: run_start_time = datetime.datetime.now() main() total_used_time = datetime.datetime.now() - run_start_time logger.warning(color("fg_bold_yellow") + f"运行完成,共用时{total_used_time}") # 如果总用时太高的情况时,尝试提示开启多进程和超快速模式 cfg = config() if total_used_time > datetime.timedelta(minutes=10) and ( not cfg.common.enable_multiprocessing or not cfg.common.enable_super_fast_mode): msg = (f"当前累计用时似乎很久({total_used_time}),是否要尝试多进程和超快速模式?\n" "多进程模式下,将开启多个进程并行运行不同账号的领取流程\n" "额外开启超快速模式,会进一步将不同账号的不同活动都异步领取,进一步加快领取速度\n" "\n" "如果需要开启,请打开配置工具,在【公共配置】tab中勾选【是否启用多进程功能】和【是否启用超快速模式(并行活动)】") logger.warning(color("bold_cyan") + msg) if is_weekly_first_run("用时过久提示"): async_message_box(msg, "用时过久", print_log=False) # 按照分钟级别来统计使用时长 total_minutes = int(total_used_time.total_seconds()) // 60 increase_counter(ga_category="run_used_time_minutes", name=total_minutes) except Exception as e: show_unexpected_exception_message(e) # 如果在github action,则继续抛出异常 if is_run_in_github_action(): raise e finally: close_pool() # 暂停一下,方便看结果 if not disable_pause_after_run() and not is_run_in_github_action(): pause()
def predict_dataset1(theta_colvec, num_features, mu_rowvec, sigma_rowvec): """Predict profits based on the trained theta vals.""" if None in (mu_rowvec, sigma_rowvec): population = 3.5 else: population = (3.5 - mu_rowvec[0, 0]) / sigma_rowvec[0, 0] predict1 = np.reshape([1, population], newshape=(1, num_features + 1)) @ theta_colvec print('For population = 35,000, ' f'we predict a profit of {predict1[0, 0]*10000}') if None in (mu_rowvec, sigma_rowvec): population = 7 else: population = (7 - mu_rowvec[0, 0]) / sigma_rowvec[0, 0] predict2 = np.reshape([1, population], newshape=(1, num_features + 1)) @ theta_colvec print('For population = 70,000, ' f'we predict a profit of {predict2[0, 0]*10000}') util.pause('Program paused. Press enter to continue.')
def plot_dataset(feature_matrix, output_colvec, num_features, dataset_title, dataset_xlabel='X', dataset_ylabel='Y'): """Plot data as a scatter diagram.""" fig = None subplot = None if num_features == 1: print('Plotting Data ...') fig, subplot = \ scatter_plot(feature_matrix[:, 1], output_colvec, xlabel=dataset_xlabel, ylabel=dataset_ylabel, title=dataset_title, marker='.', color='r', linewidths=0.02, label='Training data') util.pause('Program paused. Press enter to continue.\n') return fig, subplot
def next(baseUrl, url, numRemaining, progress): pageUrls = set() nextUrl = '' r = requests.get(url) progress.next() util.pause() page = BeautifulSoup(r.text, 'html.parser') for result in page.find_all('p', {'class': 'result-info'}): if numRemaining == 0: return pageUrls pageUrl = result.a['href'] pageUrls.add(pageUrl) numRemaining = numRemaining - 1 nextRelUrlTag = page.find('a', {'class': 'button next'}) if nextRelUrlTag != None: nextRelUrl = nextRelUrlTag['href'] if len(nextRelUrl) > 0: nextUrl = baseUrl + nextRelUrl pageUrls.update(next(baseUrl, nextUrl, numRemaining, progress)) return pageUrls
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ "*** YOUR CODE HERE ***" state = problem.getStartState() #start to iterate through the successors directions = [] explored = [] #print "Explored :", explored util.pause()#so that I can see what is happening
def run(self): """ This is a main function of the class, which should be called after setup of all parameters """ for cur_try in range(1, self.retry_count_on_data_read_error + 1): # Ref: https://stackoverflow.com/questions/2083987/how-to-retry-after-exception try: res = self._run_single() return res except OSError as err: self._info() self._info(f"OS Error. {type(err)}: {err.strerror} (errno = {err.errno}, filename = {err.filename})") if not util.pause(self.retry_pause_on_data_read_error): return self.ReturnCode.PROGRAM_INTERRUPTED_BY_USER if cur_try < self.retry_count_on_data_read_error: self._info(f"Retry {cur_try + 1} of {self.retry_count_on_data_read_error}") else: self._info("Skip file. The hash for it can't be calculated due to the errors.\n") return self.ReturnCode.DATA_READ_ERROR
def _handle_input_file(self, hash_storage: hash_storages.HashStorageAbstract, input_file_name): """ Handle single input file input_file_name """ if not isinstance(hash_storage, hash_storages.HashStorageAbstract): raise TypeError( f"HashStorageAbstract expected, {type(hash_storage)} found") start_date_time = datetime.now() self._info("Handle file start time: " + util.get_datetime_str(start_date_time) + " (" + input_file_name + ")") # Ref: https://stackoverflow.com/questions/82831/how-do-i-check-whether-a-file-exists-without-exceptions if not self._cmd_line_args.force_calc_hash and hash_storage.has_hash( input_file_name): self._info("Hash for file '" + input_file_name + "' exists ... calculation of hash skipped.") return ExitCode.OK_SKIPPED_ALREADY_CALCULATED self._info("Calculate hash for file '" + input_file_name + "'...") calc = hash_calc.FileHashCalc() calc.file_name = input_file_name calc.hash_str = self._cmd_line_args.hash_algo calc.suppress_console_reporting_output = self._cmd_line_args.suppress_console_reporting_output calc.retry_count_on_data_read_error = self._cmd_line_args.retry_count_on_data_read_error calc.retry_pause_on_data_read_error = self._cmd_line_args.retry_pause_on_data_read_error calc_res = calc.run() if calc_res != hash_calc.FileHashCalc.ReturnCode.OK: if calc_res == hash_calc.FileHashCalc.ReturnCode.PROGRAM_INTERRUPTED_BY_USER: return ExitCode.PROGRAM_INTERRUPTED_BY_USER elif calc_res == hash_calc.FileHashCalc.ReturnCode.DATA_READ_ERROR: return ExitCode.DATA_READ_ERROR else: raise Exception( f"Error on calculation of the hash: {calc_res}") hash_value = calc.result hash_storage.set_hash(input_file_name, hash_value) output_file_name = hash_storage.get_hash_file_name(input_file_name) self._info("HASH:", hash_value, "(storage in file '" + output_file_name + "')") end_date_time = datetime.now() self._info("Handle file end time: " + util.get_datetime_str(end_date_time) + " (" + input_file_name + ")") seconds = int((end_date_time - start_date_time).total_seconds()) # print("Elapsed time: {0}:{1:02d}:{2:02d}".format(int(seconds / 60 / 60), int(seconds / 60) % 60, seconds % 60)) file_size = os.path.getsize(input_file_name) speed = file_size / seconds if seconds > 0 else 0 self._info( f"Elapsed time for file: {util.format_seconds(seconds)} (Average speed: {util.convert_size_to_display(speed)}/sec)" ) if self._cmd_line_args.pause_after_file is not None: if not util.pause(self._cmd_line_args.pause_after_file): # Return specific error code return ExitCode.PROGRAM_INTERRUPTED_BY_USER return ExitCode.OK
#!/usr/bin/env python3 # coding: utf-8 from util import pause def bissextile(year): bis = ( year % 400 == 0 ) or ( year % 4 == 0 and year % 100 != 0 ) print('Bissextile' if bis else 'Non bissextile') year = None while type(year) != int: try: year = int(input("Entrez une année : ")) except: continue else: bissextile(year) pause()
def runClassifier(args, options): featureFunction = args['featureFunction'] classifier = args['classifier'] printImage = args['printImage'] # Load data numTraining = options['train'] if(options['data']=="faces"): rawTrainingData = samples.loadDataFile("facedata/facedatatrain", numTraining,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT) trainingLabels = samples.loadLabelsFile("facedata/facedatatrainlabels", numTraining) rawValidationData = samples.loadDataFile("facedata/facedatatrain", TEST_SET_SIZE,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT) validationLabels = samples.loadLabelsFile("facedata/facedatatrainlabels", TEST_SET_SIZE) rawTestData = samples.loadDataFile("facedata/facedatatest", TEST_SET_SIZE,FACE_DATUM_WIDTH,FACE_DATUM_HEIGHT) testLabels = samples.loadLabelsFile("facedata/facedatatestlabels", TEST_SET_SIZE) else: rawTrainingData = samples.loadDataFile("digitdata/trainingimages", numTraining,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT) trainingLabels = samples.loadLabelsFile("digitdata/traininglabels", numTraining) rawValidationData = samples.loadDataFile("digitdata/validationimages", TEST_SET_SIZE,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT) validationLabels = samples.loadLabelsFile("digitdata/validationlabels", TEST_SET_SIZE) rawTestData = samples.loadDataFile("digitdata/testimages", TEST_SET_SIZE,DIGIT_DATUM_WIDTH,DIGIT_DATUM_HEIGHT) testLabels = samples.loadLabelsFile("digitdata/testlabels", TEST_SET_SIZE) # Extract features print "Extracting features..." trainingData = map(featureFunction, rawTrainingData) validationData = map(featureFunction, rawValidationData) testData = map(featureFunction, rawTestData) # Conduct training and testing print "Training..." classifier.train(trainingData, trainingLabels, validationData, validationLabels) print "Validating..." guesses = classifier.classify(validationData) correct = [guesses[i] == validationLabels[i] for i in range(len(validationLabels))].count(True) print str(correct), ("correct out of " + str(len(validationLabels)) + " (%.1f%%).") % (100.0 * correct / len(validationLabels)) print "Testing..." guesses = classifier.classify(testData) correct = [guesses[i] == testLabels[i] for i in range(len(testLabels))].count(True) print str(correct), ("correct out of " + str(len(testLabels)) + " (%.1f%%).") % (100.0 * correct / len(testLabels)) util.pause() analysis(classifier, guesses, testLabels, testData, rawTestData, printImage) # do odds ratio computation if specified at command line if((options['odds']) & (options['classifier'] != "mostfrequent")): class1, class2 = options['class1'], options['class2'] features_class1,features_class2,features_odds = classifier.findHighOddsFeatures(class1,class2) if(options['classifier'] == "naivebayes"): string1 = "=== Features with max P(F_i = 1 | class = %d) ===" % class1 string2 = "=== Features with max P(F_i = 1 | class = %d) ===" % class2 string3 = "=== Features with highest odd ratio of class %d over class %d ===" % (class1, class2) else: string1 = "=== Features with largest weight for class %d ===" % class1 string2 = "=== Features with largest weight for class %d ===" % class2 string3 = "=== Features with for which weight(class %d)-weight(class %d) is biggest ===" % (class1, class2) print string1 printImage(features_class1) print string2 printImage(features_class2) print string3 printImage(features_odds)