当前位置:   article > 正文

滑动窗口和概率统计窗口_滑动窗口概率

滑动窗口概率

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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/544059
推荐阅读
相关标签
  

闽ICP备14008679号