在生成p之前,根据另一个数组对数组进行排序

2024-05-23 14:54:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做柱状图。 我有一对数组:

1)等级1,概率1 2) 类别2,概率2 每个数组有101个元素。类\ 1/类\ 2是字符串(x坐标),概率\ 1、概率\ 2是浮点。你知道吗

我的目的:

(classes_1, proba_1)(classes_2, proba_2)制作一个绘图条直方图

问题: 类2和类1都包含101个元素。它们具有相同的元素,但顺序不同,因此:

classes_1 == classes_2
False

我的问题:

1)如何将课程1和课程2排列成相同的顺序? 类1和类2中的新顺序应更新概率1和概率2中的顺序。你知道吗

类1和类2是列表<class 'list'> 概率1和概率2是<class 'numpy.ndarray'>

2)给定有序的类列表,我想用不同的颜色(比如说黑色)来选择类

我试过的:

    import numpy as np
    import  matplotlib.pyplot as plt
    classes_1 == classes_2
    classes_1 = sorted(classes_1)
    classes_2=sorted(classes_2)
    # Only classes that are sorted but not proba_1 and proba_2
    fig2, ax2 = plt.subplots()
    ax2.tick_params(rotation=90)
    ax2.bar(classes_1 +  classes_2, [0.1] * len(classes_1 +  classes_2), visible=False)
    # How can l specify the list of classes to be plotted in differet color ?
    ax2.bar(classes_1, proba_1, label=' classes 1')
    ax2.bar(classes_2, proba_2, label=' classes 2')
    ax2.set_xlabel("classes", rotation='vertical')
    ax2.set_ylabel("performance")
    ax2.legend(loc='best')

这是我的数据

第1类

['tennisswing', 'soccerjuggling', 'stillrings', 'drumming', 'rafting', 'wallpushups', 'cuttinginkitchen', 'writingonboard', 'salsaspin', 'diving', 'surfing', 'headmassage', 'mixing', 'playingguitar', 'skydiving', 'horseriding', 'trampolinejumping', 'swing', 'basketballdunk', 'moppingfloor', 'biking', 'hammerthrow', 'jugglingballs', 'longjump', 'boxingboxingspeedbag', 'rowing', 'playingtabla', 'throwdiscus', 'shotput', 'knitting', 'javelinthrow', 'baseballpitch', 'pommelhorse', 'playingdhol', 'playingdaf', 'pushups', 'nunchucks', 'bodyweightsquats', 'hammering', 'sumowrestling', 'walkingwithdog', 'skiing', 'haircut', 'unevenbars', 'playingsitar', 'playingviolin', 'applylipstick', 'cliffdiving', 'skateboarding', 'icedancing', 'pullups', 'bowling', 'babycrawling', 'shavingbeard', 'breaststroke', 'cricketbowling', 'highjump', 'horserace', 'floorgymnastics', 'jumpingjack', 'militaryparade', 'archery', 'playingflute', 'typing', 'benchpress', 'rockclimbingindoor', 'golfswing', 'fencing', 'soccerpenalty', 'applyeyemakeup', 'kayaking', 'skijet', 'taichi', 'pizzatossing', 'punch', 'lunges', 'handstandpushups', 'yoyo', 'playingcello', 'parallelbars', 'fieldhockeypenalty', 'frontcrawl', 'blowingcandles', 'bandmarching', 'playingpiano', 'hulahoop', 'jumprope', 'handstandwalking', 'brushingteeth', 'ropeclimbing', 'cricketshot', 'balancebeam', 'boxingspeedbag', 'polevault', 'cleanandjerk', 'billiards', 'basketball', 'frisbeecatch', 'blowdryhair', 'tabletennisshot', 'volleyballspiking']

概率1

array([ 0.85416667,  0.30769231,  0.7       ,  0.88372093,  0.87878788,
        0.33333333,  0.80645161,  0.62162162,  0.38461538,  0.95454545,
        0.97368421,  0.75      ,  0.97727273,  0.97826087,  1.        ,
        0.86363636,  0.82857143,  0.76470588,  1.        ,  0.64285714,
        0.63888889,  0.7173913 ,  0.38235294,  0.73809524,  0.56097561,
        0.70454545,  0.8125    ,  0.18918919,  0.30952381,  0.88888889,
        0.63636364,  0.69230769,  0.79411765,  0.83333333,  0.90243902,
        0.82142857,  0.275     ,  0.375     ,  0.65853659,  1.        ,
        0.85714286,  0.97297297,  0.67567568,  0.92857143,  0.97674419,
        0.85714286,  0.51724138,  0.92682927,  0.82857143,  1.        ,
        0.28571429,  0.95348837,  0.85714286,  0.23255814,  0.71428571,
        0.28205128,  0.67647059,  1.        ,  0.68571429,  0.22222222,
        0.77777778,  0.65853659,  0.83333333,  0.65217391,  0.88636364,
        0.87179487,  0.72727273,  0.60606061,  0.9       ,  0.56410256,
        0.86111111,  0.85714286,  0.35714286,  0.44827586,  0.83333333,
        0.48717949,  0.37837838,  0.54285714,  0.84444444,  0.72727273,
        0.86486486,  0.63157895,  0.63333333,  0.88888889,  0.89285714,
        0.33333333,  0.23076923,  0.21428571,  0.29411765,  0.15151515,
        0.36734694,  0.89655172,  0.65      ,  0.74358974,  0.81818182,
        1.        ,  0.39393939,  0.88235294,  0.6       ,  0.63636364,  1.        ])

