首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >平滑分类输出

平滑分类输出
EN

Stack Overflow用户
提问于 2020-09-19 00:57:40
回答 2查看 99关注 0票数 1

我有一个从奶牛行为检测模型获得的输出列表。即使在一段视频中,当一头牛下蛋时,它经常会识别为站立,反之亦然。在每个视频帧中,模型给出一个分类结果,我们将其附加到一个列表中。让我们假设在20帧之后,我们有一系列的输出如下-

代码语言:javascript
复制
behavious_cow_1 = ["stand","stand","stand","stand","lying", "stand","stand", "eating", "stand","stand","stand","stand","lying""stand","stand","stand","stand","stand","stand","lying"]

在20个分类结果中,我们有4个错误分类;3个谎言,1个进食。然而,这头牛一直坐在一个地方。如果列表只包含-1,2,3这样的数值,我会选择移动平均来改变错误分类。有没有Scipy,Pandas,Numpy函数可以平滑分类输出?我正在考虑取前3个和下3个值来确定当前类别。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-21 04:49:02

我使用了以下解决方案-

代码语言:javascript
复制
import scipy.stats
window_length = 7
behave = ["stand","stand","stand","stand","lying","lying", "eating"]
most_freq_val = lambda x: scipy.stats.mode(x)[0][0]
smoothed = [most_freq_val(behave[i:i+window_length]) for i in range(0,len(behave)-window_length+1)]

我尝试了Hugolmn发布的解决方案,但它在某一点上崩溃了。在滚动模式下,窗口宽度由用户提供(此处为7)。在一定的宽度内,如果在相同的次数内出现多个值,代码将无法工作。这更像是-你试图找到一个列表的统计模式(最常见的项目),但它得到了多个具有相同最高频率的项目。

票数 3
EN

Stack Overflow用户

发布于 2020-09-20 04:29:22

我自己非常惊讶,像mode()这样的函数不能在pandas中使用滚动窗口。然而,我还是找到了一个解决你问题的好办法。

首先,创建一个分类数据类型的pandas Series:

代码语言:javascript
复制
df = pd.Series(sample, dtype='category')

现在您可以看到,df.cat.categories返回数据中的类别列表,并df.cat.codes与它们关联的代码。我们可以使用后者来应用宽度为7的滚动模式(前3个,值,下一个3):

代码语言:javascript
复制
df.cat.codes
0     3
1     3
2     3
3     3
4     1
5     3
6     3
7     0
8     3
9     3
10    3
11    3
12    2
13    3
14    3
15    3
16    3
17    3
18    1
dtype: int8

df.cat.codes.rolling(7, center=True, min_periods=0).apply(lambda x: x.mode())
0     3.0
1     3.0
2     3.0
3     3.0
4     3.0
5     3.0
6     3.0
7     3.0
8     3.0
9     3.0
10    3.0
11    3.0
12    3.0
13    3.0
14    3.0
15    3.0
16    3.0
17    3.0
18    3.0
dtype: float64

最后,您可以映射代码以取回字符串:

代码语言:javascript
复制
(df.cat.codes
  .rolling(7, center=True, min_periods=0)
  .apply(lambda x: x.mode())
  .map(dict(enumerate(df.cat.categories)))
)
0     stand
1     stand
2     stand
3     stand
4     stand
5     stand
6     stand
7     stand
8     stand
9     stand
10    stand
11    stand
12    stand
13    stand
14    stand
15    stand
16    stand
17    stand
18    stand
dtype: object

这就对了!在对代码应用滚动模式后,您恢复了字符串!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63959891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档