Skip to content
Advertisement

Apply a softmax function on groupby in the same pandas dataframe

I have been looking to apply the following softmax function from https://machinelearningmastery.com/softmax-activation-function-with-python/

JavaScript

I am trying to apply this to a dataframe which is split by groups, and return the probabilites row by row for a group. My dataframe is:

JavaScript

In this instance there are two different events (10 and 12) where for event 10 the values are data = [30,32] and event 12 data = [2.5,3,4]

My expected result would be a new column probabilities with the results:

JavaScript

Any help on how to do this on all groups in the dataframe would be much appreciated! Thanks!

Advertisement

Answer

You can use groupby followed by transform which returns results indexed by the original dataframe. A simple way to do it would be

JavaScript

The result is

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement