def potential_move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False return True
def _cal_acc(embedding, label_arr): assert len(embedding) == len(label_arr) == 12000, "wrong feature num" fea_l = embedding[:6000] fea_r = embedding[6000:] assert label_arr[:6000].all() == label_arr[6000:].all( ), "wrong label order" labels = label_arr[:6000] scores = utils.cos(fea_l, fea_r) return utils.best_acc(scores, labels)
def search(query_pil, model, db='index.json'): if type(db) == str: with open(db) as json_file: data = json.load(json_file) db = pd.DataFrame(data) query = transform(query_pil.convert('RGB')) qFeatures = model(query.unsqueeze(0)) qGram = gram_matrix(qFeatures).flatten() scores = db['Gram'].apply(lambda x: cos(torch.tensor(x), qGram).item()) name = db['Name'][scores.argmax()] score = round(scores.max(), 3) return name, score
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 if self.loggable: self.log() return True
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 #print("[x,y]" + str(self.current_position['x']) + "," + str(self.current_position['y'])) self.path_history.append([self.current_position['x'], self.current_position['y']]) if self.loggable: self.log() return True
def axisAndAngle2RotMatrix(axis, angle): """ http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector Return the rotation matrix associated with counterclockwise rotation about the given axis by angle radians. """ axis = np.asarray(axis) angle = np.asarray(angle) axis = old_div(axis, utils.sqrt(np.dot(axis, axis))) a = utils.cos(old_div(angle, 2)) b, c, d = -axis * utils.sin(old_div(angle, 2)) aa, bb, cc, dd = a * a, b * b, c * c, d * d bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)], [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)], [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 #self.diffrobot.go_forward(distance=200, dc=100) self.diffrobot.driveGyro(10) self.shovel.moveShovel() #sleep(3) if self.loggable: self.log() return True
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 if self.loggable: self.log() #self.car.rotate(90*self.current_direction) #print("Move") #print('%d, %d' % (next_pos_x, next_pos_y)) self.car.move_step_grid(Vector(next_pos_x, next_pos_y)) time.sleep(0.1) return True
texts = [[word for word in document.lower().split()] for document in documents] dictionary = corpora.Dictionary(texts) print len(dictionary.token2id) corpus = [dictionary.doc2bow(text) for text in texts] tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] file = open(output_file3, "w") c = 0 for doc in corpus_tfidf: print c if c < len(event_map.keys()): event_map[c]["feature"] = doc else: maxsim, assign = 0, -1 for k, v in event_map.iteritems(): if weibo[c - len(event_map.keys())]["cls"] == v["cls"] and 0 <= weibo[ c - len(event_map.keys())]["day"] - v["stime"] <= 28: sim = cos(v["feature"], doc) if sim > maxsim: maxsim, assign = sim, k if maxsim >= 0.15: file.write( str(assign) + "\t" + str(weibo[c - len(event_map.keys())]["cls"]) + "\t" + str(weibo[c - len(event_map.keys())]["day"]) + "\t" + weibo[c - len(event_map.keys())]["cid"] + "\t" + weibo[c - len(event_map.keys())]["content"] + "\n") c += 1 file.close()
def calculate_next_pos(self): next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) return {'x': next_pos_x, 'y': next_pos_y}
texts = [[word for word in document.lower().split()] for document in documents] dictionary = corpora.Dictionary(texts) print len(dictionary.token2id) corpus = [dictionary.doc2bow(text) for text in texts] tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] event = {1:[],2:[],3:[]} c = 0 for doc in corpus_tfidf: print c feature = doc maxsim, assign = 0, -1 for e in xrange(len(event[news[c][0]])): if abs(event[news[c][0]][e]["stime"] - news[c][1]) <= 14: sim = utils.cos(event[news[c][0]][e]["feature"],feature) # print "---- ---- ----" # print event[news[c][0]][e]["title"][0], news[c][2], sim # print event[news[c][0]][e]["feature"], feature # print "---- ---- ----" if sim > maxsim: maxsim, assign = sim, e # print maxsim if maxsim >= 0.15: event[news[c][0]][assign]["title"].append(news[c][2]) fmap = {} for (p,s) in event[news[c][0]][assign]["feature"]: fmap[p] = s for (p,s) in feature: fmap[p] = s if not fmap.has_key(p) else fmap[p]+s event[news[c][0]][assign]["feature"] = [(p,s) for p,s in fmap.iteritems()]
else: file2.write(line) fileinput.close() file1.close() file2.close() # Do classifying texts = [[word for word in document.lower().split()] for document in documents] dictionary = corpora.Dictionary(texts) print len(dictionary.token2id) corpus = [dictionary.doc2bow(text) for text in texts] tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] file = open(output_file3,"w") c = 0 for doc in corpus_tfidf: print c if c < len(event_map.keys()): event_map[c]["feature"] = doc else: maxsim, assign = 0, -1 for k, v in event_map.iteritems(): if weibo[c-len(event_map.keys())]["cls"] == v["cls"] and 0 <= weibo[c-len(event_map.keys())]["day"] - v["stime"] <= 28: sim = cos(v["feature"],doc) if sim > maxsim: maxsim, assign = sim, k if maxsim >= 0.15: file.write(str(assign)+"\t"+str(weibo[c-len(event_map.keys())]["cls"])+"\t"+str(weibo[c-len(event_map.keys())]["day"])+"\t"+weibo[c-len(event_map.keys())]["cid"]+"\t"+weibo[c-len(event_map.keys())]["content"]+"\n") c += 1 file.close()
def is_moving_west(droid): return droid.speed > 0 and round(cos(-droid.heading + 90), 3) < 0
def paintEvent(self, e): qp = QPainter() qp.begin(self) self.resize((len(self.data) + 2) * self.interval, config.channels * config.min_channel_size) qp.setPen( QPen(config.second_channel_point_color, config. first_channel_line_spacing)) ######可以试下画刷 setBrush,10指定点的大小 qp.drawLine(0, 0, 0, config.channels * config.min_channel_size) qp.drawLine(0, config.min_channel_size, self.width(), config.min_channel_size) qp.setPen(QPen(Qt.black, 5)) ######可以试下画刷 setBrush,10指定点的大小 for index in range(0, config.min_channel_size, config.coordinate_interval): qp.drawPoint(0, config.min_channel_size - index) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(config.y_axes_distance, config.min_channel_size - index, config.coordinate_interval, config.min_channel_size - index + config.font_size), Qt.AlignLeft | Qt.AlignTop, str(index)) for index in range(0, config.min_channel_size, config.coordinate_interval): qp.drawPoint(0, 2 * config.min_channel_size - index) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(config.y_axes_distance, 2 * config.min_channel_size - index, config.coordinate_interval, 2 * config.min_channel_size - index + config.font_size), Qt.AlignLeft | Qt.AlignTop, str(index)) for index in range(0, self.width(), int(self.interval)): qp.drawPoint(index, config.min_channel_size) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(index, config.min_channel_size - config.font_size, index + config.font_size, config.min_channel_size), Qt.AlignLeft | Qt.AlignTop, str(index)) if self.my_sender: if isinstance(self.my_sender, QSlider): print("滑动了") print(self.my_sender.value()) val = self.my_sender.value() self.interval = int(config.default_interval + config.default_slider_interval * (val - config.default_slider_value)) ## 第二条线 if self.my_sender: if isinstance(self.my_sender, QAction): if self.my_sender.text() == "sin": self.new_data = utils.sin(self.data) else: self.new_data = utils.cos(self.data) # else: # new_data = utils.sin(self.data) if self.new_data: qp.setPen( QPen(config.second_channel_point_color, config.point_size)) ######可以试下画刷 setBrush,10指定点的大小 for index, x in enumerate(self.new_data): qp.drawPoint((index + 1) * self.interval, 2 * config.min_channel_size - x) qp.setPen( QPen(config.second_channel_line_color, config.second_channel_line_spacing, config.second_channel_line_type) ) ####前一个random是线条粗线,后一个random是线条类型 for index, x in enumerate(self.new_data): if index == len(self.new_data) - 1: break qp.drawLine( (index + 1) * self.interval, 2 * config.min_channel_size - x, (index + 2) * self.interval, 2 * config.min_channel_size - self.new_data[index + 1]) self.drawLines(qp) ######画线 self.drawPoints(qp) ###画点 qp.end()
texts = [[word for word in document.lower().split()] for document in documents] dictionary = corpora.Dictionary(texts) print len(dictionary.token2id) corpus = [dictionary.doc2bow(text) for text in texts] tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] event = {1: [], 2: [], 3: []} c = 0 for doc in corpus_tfidf: print c feature = doc maxsim, assign = 0, -1 for e in xrange(len(event[news[c][0]])): if abs(event[news[c][0]][e]["stime"] - news[c][1]) <= 14: sim = utils.cos(event[news[c][0]][e]["feature"], feature) # print "---- ---- ----" # print event[news[c][0]][e]["title"][0], news[c][2], sim # print event[news[c][0]][e]["feature"], feature # print "---- ---- ----" if sim > maxsim: maxsim, assign = sim, e # print maxsim if maxsim >= 0.15: event[news[c][0]][assign]["title"].append(news[c][2]) fmap = {} for (p, s) in event[news[c][0]][assign]["feature"]: fmap[p] = s for (p, s) in feature: fmap[p] = s if not fmap.has_key(p) else fmap[p] + s event[news[c][0]][assign]["feature"] = [(p, s)