python列表偏移量为什么可以有-1_合并值为1加上偏移量(掩码)的python列表元素...

The goal is to find a generic method to solve the following task:

I have two python lists of the same length filled with zeros and ones:

detection = [0,0,1,0] # only examples, they can be of any length

ground_truth = [0,1,0,0] # and the ones can be at any indizes

and a integer number

offset = 1 # this number is also variable

The goal is to combine #offset elements in detection around elements equal to 1

and then combine the same index elements of ground_truth logical or, resulting the new lists:

detection = [0,1]

ground_truth = [0,1]

graphical explanation:

Background Info: The detection / ground truth values belong to a binary classification of a time series and The idea is to have a flexible evaluation that results in a TP if the detection fits the ground_truth is within a certain range of time steps (=offset).

Additional Example:

offset = 1

detection = [1,0,0,0,1,1,0]

ground_truth = [0,0,0,1,0,0,0]

would result to:

detection = [1,0,1]

ground_truth = [0,0,1]

解决方案

I found the ultimate solution.

sub questions that solved it:

Code:

# Create Mask from Detection and Offset

w = offset*2 +1

mask = np.convolve(detection, np.ones(w), mode='same').clip(0,1).astype(int)

# Create Soft Detection

soft_detection = mask[~((np.diff(mask,prepend=False)==0) & mask==1)].tolist()

# Create Soft Ground Truth

idx = np.flatnonzero(np.r_[True,np.diff(mask)!=0])

soft_ground_truth = np.bitwise_or.reduceat(ground_truth, idx).tolist()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值