How to read an array of arrays and save each array[ ] into a different variable to run function with barplot?

Asked

Viewed 89 times

0

How can I read this array of arrays:

[45 32 56 57 44 21 36 35 39 23 24 27 25 26 31 28 29 30 20 22 18  8 19  1
 33  2  3  4  0  6  7  9 17 10 11 12 49 55 13 14 15 16 52 46 43 34 41 37
 40  5 54 51 53 47 38 42 50 48] [50 34 46 45 48 24 27 28 26 43 32 19 10 25 18 12 20 13  7 15 17  9  8 14
 22 21  4  3  5 16  1  6  0 11  2 29 47 52 55 57 54 56 38 35 31 23 37 53
 33 42 36 49 51 44 39 41 30 40] [54 29 40 42 53 23 31 34 37 39 38 17 19 22 12 10 11  3  6 13 15  8 14 16
 25 27  4  5 21  0  1 18  9  7  2 32 28 46 48 57 56 55 20 44 33 26 41 52
 24 45 43 50 51 49 35 30 47 36] [57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34
 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
  9  8  7  6  5  4  3  2  1  0]

Save each array in a different variable: ranks1, ranks2, ranks3, ranks4, ranks5 and be able to perform these functions:

One that’s in the main:

frk.plot_ranks(ranks1, ranks2, ranks3, ranks4, ranks5, ... )

And this other one that works to plot the graphics in another script:

def plot_ranks(bars1, bars2, bars3, bars4, bars5, features_train, title, RESULT_PATH='Results'):

    df = pd.DataFrame({'LA': bars1, 'DT': bars2, 'RF': bars3, 'PCA': bars4, 'ARD': bars5})
    index = X
    df.index = index
    ax = df.plot.barh(stacked=True)

    plt.show()

What happens is that if you don’t have one of these variables, say bars5 is not in the array the function plot_ranks() does not execute and gives error. How can I separate this array of arrays in these variables: ranks1, ranks2, ranks3, ranks4, ranks5 (of the function that is in main) and even in the absence of one of them these functions can be executed so that the graph is plotted and does not give error?

1 answer

1


Assuming that your array Two-dimensional input is something like:

entrada = [
    [45,32,56,57,44,21,36,35,39,23,24,27,25,26,31,28,29,30,20,22,18,8,19,1,33,2,3,4,0,6,7,9,17,10,11,12,49,55,13,14,15,16,52,46,43,34,41,37,40,5,54,51,53,47,38,42,50,48],
    [50,34,46,45,48,24,27,28,26,43,32,19,10,25,18,12,20,13,7,15,17,9,8,14,22,21,4,3,5,16,1,6,0,11,2,29,47,52,55,57,54,56,38,35,31,23,37,53,33,42,36,49,51,44,39,41,30,40],
    [54,29,40,42,53,23,31,34,37,39,38,17,19,22,12,10,11,3,6,13,15,8,14,16,25,27,4,5,21,0,1,18,9,7,2,32,28,46,48,57,56,55,20,44,33,26,41,52,24,45,43,50,51,49,35,30,47,36],
    [57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0]
]

You can convert it to a dictionary by creating a key for each element of the list.

data = {
    'rank1': entrada[0],
    'rank2': entrada[1],
    'rank3': entrada[2],
    'rank4': entrada[3],
}

Or better yet:

data = {f'rank{i}' : e for i, e in enumerate(entrada,1)}

Once the dictionary is created, you will be able to create a DataFrame from him:

df = pd.DataFrame(data)

Putting it all together:

import pandas as pd
import matplotlib.pyplot as plot

entrada = [
    [45,32,56,57,44,21,36,35,39,23,24,27,25,26,31,28,29,30,20,22,18,8,19,1,33,2,3,4,0,6,7,9,17,10,11,12,49,55,13,14,15,16,52,46,43,34,41,37,40,5,54,51,53,47,38,42,50,48],
    [50,34,46,45,48,24,27,28,26,43,32,19,10,25,18,12,20,13,7,15,17,9,8,14,22,21,4,3,5,16,1,6,0,11,2,29,47,52,55,57,54,56,38,35,31,23,37,53,33,42,36,49,51,44,39,41,30,40],
    [54,29,40,42,53,23,31,34,37,39,38,17,19,22,12,10,11,3,6,13,15,8,14,16,25,27,4,5,21,0,1,18,9,7,2,32,28,46,48,57,56,55,20,44,33,26,41,52,24,45,43,50,51,49,35,30,47,36],
    [57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0]
]

data = {f'rank{i}' : e for i, e in enumerate(entrada,1)}

df = pd.DataFrame(data);
df.plot.bar(stacked=True);
plot.show(block=True);

Exit:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.