jeudi 22 février 2018

np.random choice weighted dropout over time?

I have a df where I want to show the number of people randomly added to a sports team a year where the inverse of rank is used as a normalizing weight. The code has 10 individuals to assign to a team each year. I have code for this that works fine. My problem is how can I drop teams and their weights out over time once they hit their max number of team members assigned, without dropping them from the df? I know this would require readjusting the weights and choices each year something dropped out but I'm not sure how to implement that.

      df_input

     Rank1 Team  max_num   current_num  
       1    Foo    15          6       
       2    Bar    16          5       
       3    Baz    14          9        
       3    Cat    18          10       



   df_output

     Rank1 Rank2  Team  max_num   current_num  Y1 Y2  Y3   Final_total
       1    3      Foo    15          6        4   5   4         19
       2    2      Bar    16          5        4   3   2         14
       3    1      Baz    14          9        1   2   2         14
       3    1      Cat    18          10       1   0   2         13

Current code :

      def Team_picker(df):
            new_weight = {1:3,3:1}
            df['Rank2'] = df.Rank.map(new_weight)
            All_Teams = df['Team'].tolist()
            weights = df['Rank2'].tolist()
            norm = [float(i)/sum(weights) for i in weights]

            for x in range(1,4):
                 year = 'Y %d'% x
                 T = [choice(All_Teams, p=norm) for _ in range(10)]
                 Player_counts = Counter(T)
                 df[year] = df['Team'].map(Player_counts)
            T_Years = [col for col in df.columns if col.startswith('Y')]
            df['Final_Total']= df['current_num'] + (df[T_Years].sum(axis=1))

            return df

However, the df I'm aiming for would look more like this:

      df_output2

  Rank1 Rank2  Team  max_num   current_num  Y1 Y2  Y3   Final_total
   1    3      Foo    15          6         4   5   0         15
   2    2      Bar    16          5         4   3   4         16
   3    1      Baz    14          9         1   2   2         14
   3    1      Cat    18          10        1   0   4         15 

TL;DR How can keep all of the teams in the random draw until they reach the max and then drop them out without dropping them from the DF while also changing the weights for the next draw year. Also can I implement this during a draw year if say something ranked really high only needs one more person to hit their max?




Aucun commentaire:

Enregistrer un commentaire