I have a movie dataframe with movie names, their respective genre, and vector representation (numpy arrays).
ID Year Title Genre Word Vector
1 2003.0 Dinosaur Planet Documentary [-0.55423898, -0.72544044, 0.33189204, -0.1720...
2 2004.0 Isle of Man TT 2004 Review Sports & Fitness [-0.373265237, -1.07549703, -0.469254494, -0.4...
3 1997.0 Character Foreign [-1.57682264, -0.91265768, 2.43038678, -0.2114...
4 1994.0 Paula Abdul's Get Up & Dance Sports & Fitness [0.3096168, -0.57186663, 0.39008939, 0.2868615...
5 2004.0 The Rise and Fall of ECW Sports & Fitness [0.17175879, -2.38005066, -0.45771399, 1.32608...
I'd like to group by genre and get each genre's average vector representation (the component wise average of each movie vector in the genre).
I first tried:
movie_df.groupby(['Genre']).mean()
But the built in mean function isn't able to take the mean of numpy arrays.
I tried creating my own function to do so and then apply it to each group, but I'm not sure this is using apply correctly:
def vector_average(group):
series_to_array = np.array(group.tolist())
return np.mean(series_to_array, axis = 0)
movie_df.groupby(['Genre']).apply(vector_average)
Any pointers would be appreciated!
df.head(5)
and paste it here? – SpasticWord Vector
is a column of numpy arrays or lists? – Spastic