def updateEffects(self): for key in self.effects.keys(): if self.effects[key][1] > 0: self.effects[key][1] -=1 if self.effects[key][1] == 0: self.effects[key][0] = 0 # "ballFire" effect allows the ball to destroy the bricks without bouncing on them. if self.effects["ballFire"][0]: self.itemconfig(self.ball, fill=self.bricksColors["p"]) else: self.itemconfig(self.ball, fill="#2c3e50") # "barTall" effect increses the bar size. if self.effects["barTall"][0] != self.effectsPrev["barTall"][0]: diff = self.effects["barTall"] - self.effectsPrev["barTall"][0] self.barWidth += diff*60 coords = self.coords(self.ball) self.coords(self.ball, tk._flatten((coords[0]-diff*10, coords[1]-diff*10, coords[2]+diff*10, coords[3]+diff*10))) # "ballTall" effect increases the ball size. if self.effects["ballTall"][0] != self.effectsPrev["ballTall"][0]: diff = self.effects["ballTall"][0] - self.effectsPrev["ballTall"][0] self.ballRadius += diff*10 coords = self.coords(self.ball) self.coords(self.ball, tk._flatten((coords[0]-diff*10, coords[1]-diff*10, coords[2]+diff*10, coords[3]+diff*10))) # "shield" effect allows the ball to bounce once # at the bottom of the screen (it's like an additional life). if self.effects["shield"][0]: self.itemconfig(self.shield, fill=self.bricksColors["b"], state="normal") else: self.itemconfig(self.shield, state="hidden") self.effectsPrev = copy.deepcopy(self.effects)
def updateEffects(self): for key in self.effects.keys(): if self.effects[key][1] > 0: self.effects[key][1] -= 1 if self.effects[key][1] == 0: self.effects[key][0] = 0 # "ballFire" effect allows the ball to destroy bricks without boucing on them. if self.effects["ballFire"][0]: self.itemconfig(self.ball, fill=self.bricksColors["p"]) else: self.itemconfig(self.ball, fill="#2c3e50") # "barTall" effect increases the bar size. if self.effects["barTall"][0] != self.effectsPrev["barTall"][0]: diff = self.effects["barTall"][0] - self.effectsPrev["barTall"][0] self.barWidth += diff*60 coords = self.coords(self.bar) self.coords(self.bar, tk._flatten((coords[0]-diff*30, coords[1], coords[2]+diff*30, coords[3]))) # "ballTall" effect increases the ball size. if self.effects["ballTall"][0] != self.effectsPrev["ballTall"][0]: diff = self.effects["ballTall"][0] - self.effectsPrev["ballTall"][0] self.ballRadius += diff*10 coords = self.coords(self.ball) self.coords(self.ball, tk._flatten((coords[0]-diff*10, coords[1]-diff*10, coords[2]+diff*10, coords[3]+diff*10))) # "shield" effect allows the ball to bounce once # at the bottom of the screen (it's like an additional life). if self.effects["shield"][0]: self.itemconfig(self.shield, fill=self.bricksColors["b"], state="normal") else: self.itemconfig(self.shield, state="hidden") self.effectsPrev = copy.deepcopy(self.effects)
def fit_transform(self, X): train_Feature = [] X = np.array(X) for i in range(len(X)): train_feature = [] for j in range(len(self.FM)): x = X[i][self.FM[j]] # update norms self.norm_max[j][x > self.norm_max[j]] = x[ x > self.norm_max[j]] self.norm_min[j][x < self.norm_min[j]] = x[ x < self.norm_min[j]] # 0-1 normalize x = (x - self.norm_min[j]) / ( self.norm_max[j] - self.norm_min[j] + 0.0000000000000001) train_feature = np.concatenate((train_feature, x)) train_Feature.append(train_feature) for i in range(len(self.FM)): self.norm_max[i] = self.norm_max[i].tolist() self.norm_min[i] = self.norm_min[i].tolist() self.norm_max = np.array(_flatten(self.norm_max)) self.norm_min = np.array(_flatten(self.norm_min)) self.FM = np.array(_flatten(self.FM)) return np.array(train_Feature)
def get_train_batch(self, i): targets, types, regions, ratings, prices = [], [], [], [], [] (neighbors, neighbortypes, neighbordistances, neighborratings, neighborcomments, neighborprices, neighborgroups, lengthes) = ([], [], [], [], [], [], [], []) max_n = 0 for shop in i: (t_neighbors, t_neighbortypes, t_distances, t_ratings, t_comments, t_price, t_group) = self.shop_groups(shop) if max_n < len(t_neighbors): max_n = len(t_neighbors) targets.append(self.comments[shop]) types.append(self.type[[shop]].values.tolist()[0]) regions.append(self.region[shop]) ratings.append(self.ratings[shop]) prices.append(self.price[shop]) neighbors.append(t_neighbors) neighbortypes.append(t_neighbortypes) neighbordistances.append(t_distances) neighborratings.append(t_ratings) neighborcomments.append(t_comments) neighborprices.append(t_price) neighborgroups.append(t_group) lengthes.append(len(t_neighbors)) for i, _ in enumerate(neighbors): n_pad = max_n - len(neighbors[i]) neighbors[i] = neighbors[i] + [self.config['n_shops']] * n_pad neighbortypes[i] = neighbortypes[i] + [self.config['n_types'] ] * n_pad neighbordistances[i] = neighbordistances[i] + [ self.config['n_distances'] ] * n_pad neighborratings[i] = neighborratings[i] + [0] * n_pad neighborcomments[i] = neighborcomments[i] + [0] * n_pad neighborprices[i] = neighborprices[i] + [self.config['n_prices'] ] * n_pad neighborgroups[i] = neighborgroups[i] + [0] * n_pad targets = torch.FloatTensor(targets) #(batch_size*n) types = torch.tensor(_flatten(types)) regions = torch.tensor(_flatten(regions)) ratings = torch.FloatTensor(_flatten(ratings)) prices = torch.FloatTensor(_flatten(prices)) #neighbors=torch.tensor(neighbors) neighbortypes = torch.LongTensor(neighbortypes) neighbordistances = torch.LongTensor(neighbordistances) neighborratings = torch.FloatTensor(neighborratings) neighborcomments = torch.FloatTensor(neighborcomments) neighborprices = torch.LongTensor(neighborprices) neighborgroups = torch.FloatTensor(neighborgroups) return (types, regions, targets, ratings, prices, lengthes, neighbortypes, neighbordistances, neighborratings, neighborcomments, neighborprices, neighborgroups)
def merge_txt(src_id): path = '/Volumes/pulsar/WR/1671/txt/' epoch_file = 'epoch_src_{0}_G.txt'.format(src_id) epoch_all = np.loadtxt(path + epoch_file) obs_tstart = epoch_all[:, 0] obs_tstop = epoch_all[:, 1] obs_ID_all = epoch_all[:, 2] obs_expt = epoch_all[:, -1] obs_ID_all = obs_ID_all.astype(int) res_t = [] res_E = [] res_ID = [] epoch_ID = [] epoch_start = [] epoch_stop = [] epoch_expt = [] for i in range(len(obs_ID_all)): res_temp = np.loadtxt(path + 'bkg_txt/' + str(src_id) + '_bkg_{0}.txt'.format(obs_ID_all[i])) if len(res_temp) == 0: continue elif type(res_temp[0]) == type(np.array([1.2])[0]): ##判断是否只有1个光子,数据类型的bug,1.2只是随便取的一个值,任意float均可 epoch_ID.append(obs_ID_all[i]) epoch_start.append(obs_tstart[i]) epoch_stop.append(obs_tstop[i]) epoch_expt.append(obs_expt[i]) res_t.append(list([res_temp[0]])) res_E.append(list([res_temp[1]])) res_ID.append(list(([res_temp[2]]))) else: epoch_ID.append(obs_ID_all[i]) epoch_start.append(obs_tstart[i]) epoch_stop.append(obs_tstop[i]) epoch_expt.append(obs_expt[i]) res_t.append(list(res_temp[:, 0])) res_E.append(list(res_temp[:, 1])) res_ID.append(list((res_temp[:, 2]))) res_t = list(_flatten(res_t)) res_E = list(_flatten(res_E)) res_ID = list(_flatten(res_ID)) result = np.column_stack((res_t, res_E, res_ID)) epoch_info = np.column_stack( (epoch_start, epoch_stop, epoch_ID, epoch_expt)) np.savetxt(path + 'epoch_bkg_' + str(src_id) + '_G.txt', epoch_info, fmt='%15.2f %15.2f %10d %20.2f') np.savetxt(path + str(src_id) + '_bkg_G.txt', result, fmt="%.7f %5.3f %d")
def alignment(size, trials): S = cn[0:size] T = en[0:size] p_s_t = {} c_s_t = {} total_T = {} total_S = {} S_flatten = _flatten(S) T_flatten = _flatten(T) counter_S = Counter(S_flatten) counter_T = Counter(T_flatten) # initial for word_S in counter_S.keys(): p_s_t[word_S] = {} c_s_t[word_S] = {} total_S[word_S] = 0 for i in range(size): for word_S in S[i]: for word_T in T[i]: p_s_t[word_S].setdefault(word_T, 1) c_s_t[word_S].setdefault(word_T, 0) for word_T in counter_T.keys(): total_T[word_T] = 0 for trial in range(trials): print(trial) c_s_t_dist = c_s_t total_T_dist = total_T total_S_dist = total_S for z in range(size): for word_S in S[z]: total_S_dist[word_S] = 0 for word_T in T[z]: total_S_dist[word_S] = total_S_dist[word_S] + p_s_t[word_S][word_T] for word_S in S[z]: for word_T in T[z]: c_s_t_dist[word_S][word_T] = c_s_t_dist[word_S][word_T] + p_s_t[word_S][word_T] / total_S_dist[ word_S] total_T_dist[word_T] = total_T_dist[word_T] + p_s_t[word_S][word_T] / total_S_dist[word_S] for word_S in counter_S.keys(): for word_T in p_s_t[word_S].keys(): p_s_t[word_S][word_T] = c_s_t_dist[word_S][word_T] / total_T_dist[word_T] return p_s_t
def change_size(self, size): self.size = size scaled_trunk_coords = scaled_coordinates(apple_trunk_points, self.base_x, self.base_y, self.size) scaled_leafs_coords = scaled_coordinates(apple_leafs_points, self.base_x, self.base_y, self.size) canvas.coords(self.leafs, tkinter._flatten(scaled_leafs_coords)) canvas.coords(self.trunk, tkinter._flatten(scaled_trunk_coords))
def merge_txt(inpath, outpath, outname, epoch_file, ecf): res_t = [] res_E = [] res_ID = [] epoch_ID = [] epoch_start = [] epoch_stop = [] epoch_expt = [] epoch_all = np.loadtxt(inpath + epoch_file) if epoch_all.ndim == 1: epoch_all = np.array([epoch_all]) obs_tstart = epoch_all[:, 0] obs_tstop = epoch_all[:, 1] obs_ID_all = epoch_all[:, 2] obs_expt = epoch_all[:, 3] obs_ID_all = obs_ID_all.astype(int) for i in range(len(obs_ID_all)): obsid = obs_ID_all[i] txt_filename = inpath + 'txt_psf{0}_{1}/'.format( int(ecf * 100), obsid) + outname + '_' + str(obsid) + '.txt' # txt_filename=inpath+'txt_psf50_{0}/'.format(obsid)+outname+'_bkg_'+str(obsid)+'.txt' if os.path.exists(txt_filename): res_temp = np.loadtxt(txt_filename) if res_temp.size == 0: print('Empty') continue if res_temp.ndim == 1: res_temp = np.array([res_temp]) if res_temp.ndim == 2: res_t.append(list(res_temp[:, 0])) res_E.append(list(res_temp[:, 1])) res_ID.append(list((res_temp[:, 2]))) epoch_ID.append(obs_ID_all[i]) epoch_start.append(obs_tstart[i]) epoch_stop.append(obs_tstop[i]) epoch_expt.append(obs_expt[i]) res_t = list(_flatten(res_t)) res_E = list(_flatten(res_E)) res_ID = list(_flatten(res_ID)) result = np.column_stack((res_t, res_E, res_ID)) result = result[result[:, 0].argsort()] epoch_info = np.column_stack( (epoch_start, epoch_stop, epoch_ID, epoch_expt)) np.savetxt(outpath + 'epoch_src_' + str(outname) + '.txt', epoch_info, fmt='%15.2f %15.2f %10d %20.2f') # np.savetxt(outpath + 'epoch_bkg_' + str(outname) + '.txt', epoch_info, # fmt='%15.2f %15.2f %10d %20.2f') np.savetxt(outpath + outname + '.txt', result, fmt="%.7f %10.3f %10d")
def change_size(self, size): self.size = size scaled_trunk_coords = scaled_coordinates( apple_trunk_points, self.base_x, self.base_y, self.size ) scaled_leafs_coords = scaled_coordinates( apple_leafs_points, self.base_x, self.base_y, self.size ) canvas.coords(self.leafs, tkinter._flatten(scaled_leafs_coords)) canvas.coords(self.trunk, tkinter._flatten(scaled_trunk_coords))
def randFiles(self, trainingImages, trainingPatches): random_indx = np.random.randint(low=0, high=self.size(), size=trainingImages) random_bgrFiles, random_depthFiles, random_infoFiles = [], [], [] for i in range(trainingImages): random_bgrFiles.append([self.bgrFiles[random_indx[i]]] * trainingPatches) random_depthFiles.append([self.depthFiles[random_indx[i]]] * trainingPatches) random_infoFiles.append([self.infoFiles[random_indx[i]]] * trainingPatches) self.rand_bgrFiles = list(_flatten(random_bgrFiles)) self.rand_depthFiles = list(_flatten(random_depthFiles)) self.rand_infoFiles = list(_flatten(random_infoFiles))
def change_size(self, size): self.size = size scaled_leaf_coords = scaled_coordinates(self.leaf_points, self.base_x, self.base_y, self.size) canvas.coords(self.leaf, tkinter._flatten(scaled_leaf_coords)) scaled_fruit_coords = scaled_coordinates(self.fruit_points, self.base_x, self.base_y, self.size) canvas.coords(self.fruit, tkinter._flatten(scaled_fruit_coords)) scaled_stick_coords = scaled_coordinates(self.stick_points, self.base_x, self.base_y, self.size) canvas.coords(self.stick, tkinter._flatten(scaled_stick_coords))
def reset(self): self.score = 0 self.barWidth = 100 self.ballRadius = 7 self.coords(self.shield, (0, self.screenHeight-5, self.screenWidth, self.screenHeight)) self.itemconfig(self.shield, fill=self.bricksColors["b"], state="hidden") self.shieldVisibility = False self.coords(self.bar, ((self.screenWidth - self.barWidth)/2, self.screenHeight - self.barHeight, (self.screenWidth + self.barWidth)/2, self.screenHeight)) self.coords(self.ball, (self.screenWidth/2 - self.ballRadius, self.screenHeight - self.barHeight - 2*self.ballRadius, self.screenWidth/2 + self.ballRadius, self.screenHeight - self.barHeight)) self.itemconfig(self.ball, fill="#2c3e50") self.coords(self.ballNext, tk._flatten(self.coords(self.ball))) self.effects = { "ballFire": [0, 0], "barTall": [0, 0], "ballTall": [0, 0], "shield": [0, -1], } self.effectsPrev = copy.deepcopy(self.effects) self.ballThrown = False self.keyPressed = [False, False] self.losed = False self.won = False self.ballAngle = math.radians(90) for brick in self.bricks: self.delete(brick) del brick
def set(self, rc=None, index=None, *args, **kwargs): """If rc is specified (either 'row' or 'col') then it is assumes that args (if given) represents values which will be set into the subsequent columns (if row is specified) or rows (for col). If index is not None and args is not given, then it will return the value(s) for the cell(s) specified. If kwargs is given, assumes that each key in kwargs is a index in this table and sets the specified index to the associated value. Table validation will not be triggered via this method. Note that the table must have an associated array (defined through the variable option) in order to this work.""" if not args and index is not None: if rc: args = (rc, index) else: args = (index, ) return self.tk.call(self._w, 'set', *args) if rc is None: args = tkinter._flatten(list(kwargs.items())) self.tk.call(self._w, 'set', *args) else: self.tk.call(self._w, 'set', rc, index, args)
def find_interfer_line(im): data = np.array(im.getdata()).reshape((23, 60)) index_array = [] for i in range(23): index_array.append(get_index([int(item / 255) for item in data[i, :]])) result = [] index = [] for i in range(22): if len(index_array[i])>0: for j in range(len(index_array[i])): k = i+1 result1 = [] result1.append(index_array[i][j]) head = index_array[i][j] tag = index_array[k] while (k <= 22): content = find_near_arr(head, tag) if (content is not None) and sum([set(content).issubset(item) for item in result1])<len(content): index_array[k].remove(content) result1.append(content) head = content k += 1 if k<=22: tag = index_array[k] else: break if len(list(set(_flatten(result1))))>13: # print(i,result1) index.append(i) result.append(del_index(result1)) return index,result
def test(test_data,test_label): print("开始进行测试。。。") #save_path = os.path.join(PATH,'checkpoint/bilstm') saver = tf.train.import_meta_graph(os.path.join(test_save_path,"best_validation.meta")) with tf.Session() as sess: #sess.run(tf.global_variables_initializer()) saver.restore(sess, os.path.join(predict_save_path,"best_validation")) # 读取保存的模型 data_len = len(test_data) test_batchsize = 128 batch_test = batch_iter(test_data, test_label, 128, is_train=False) pred_label = [] for x_batch,y_batch in batch_test: feed_dict = { model.inputX: x_batch, model.inputY: y_batch, model.dropoutKeepProb: 1.0, } predictions = sess.run([model.predictions], feed_dict) pred_label.append(predictions[0].tolist()) pred_label = list(_flatten(pred_label)) test_label = [np.argmax(item) for item in test_label] # 评估 print("计算Precision, Recall and F1-Score...") print(metrics.classification_report(test_label, pred_label, target_names=true_labelList))
def get_curve_attributions(data): attributions = [] attributions_time_response10_90 = [] attributions_response_sensitivity = [] attributions_recover_sensitivity = [] length = len(data[0]) for i in range(len(data)): "取响应阶段初始值" init_value = min(data[i][:length//2]) init_value_index = data[i].index(init_value) "取响应阶段最大值" response_value = max(data[i][:length]) response_value_index = data[i].index(response_value) "求响应敏感度" attributions_response_sensitivity.append(10 * (response_value - init_value) / init_value) "取响应10-90%的时间" response_10percent_value = (9*init_value + response_value)/10 # 取上升10%的时间 response_10percent_index = get_similar_value_index(data[i][init_value_index:response_value_index], response_10percent_value) response_90percent_value = (init_value + 9 * response_value) / 10 # 取上升90%的时间 response_90percent_index = get_similar_value_index(data[i][init_value_index:response_value_index], response_90percent_value) attributions_time_response10_90.append((response_90percent_index - response_10percent_index) / 60) "取恢复阶段结束后的值" recover_value = min(data[i][length//2:length]) "求恢复敏感度" attributions_recover_sensitivity.append(10 * (response_value - recover_value) / recover_value) attributions.append(attributions_response_sensitivity) attributions.append(attributions_recover_sensitivity) attributions.append(attributions_time_response10_90) attributions = list(_flatten(attributions)) return attributions
def writeKeyword(): if request.method == 'POST': d = request.get_json() keyword_list_new = [] for tmp_map in d: keyword_list_new.append(tmp_map['key']) keyword_list_new_set = set(keyword_list_new) # 用户更改的关键词 keywords_loc = readTXT(keywordsPath) keyword_list_loc = [] for item in keywords_loc: keyword_list_loc.append(item.split(' ')) keyword_list_loc = list(_flatten(keyword_list_loc)) keyword_list_loc_set = set(keyword_list_loc) # 本地保存所保存的关键词 keyword_list_old_set = set(keyword_list_old) # ocr识别时数据被打包的关键词 write_keywords = keyword_list_new_set - keyword_list_old_set write_keywords = list(write_keywords - keyword_list_loc_set) print(write_keywords) if write_keywords: with open(keywordsPath, 'a') as f: # 'a'表示append,即在原来文件内容后继续写数据 for keyword in write_keywords: f.write(keyword + '\n') info = json.dumps({"info": "已写入字段"}, ensure_ascii=False) else: info = json.dumps({"info": "已检测字段"}, ensure_ascii=False) return info
def FIRST(n): if first[n]: # 如果已经求出来了 不是None return first[n] for lis in grammar[n]: # 对于n的每一条产生式 first_candidate[n].append(FIRST_CANDIDATE(lis)) # 求这条产生式右端符号串的First集 first[n] = list(set(_flatten(first_candidate[n]))) # 求每条产生式First集的交集 return first[n] # 返回First集
def CreateVSM(DocumentList): """ 生成向量空间模型 :param DocumentList:分词结果列表 :return: list 每篇文档的词向量组成的列表 """ database = list(_flatten(DocumentList)) #将所有文档整合成语料库 Vocabulary = Create_Vocabulary(CountWords(database), 10000, 23000) #创建词典,词典大小为b-a Document_Frequency = DocumentFrequency(DocumentList) IDF_v = IDF(Vocabulary) #print(type(IDF_v)) TF_list = [] VSM = [] for Document in Document_Frequency: TF_v = TF(Document, Vocabulary) TF_list.append(TF_v) VSM_v = TF_v * IDF_v print(IDF_v, TF_v, VSM_v) #print(VSM_v) VSM.append(VSM_v) #保存所有文档的TF TF_array = numpy.array(TF_list) print("the TF_array is ", TF_array.shape) TF_array.tofile(r"E:\data mining\TF.txt") #保存所有文档的向量列表 VSM_array = numpy.array(VSM) print(VSM_array.shape) VSM_array.tofile(r'E:\data mining\VSM.txt') return VSM_array
def detectingTextCoverage(textPath=None, wlPath=None): """ 检测文本中的字符的覆盖面 :param textPath: :return: """ if textPath == None: textPath = "./data/corpus" if wlPath == None: wlPath = chn_lib_path wl = readTXT(wlPath) wl_set = set(wl) textName = os.listdir(textPath) text_all = [] for name in textName: path_name = os.path.join(textPath, name) text_all.append(readTXT(path_name)) text_all = list(_flatten(text_all)) # 多维转一维 chars = [] for string in text_all: for char in set(string): chars.append(char) chars_set = set(chars) print("字库中字符总数:{}".format(len(wl_set))) print("字库中不存在的字符:{}".format(''.join(sorted(chars_set - wl_set)))) print("文章中不存在的字符:{}".format(''.join(sorted(wl_set - chars_set)))) characteRcoverage = len(chars_set - (wl_set - chars_set)) / len(wl_set) print("字符覆盖率:{:.2f}%".format(characteRcoverage * 100))
def change_position(self, base_x, base_y): self.base_x = base_x self.base_y = base_y scaled_leaf_coords = scaled_coordinates(self.leaf_points, self.base_x, self.base_y, self.size) canvas.coords(self.leaf, tkinter._flatten(scaled_leaf_coords)) scaled_fruit_coords = scaled_coordinates(self.fruit_points, self.base_x, self.base_y, self.size) canvas.coords(self.fruit, tkinter._flatten(scaled_fruit_coords)) scaled_stick_coords = scaled_coordinates(self.stick_points, self.base_x, self.base_y, self.size) canvas.coords(self.stick, tkinter._flatten(scaled_stick_coords))
def NBC(traindict,testdict,trainnum): i=0 j=0 for key2 in testdict.keys(): testclass=[] count=0 for doclist in testdict[key2]: pdict={} for key1 in traindict.keys(): prior=numpy.log(len(traindict[key1])/trainnum) classlist=list(_flatten(traindict[key1])) frequencydict = dict(Counter(classlist)) total=len(classlist) classwordnum=len(list(set(classlist))) otraindict = traindict.copy() otraindict.pop(key1) allotherlist=[] for key3 in otraindict.keys(): eachclasslist=list(_flatten(otraindict[key3])) allotherlist.append(eachclasslist) otherclasslist=list(_flatten(allotherlist)) othertotal=len(otherclasslist) frequency = dict(Counter(otherclasslist)) num=len(list(set(otherclasslist))) eachdoc=0 for each in doclist: wordcount=frequencydict[each] if each in frequencydict.keys() else 0 othercount=frequency[each] if each in frequency.keys() else 0 eachclass=numpy.log((wordcount+1)/(total+classwordnum)) eachnotinclass=numpy.log((othercount+1)/(othertotal+num)) eachword=eachclass-eachnotinclass eachdoc+=eachword eachdoc+=prior pdict.setdefault(key1,eachdoc) p=max(pdict,key=pdict.get) testclass.append(p) for each in testclass: if each==key2: count+=1 n=count/len(testclass) i+=count j+=len(testclass) acc=i/j return acc
def change_size(self, size): self.size = size scaled_leaf_coords = scaled_coordinates( self.leaf_points, self.base_x, self.base_y, self.size ) canvas.coords(self.leaf, tkinter._flatten(scaled_leaf_coords)) scaled_fruit_coords = scaled_coordinates( self.fruit_points, self.base_x, self.base_y, self.size ) canvas.coords(self.fruit, tkinter._flatten(scaled_fruit_coords)) scaled_stick_coords = scaled_coordinates( self.stick_points, self.base_x, self.base_y, self.size ) canvas.coords(self.stick, tkinter._flatten(scaled_stick_coords))
def cut_words(): # jieba.load_userdict("add_words.txt") words = [] with open("cleared_comments.txt", "r") as fp: for line in fp.readlines(): words.append(jieba.lcut(line)) words = list(_flatten(words)) return words
def tick_effects(self): for key in self.effects.keys(): if self.effects[key][1] > 0: self.effects[key][1] -= 1 if self.effects[key][1] == 0: self.effects[key][0] = 0 # "ball.onFire" effect allows the ball to destroy bricks without bouncing on them. if self.effects["ball.onFire"][0]: self.itemconfig(self.ball, fill=self.COLOR_MAPPING["y"]) else: self.itemconfig(self.ball, fill="#ffffff") # "bar.isTall" effect increases the bar size. if self.effects["bar.isTall"][0] != self.lastKnownEffectsState[ "bar.isTall"][0]: diff = self.effects["bar.isTall"][0] - self.lastKnownEffectsState[ "bar.isTall"][0] self.barWidth += diff * 60 coords = self.coords(self.bar) self.coords( self.bar, tk._flatten((coords[0] - diff * 30, coords[1], coords[2] + diff * 30, coords[3]))) # "ball.isTall" effect increases the ball size. if self.effects["ball.isTall"][0] != self.lastKnownEffectsState[ "ball.isTall"][0]: diff = self.effects["ball.isTall"][0] - self.lastKnownEffectsState[ "ball.isTall"][0] self.ballRadius += diff * 10 coords = self.coords(self.ball) self.coords( self.ball, tk._flatten((coords[0] - diff * 10, coords[1] - diff * 10, coords[2] + diff * 10, coords[3] + diff * 10))) # "shield.visible" effect allows the ball to bounce once # at the bottom of the screen (it's like an additional life). if self.effects["shield.visible"][0]: self.itemconfig(self.shieldVisible, fill=self.COLOR_MAPPING["b"], state="normal") else: self.itemconfig(self.shieldVisible, state="hidden") self.lastKnownEffectsState = copy.deepcopy(self.effects)
def get_gt_image_name2(img_name, gt_images_name_list): name_info_list = [x.split('-') for x in img_name.split('_')] from tkinter import _flatten name_info_list = list(_flatten(name_info_list)) for gt_image_name in gt_images_name_list: gt_image_name_list = [ x.split('-') for x in gt_image_name.split('_') ] gt_image_name_list = list(_flatten(gt_image_name_list)) find_flag = True for name_info in name_info_list: if name_info not in gt_image_name_list: find_flag = False break if find_flag: return gt_image_name return None
def change_position(self, base_x, base_y): self.base_x = base_x self.base_y = base_y scaled_leaf_coords = scaled_coordinates( self.leaf_points, self.base_x, self.base_y, self.size ) canvas.coords(self.leaf, tkinter._flatten(scaled_leaf_coords)) scaled_fruit_coords = scaled_coordinates( self.fruit_points, self.base_x, self.base_y, self.size ) canvas.coords(self.fruit, tkinter._flatten(scaled_fruit_coords)) scaled_stick_coords = scaled_coordinates( self.stick_points, self.base_x, self.base_y, self.size ) canvas.coords(self.stick, tkinter._flatten(scaled_stick_coords))
def falling_leaf(self, leaf, leaf_base_x, leaf_base_y, falling_place): leaf_base_y += 5 if leaf_base_y < falling_place: scaled_leaf_coords = scaled_coordinates(leaf_points, leaf_base_x, leaf_base_y, self.size) canvas.coords(leaf, tkinter._flatten(scaled_leaf_coords)) canvas.after(30, self.falling_leaf, leaf, leaf_base_x, leaf_base_y, falling_place)
def move(self, zoom, showOrbit): pX, pY = self.realToScreen(zoom, self.pos.x, self.pos.y) self.canvas.coords(self.p, pX - self.rScreen, pY - self.rScreen, pX + self.rScreen, pY + self.rScreen) if showOrbit: self.posList.pop(0) self.posList.append((pX, pY)) self.canvas.coords(self.orbit, tkinter._flatten(self.posList))
def para2words(paragraphs_list): word_list = [] for i in range(len(paragraphs_list)): line=str(paragraphs_list.pop()) line=filter(line)#过滤每一段 word=line.split(' ') word_list.append(word) word_list=list(_flatten(word_list))#把多为数组转化为一维数组 return pandas.value_counts(word_list) #return word:count
def visual_Frequency(DocumentList): """ 打印数据集中的词频分布 :param DocumentList: 二维列表,每一行存储一篇文章的分词结果 :return: none,打印词频分布图 """ database = list(_flatten(DocumentList)) vocabulary = nltk.FreqDist(database) vocabulary.plot()
def show_grammar(): strings = [] for key in grammar.keys(): strings.append([key + '->']) for lis in grammar[key]: strings[-1].append(''.join(lis) + '|') strings[-1][-1] = strings[-1][-1].replace('|', '\n') strings = ''.join(list(_flatten(strings))) return strings
def main(): path = r'C:\file\20news-18828' documents_wordlist, test_wordlist, train_class_label, test_class_label = documents_read( path) #读入所有文档并进行预处理后将结果存入documents_wordlist列表 total_frequency = collections.Counter(list( _flatten(documents_wordlist))) #去重,得到一个.counter()类 dictionary = dict(total_frequency.most_common()) #得到文档中所有词词频的词典 for key, value in dictionary.items(): #得到去除词频很高或很低的词典(最终使用的词典) if value < 2 & value > 5000: del dictionary[key] # 得到训练集和测试集的所有单文档的词频list,list中是每个文档的词频的counter类 documentsfrequency = documents_frequency(documents_wordlist) test_set_frequency = documents_frequency(test_wordlist) #计算TF*IDF IDF = count_IDF(dictionary) VSM_list = [] #保存最终的VSM for document in documentsfrequency: TF = count_TF(document, dictionary) VSM = IDF * TF VSM_list.append(VSM) VSM_array = numpy.array(VSM_list) print('the type of train_VSM_array is ', VSM_array.shape) test_VSM_list = [] for test_document in test_set_frequency: test_TF = count_TF(test_document, dictionary) test_VSM = IDF * test_TF test_VSM_list.append(test_VSM) test_VSM_array = numpy.array(test_VSM_list) print('the type of test_VSM_array is ', test_VSM_array.shape) #计算test set的cosin值(N*M矩阵) test_values = count_cosinvalue(VSM_array, test_VSM_array) print("N*M矩阵的维数:", test_values.shape) #K-NN方法 K = 25 correct_count = 0 for i in range(len(test_class_label)): train_class_index = numpy.array(train_class_label)[test_values[ i, :].argsort()] train_class_index = train_class_index.reshape(len(train_class_index), ) train_class_index = train_class_index.tolist() test_list = train_class_index[len(train_class_label) - K - 1:len(train_class_label) - 1:] #取出矩阵的每一行元素值排序后的索引表 test_class_count = collections.Counter(test_list) test_class = test_class_count.most_common(1) #返回数量最多的元素及其个数 predict_label = list(dict(test_class).keys())[0] if test_class_label[i] == predict_label: correct_count += 1 correct_rate = correct_count / len(test_class_label) print("the correct rate of classifier is %.2f%%" % (correct_rate * 100)) return correct_rate
def updateEffects(self): for key in self.effects.keys(): if self.effects[key][1] > 0: self.effects[key][1] -= 1 if self.effects[key][1] == 0: self.effects[key][0] = 0 # L'effet "ballFire" permet à la balle de détruire les briques sans rebondir sur elles. if self.effects["ballFire"][0]: self.itemconfig(self.ball, fill=self.bricksColors["p"]) else: self.itemconfig(self.ball, fill="#ffffff") # L'effet "barTall" augmente la taille de la barre. if self.effects["barTall"][0] != self.effectsPrev["barTall"][0]: diff = self.effects["barTall"][0] - self.effectsPrev["barTall"][0] self.barWidth += diff * 60 coords = self.coords(self.bar) self.coords( self.bar, tk._flatten((coords[0] - diff * 30, coords[1], coords[2] + diff * 30, coords[3]))) # L'effet "ballTall" augmente la taille de la balle. if self.effects["ballTall"][0] != self.effectsPrev["ballTall"][0]: diff = self.effects["ballTall"][0] - self.effectsPrev["ballTall"][0] self.ballRadius += diff * 10 coords = self.coords(self.ball) self.coords( self.ball, tk._flatten((coords[0] - diff * 10, coords[1] - diff * 10, coords[2] + diff * 10, coords[3] + diff * 10))) # L'effet "bouclier" permet à la balle de rebondir une fois if self.effects["shield"][0]: self.itemconfig(self.shield, fill=self.bricksColors["b"], state="normal") else: self.itemconfig(self.shield, state="hidden") self.effectsPrev = copy.deepcopy(self.effects)
def separate_direct_indirect_list(removed_invalid_url): # remove same elements removed_invalid_url_set = set(_flatten(removed_invalid_url)) url_direct_list = [] url_redirect_list = [] for url in removed_invalid_url_set: if "link?url=" in url: url_redirect_list.append(url) else: url_direct_list.append(url) return url_direct_list, url_redirect_list
def falling_leaf(self, leaf, leaf_base_x, leaf_base_y, falling_place): leaf_base_y += 5 if leaf_base_y < falling_place: scaled_leaf_coords = scaled_coordinates( leaf_points, leaf_base_x, leaf_base_y, self.size ) canvas.coords(leaf, tkinter._flatten(scaled_leaf_coords)) canvas.after( 30, self.falling_leaf, leaf, leaf_base_x, leaf_base_y, falling_place )
def height(self, row=None, **kwargs): """If row and kwargs are not given, a list describing all rows for which a width has been set is returned. If row is given, the height of that row is returnd. If kwargs is given, then it sets the key/value pairs, where key is a row and value represents the height for the row.""" if row is None and not kwargs: pairs = self.tk.splitlist(self.tk.call(self._w, 'height')) return dict(pair.split() for pair in pairs) elif row: return int(self.tk.call(self._w, 'height', str(row))) args = tkinter._flatten(list(kwargs.items())) self.tk.call(self._w, 'height', *args)
def width(self, column=None, **kwargs): """If column and kwargs are not given, a dict describing all columns for which a width has been set is returned. If column is given, the width of that column is returnd. If kwargs is given, then it sets the key/value pairs, where key is a column and value represents the width for the column.""" if column is None and not kwargs: pairs = self.tk.splitlist(self.tk.call(self._w, 'width')) return dict(pair.split() for pair in pairs) elif column is not None: return int(self.tk.call(self._w, 'width', str(column))) args = tkinter._flatten(list(kwargs.items())) self.tk.call(self._w, 'width', *args)
def spans(self, index=None, **kwargs): """Manipulate row/col spans. When called with no arguments, all known spans are returned as a dict. When called with only the index, the span for that index only is returned, if any. Otherwise kwargs is assumed to contain keys/values pairs used to set spans. A span starts at the row,col defined by a key and continues for the specified number of rows,cols specified by its value. A span of 0,0 unsets any span on that cell.""" if kwargs: args = tkinter._flatten(list(kwargs.items())) self.tk.call(self._w, 'spans', *args) else: return self.tk.call(self._w, 'spans', index)
def reset(self): self.barWidth = 100 self.ballRadius = 7 self.coords(self.shield, (0, self.screenHeight-5, self.screenWidth, self.screenHeight)) self.itemconfig(self.shield, fill=self.bricksColors["b"], state="hidden") self.coords(self.bar, ((self.screenWidth - self.barWidth)/2, self.screenHeight - self.barHeight, (self.screenWidth + self.barWidth)/2, self.screenHeight)) self.coords(self.ball, (self.screenWidth/2 - self.ballRadius, self.screenHeight - self.barHeight - 2*self.ballRadius, self.screenWidth/2 + self.ballRadius, self.screenHeight - self.barHeight)) self.itemconfig(self.ball, fill="#2c3e50") self.coords(self.ballNext, tk._flatten(self.coords(self.ball))) self.effects = { "ballFire": [0, 0], "barTall": [0, 0], "ballTall": [0, 0], "shield": [0, -1], } self.effectsPrev = copy.deepcopy(self.effects) self.ballThrown = False self.keyPressed = [False, False] self.losed = False self.won = False self.ballAngle = math.radians(90) for brick in self.bricks: self.delete(brick) del brick
def moveBall(self): self.move(self.ballNext, self.ballSpeed*math.cos(self.ballAngle), -self.ballSpeed*math.sin(self.ballAngle)) ballNextCoords = self.coords(self.ballNext) # Collisions computation between ball and bricks i = 0 while i < len(self.bricks): collision = self.collision(self.ball, self.bricks[i]) collisionNext = self.collision(self.ballNext, self.bricks[i]) if not collisionNext: brickColor = self.itemcget(self.bricks[i], "fill") # "barTall" effect (green bricks) if brickColor == self.bricksColors["g"]: self.effects["barTall"][0] = 1 self.effects["barTall"][1] = 240 # "shield" effect (blue bricks) elif brickColor == self.bricksColors["b"]: self.effects["shield"][0] = 1 # "ballFire" effect (purpil bricks) elif brickColor == self.bricksColors["p"]: self.effects["ballFire"][0] += 1 self.effects["ballFire"][1] = 240 # "ballTall" effect (turquoise bricks) elif brickColor == self.bricksColors["t"]: self.effects["ballTall"][0] = 1 self.effects["ballTall"][1] = 240 if not(self.effects["ballFire"][0]): if collision == 1 or collision == 3: self.ballAngle = math.radians(180) - self.ballAngle if collision == 2 or collision == 4: self.ballAngle = -self.ballAngle # If the brick is red, it becomes orange. if brickColor == self.bricksColors["r"]: self.itemconfig(self.bricks[i], fill=self.bricksColors["o"]) # If the brick is orange, it becomes yellow. elif brickColor == self.bricksColors["o"]: self.itemconfig(self.bricks[i], fill=self.bricksColors["y"]) # If the brick is yellow (or an other color except red/orange), it is destroyed. else: self.delete(self.bricks[i]) del self.bricks[i] i += 1 self.won = len(self.bricks) == 0 # Collisions computation between ball and edge of screen if ballNextCoords[0] < 0 or ballNextCoords[2] > self.screenWidth: self.ballAngle = math.radians(180) - self.ballAngle elif ballNextCoords[1] < 0: self.ballAngle = -self.ballAngle elif not(self.collision(self.ballNext, self.bar)): ballCenter = self.coords(self.ball)[0] + self.ballRadius barCenter = self.coords(self.bar)[0] + self.barWidth/2 angleX = ballCenter - barCenter angleOrigin = (-self.ballAngle) % (3.14159*2) angleComputed = math.radians(-70/(self.barWidth/2)*angleX + 90) self.ballAngle = (1 - (abs(angleX)/(self.barWidth/2))**0.25)*angleOrigin + ((abs(angleX)/(self.barWidth/2))**0.25)*angleComputed elif not(self.collision(self.ballNext, self.shield)): if self.effects["shield"][0]: self.ballAngle = -self.ballAngle self.effects["shield"][0] = 0 else : self.losed = True self.move(self.ball, self.ballSpeed*math.cos(self.ballAngle), -self.ballSpeed*math.sin(self.ballAngle)) self.coords(self.ballNext, tk._flatten(self.coords(self.ball)))
def set(self, **kw): self._tk.call( 'array', 'set', str(self), tkinter._flatten(list(kw.items())))