1

I am working on a memory-based collaborative filtering algorithm. I am building a matrix that I want to write into CSV file that contains three columns: users, app and ratings.

fid = fopen('pred_ratings.csv','wt');

for i=1:user_num
  for j=1:item_num
    if R(j,i) == 1
      entry = Y(j,i);
    else
      entry = round(P(j,i));
    end
    fprintf(fid,'%d %d %d\n',i,j,entry);
  end
end
fclose(fid);

The above code is a MATLAB implementation of writing a multidimensional matrix into a file having 3 columns. I tried to imitate this in python, using:

n_users=816
n_items=17
f = open("guru.txt","w+")
for i in range(1,n_users):
  for j in range(1,n_items):
      if (i,j)==1 in a:
          entry = data_matrix(j, i)
       else:
          entry = round(user_prediction(j, i))
     print(f, '%d%d%d\n', i, j, entry)
f.close

But this results in the following error:

File "<ipython-input-198-7a444566e1ce>", line 7, in <module>
entry = round(user_prediction(j, i))
TypeError: 'numpy.ndarray' object is not callable

What can be done to fix this?

Dev-iL
  • 22,722
  • 7
  • 53
  • 89

1 Answers1

0

numpy uses square brackets for indexing. Since user_predictions is a numpy array, it should be indexed as

user_predictions[i, j]

The same goes for data_matrix.

You should probably read the Numpy for MATLAB users guide.

Edit: Also, the

if (i,j)==1 in a:

line is very dubious. (i, j) is a tuple of two integers, which means it will never be equal to 1. That line is thus equivalent to if False in a: which is probably not what you want.

Hannes Ovrén
  • 18,969
  • 8
  • 64
  • 72