赞
踩
def slide(data,N,start,end,m,n): ''' 滑动窗口 data 传入数据矩阵 N 传入滑动长度 start 传入矩阵长度list end 传入数值位置 m 矩阵长度 n 矩阵最大列数 ''' return np.array([data[i,j] for i,j in zip(start,end-N) if j > N and j < n ]) def prob_slide_simple(word,original,dim=1,direction='left'): ''' word 传入单词 original 传入原文矩阵 dim 选择滑动次数 direction 选择滑动方向(左/右) ''' m,n = original.shape word_address = np.argwhere(original==word) start_temp = word_address[:,0] end_temp = word_address[:,-1] filter_mat = original[end_temp > 0] filter_cond = np.argwhere(filter_mat == word) start = filter_cond[:,0] end = filter_cond[:,-1] counter_dict = dict({}) for i in range(1,dim+1): if direction == "right": idim = i*-1 else: idim = i print(idim) data = slide(filter_mat,idim,start,end,m,n) distion_data = np.unique(data) counter = {d:(data == d).dot(np.ones(data.shape[0]))/m for d in distion_data} if len(counter)>0: counter_dict.update({idim:counter}) return counter_dict prob_slide_simple('studying',example,dim=2,direction='right')
def prob_slide(word,original,dim=1,direction='left'): ''' word 传入单词 original 传入原文矩阵 dim 选择滑动次数 direction 选择滑动方向(左/右) ''' m,n = original.shape delete_rules = (original==word).any(axis=1) filter_mat = original[delete_rules > 0] filter_cond = np.argwhere(filter_mat == word) start = filter_cond[:,0] end = filter_cond[:,-1] counter_dict = dict({}) for i in range(1,dim+1): if direction == "right": idim = i*-1 else: idim = i data = slide(filter_mat,idim,start,end,m,n) distion_data = np.unique(data) counter = {d:(data == d).dot(np.ones(data.shape[0]))/m for d in distion_data} if len(counter)>0: counter_dict.update({idim:counter}) return counter_dict prob_slide('is',example,dim=2,direction='right')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。