将提升,支持和置信度四舍五入为小数点后四位,但提升为小数点后一位

问题描述 投票:0回答:1

我使用Apyori API从我的数据集生成关联。

#categorize central region into a basket
basket = (df
         .groupby(['Customer Name', 'Sub-Category'])['Quantity']
         .sum().unstack().reset_index().fillna(0)
         .set_index('Customer Name'))

#create a function to normalize the data
#any >1 values will return 1, any <1 values will return 0
def encode_units(x):
    if x <= 0:
        return 0
    if x>=1:
        return 1

#apply the function to basket
basket_sets = basket.applymap(encode_units)
basket_sets
#find frequent itemset, which minimum support is at least 0.35
frequent_itemsets = apriori(basket_sets, min_support=0.35, use_colnames=True)

#generate rules with corresponding support, lift and confidence
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)

a = rules[(rules['lift'] >= 1) & (rules['confidence'] >= 0.8)]
t = a.sort_values(['lift', 'confidence'], ascending=False)[['antecedents', 'consequents', 'lift', 'support', 'confidence']]

然后我得到了结果:

    antecedents           consequents   lift    support confidence
74  (Accessories, Storage)  (Paper) 1.114737    0.353090    0.858896
166 (Storage, Furnishings)  (Paper) 1.106141    0.378310    0.852273
170 (Phones, Storage)   (Paper) 1.103379    0.372005    0.850144
146 (Binders, Storage)  (Paper) 1.094049    0.460277    0.842956
etc...

我应该如何将提升力,支撑力和置信度四舍五入到小数点后四位,并且还要保留前项的索引然后显示?谢谢

=======================更新=====================>

我使用t.index = np.arange(1,len(t)+1)替换了索引,但是四舍五入后,我仍在寻找解决方案。

======================= updates2 ===================== >>

好的,我已经使用]找到了答案>

t.lift = t.lift.apply(np.round, decimals=4) t.support = t.support.apply(np.round, decimals=4) t.confidence = t.confidence.apply(np.round, decimals=4)

我使用Apyori API从我的数据集生成关联。 #将中央区域分类到购物篮=(df .groupby(['Customer Name','Sub-Category'])['Quantity'] .sum()....

确定我自己的问题,我实际上是使用]找到了解决方案>

t.lift = t.lift.apply(np.round, decimals=4) t.support = t.support.apply(np.round, decimals=4) t.confidence = t.confidence.apply(np.round, decimals=4

python pandas numpy
1个回答
0
投票

确定我自己的问题,我实际上是使用]找到了解决方案>

t.lift = t.lift.apply(np.round, decimals=4) t.support = t.support.apply(np.round, decimals=4) t.confidence = t.confidence.apply(np.round, decimals=4

© www.soinside.com 2019 - 2024. All rights reserved.