範囲の有効性に基づいて生データを選択したいと思います。最も感度の高い設定が C、次に B、次に A である機器があります。したがって、C から始めて、すべての値がしきい値よりも小さいかどうかを確認し、はいの場合は完璧であり、この感度のすべてのデータを最高に設定します = 1.
from StringIO import StringIO
a = """category,val,sensitivity_level
x,20,A
x,31,B
x,60,C
x,20,A
x,25,B
x,60,C
y,20,A
y,40,B
y,60,C
y,20,A
y,24,B
y,30,C"""
df = pd.read_csv(StringIO(a))
def grp_1evel_1(x):
"""
return if all the elements are less than threshold
"""
return x<=30
def grp_1evel_0(x):
"""
Input: data grouped by category. Here I want to go through this categories, in an descending order,
that is C, B and then A, and wherever one of this categories has x<=30 valid for all elements select
that category as best category. Think about a device sensitivity, that at the highest sensitivity the
data maybe garbage, so you would like to move down the sensitivity and check again.
"""
x['islessthan30'] = x.groupby('sensitivity_level').transform(grp_1evel_1)
return x
print df.groupby('category').apply(grp_1evel_0)
しかし残念ながら、上記のコードはこの行列を生成しません。なぜなら - groupby を降順でソートできない - groupby の groupby に値を割り当てることができないからです
:
x,20,A,1
x,31,B,0
x,60,C,0
x,20,A,1
x,25,B,0
x,60,C,0
y,20,A,0
y,29,B,1
y,60,C,0
y,20,A,0
y,24,B,1
y,30,C,0
ヒントはありますか?
アルゴリズムは次のようになります
カテゴリ内で、最高の感度から開始し、すべての値がしきい値よりも小さい場合は、この感度を 1 に設定し、他の低い感度をスキップします。