dimanche 7 octobre 2018

Object to deep for desired array, numpy random choice

This is the code that I've been using to generate a random weighted choice from a baseball game simulator that I've been working on as my first python project.

elements = ['1b', '2b', '3b', 'hr', 'bb', 'k', 'out']
        if order_pos_away == 1:
            weights = ab1
        if order_pos_away == 2:
            weights = ab2
        if order_pos_away == 3:
            weights = ab3
        if order_pos_away == 4:
            weights = ab4
        if order_pos_away == 5:
            weights = ab5
        if order_pos_away == 6:
            weights = ab6
        if order_pos_away == 7:
            weights = ab7
        if order_pos_away == 8:
            weights = ab8
        if order_pos_away == 9:
            weights = ab9
        from numpy.random import choice
        c = choice(elements, p=weights)

Each time the loop runs through order_pos_away is increased by 1 so it will take the next batter's list of probabilities that add up to exactly 1. When the list probabilities are entered manually I have no issues. However, when I try to import them from the excel sheet where they are stored I get the error:

ValueError: object too deep for desired array

I've tried multiple ways of getting the excel data into python (read_csv, xlrd, numpy, pandas). Regardless of what I try, I always end up with the same error. I've found no solution for this as much as I've searched and have had difficulty even finding a good explanation of what is happening when I get this error.




Aucun commentaire:

Enregistrer un commentaire