类别2

['wallpushups', 'taichi', 'salsaspin', 'knitting', 'biking', 'throwdiscus', 'stillrings', 'skateboarding', 'playingtabla', 'bodyweightsquats', 'hammering', 'swing', 'playingflute', 'cricketbowling', 'billiards', 'playingviolin', 'cuttinginkitchen', 'cricketshot', 'ropeclimbing', 'unevenbars', 'applyeyemakeup', 'babycrawling', 'blowingcandles', 'writingonboard', 'playingdaf', 'pushups', 'polevault', 'surfing', 'baseballpitch', 'blowdryhair', 'highjump', 'jugglingballs', 'skydiving', 'playingdhol', 'rockclimbingindoor', 'diving', 'militaryparade', 'playingpiano', 'tabletennisshot', 'pommelhorse', 'archery', 'volleyballspiking', 'basketball', 'golfswing', 'typing', 'skiing', 'javelinthrow', 'parallelbars', 'breaststroke', 'trampolinejumping', 'fieldhockeypenalty', 'shavingbeard', 'sumowrestling', 'bowling', 'balancebeam', 'frontcrawl', 'rafting', 'boxingspeedbag', 'cleanandjerk', 'brushingteeth', 'punch', 'soccerjuggling', 'tennisswing', 'nunchucks', 'lunges', 'pullups', 'cliffdiving', 'floorgymnastics', 'basketballdunk', 'playingsitar', 'handstandwalking', 'yoyo', 'handstandpushups', 'soccerpenalty', 'fencing', 'icedancing', 'benchpress', 'skijet', 'rowing', 'longjump', 'mixing', 'applylipstick', 'moppingfloor', 'headmassage', 'horserace', 'drumming', 'walkingwithdog', 'horseriding', 'kayaking', 'shotput', 'hammerthrow', 'jumprope', 'playingguitar', 'hulahoop', 'bandmarching', 'pizzatossing', 'boxingboxingspeedbag', 'frisbeecatch', 'jumpingjack', 'haircut', 'playingcello']

概率2

array([ 0.25      ,  0.67857143,  0.76923077,  1.        ,  0.44444444,
        0.37837838,  0.7       ,  0.88571429,  0.71875   ,  0.03125   ,
        0.26829268,  0.76470588,  0.80952381,  0.33333333,  1.        ,
        0.89285714,  0.48387097,  0.3877551 ,  0.03030303,  0.92857143,
        0.41025641,  0.48571429,  0.7       ,  0.72972973,  0.75609756,
        0.82142857,  0.8974359 ,  0.94736842,  0.71794872,  0.425     ,
        0.82352941,  0.79411765,  1.        ,  0.52083333,  0.94871795,
        0.88636364,  0.77777778,  0.89285714,  0.60606061,  0.79411765,
        0.68292683,  0.89189189,  0.33333333,  0.81818182,  0.36956522,
        0.94594595,  0.6969697 ,  0.6969697 ,  0.64285714,  0.8       ,
        0.67567568,  0.30232558,  0.96969697,  0.97674419,  0.89655172,
        0.5       ,  0.78787879,  0.625     ,  0.87878788,  0.82352941,
        0.83333333,  0.07692308,  0.83333333,  0.075     ,  0.46153846,
        0.85714286,  0.97560976,  0.31428571,  1.        ,  0.48837209,
        0.07142857,  0.31428571,  0.40540541,  0.85      ,  0.60606061,
        1.        ,  1.        ,  0.39285714,  0.72727273,  0.80952381,
        0.93181818,  0.24137931,  0.75      ,  0.65      ,  0.87878788,
        0.76744186,  0.62857143,  0.86363636,  0.83333333,  0.42857143,
        0.65217391,  0.28205128,  1.        ,  0.69444444,  0.73333333,
        0.13793103,  0.04878049,  0.76470588,  0.11111111,  0.48648649,
        0.97777778])

我想用不同颜色绘制的类有:

['bowling', 'babycrawling', 'shavingbeard', 'breaststroke','lunges', 'handstandpushups', 'yoyo', 'drumming']

谢谢你的帮助


Tags: 元素顺序bar概率classessortedbowlingproba
2条回答

既然您希望proba_1[i]与类\u 1[i]匹配,那么确保它们保持在一起的一种方法就是压缩它们。所以你可以这样做:

classes_1, proba_1 = zip(*sorted(zip(classes_1, proba_1)))
classes_2 proba_2 = zip(*sorted(zip(classes_2, proba_2)))

您将以元组结束,但是这对于您想要的应该很好。你知道吗


解释:

内部zip确保匹配元素保持在一起。我们对元组列表进行排序。*将它们解压到zip函数中,将元组分回到单独的列表中(尽管它们最终是一个长元组,如果需要,您可以轻松地将其转换为列表)

因为它将基于classes_1的元素执行排序,并且classes_1classes_2具有相同的元素,所以最终将以相同的方式对列表进行排序

我不记得必须按照另一个列表的顺序对列表进行排序,但对于您的情况,我会这样做:

data2 = list(zip(classes_2, proba_2))
data2.sort(key=lambda x: classes_1.index(x[0]))
classes_2, proba_2 = zip(*data2)

现在classes_1classes_2遵循相同的顺序。你知道吗

编辑:请注意,这种方法的复杂性是二次的。因此,对于更大的数组或更严格的时间限制,您可以通过在dict中为classes_1建立索引来加快查找速度

相关问题 更多 >