0

I want to calculate the mean ratings for each user using his specified ratings.

I built a ratings matrix from my ratings dataframe as follow:

ratings_pivot = filter_ratings.pivot('User_id','item_id','Rating').fillna(0).astype(int).values

But then didn't know how to consider only nonzero values when calculating. The method mean() provided by numpy, if used on axis = 1 it will consider all elements values even those equal to zero.

How to do it?

SarahData
  • 769
  • 1
  • 12
  • 38
  • 2
    Something like this - https://stackoverflow.com/questions/38542548/? – Divakar Nov 08 '17 at 12:32
  • Where's your data? – cs95 Nov 08 '17 at 12:39
  • 1
    @Divakar yes! thanks didn't find it before writing the question. – SarahData Nov 08 '17 at 12:44
  • @cᴏʟᴅsᴘᴇᴇᴅ I didn't put it because it's not needed for the question. I wanted to find a way to consider only nonzero values when calculating mean over each row of the matrix. – SarahData Nov 08 '17 at 12:46
  • In that case, I think your question can be closed. If you'd provided data and expected output, we could've offered you alternative pandas solutions. – cs95 Nov 08 '17 at 12:48
  • Computing the nonzero mean of `.fillna(0).astype(int)` is a pretty roundabout way of getting the result of [`np.nanmean`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.nanmean.html). – MB-F Nov 08 '17 at 14:37

0 Answers0