9

Since I am creating a dataframe, I don't understand why I am getting an array error.

M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
M2.head(2)

AttributeError: 'numpy.ndarray' object has no attribute 'fillna'
1
  • 2
    What is M defined as? Commented Jun 3, 2016 at 23:52

3 Answers 3

6

(M - 3) is getting interpreted as a numpy.ndarray. This implies that M is defined somewhere as a numpy.ndarray. Test it out by running:

print type(M)
Sign up to request clarification or add additional context in comments.

Comments

5

You are calling the .fillna() method on a numpy array. And numpy arrays don't have that method defined.

You can probably convert the numpy array to a pandas.DataFrame and then apply the .fillna() method.

1 Comment

Yes. I used : y = [np.nan, np.nan, 2, 3, 4, 5, np.nan] y_temp = pd.DataFrame(y).fillna(1E-8)[0].values
4

Your code is not complete at the moment, so it is hard to pin point why M is causing an error. There could be a couple reasons:

  1. You have a typo and (M - 3) should be (M2 - 3)

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M2 - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  2. You need to define/convert M as pandas.DataFrame somewhere else in your code

    # With out seeing this part of the code, no one can really help you
    M = pd.DataFrame(...)
    # ...
    # ...
    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  3. You could convert it to a pandas.DataFrame right before you use it.

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (pd.DataFrame(M) - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.