Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

There is an numpy calculation process as below, which has been approved correctly, but I don't understand the process clearly and would like to ask for help. The steps as following:

  1. There is user_item_train matrix as below which created through unstack: enter image description here
  2. There is a test data index list as test_idx as below: enter image description here
  3. Below code: look for the user_id which are in test_idx from train dataset, and return boolen values enter image description here
  4. There is u_train dataset as below which generated through np.linalg.svd(user_item_train,full_matrices=False): enter image description here
  5. I don't understand this step: enter image description here

May I ask for your help to explain it a bit?
Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

u_train is a 2-dimensional array. In NumPy, you can use lists as slices. It's easiest to imagine with an example: Assume I have a 4x4 array named arr. Then,

  • arr[3, 1] gives me the value in row 3, column 1 (remember that we count from 0).
  • arr[[1, 3], [0, 2]] gives me a 2x1 array with the following original indices: that is, row 1 and 3, column 0 and 2.
+--------+--------+
| (1, 0) | (1, 2) |
+--------+--------+
| (3, 0) | (3, 2) |
+--------+--------+
  • arr[[0, 3], :] gives me the row 0 and 3, all columns, since : implies everything in that axis:
+--------+--------+--------+--------+
| (0, 0) | (0, 1) | (0, 2) | (0, 3) |
+--------+--------+--------+--------+
| (3, 0) | (3, 1) | (3, 2) | (3, 3) |
+--------+--------+--------+--------+
  • arr[3] is identical to arr[3, :], this is just syntactic sugar.
  • For using a boolean list as indices, a True in the list means include the row of the same index as it, and vice versa. For example, arr[:, [True, True, False, False]] is equivalent to arr[:, [0, 1]]. Note that, perhaps obviously, the length of the boolean list must be the same as the dimension, so arr[:, [True, False, True]] will raise an IndexError.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...