convert.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2018, deepakn94, codyaustun, robieta. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # -----------------------------------------------------------------------
  16. #
  17. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  18. #
  19. # Licensed under the Apache License, Version 2.0 (the "License");
  20. # you may not use this file except in compliance with the License.
  21. # You may obtain a copy of the License at
  22. #
  23. # http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. # Unless required by applicable law or agreed to in writing, software
  26. # distributed under the License is distributed on an "AS IS" BASIS,
  27. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. # See the License for the specific language governing permissions and
  29. # limitations under the License.
  30. from argparse import ArgumentParser
  31. import pandas as pd
  32. from load import implicit_load
  33. import tensorflow as tf
  34. MIN_RATINGS = 20
  35. USER_COLUMN = 'user_id'
  36. ITEM_COLUMN = 'item_id'
  37. def parse_args():
  38. parser = ArgumentParser()
  39. parser.add_argument('--path', type=str, default='/data/ml-20m/ratings.csv',
  40. help='Path to reviews CSV file from MovieLens')
  41. parser.add_argument('--output', type=str, default='/data',
  42. help='Output directory for train and test files')
  43. return parser.parse_args()
  44. def main():
  45. args = parse_args()
  46. print("Loading raw data from {}".format(args.path))
  47. df = implicit_load(args.path, sort=False)
  48. print("Filtering out users with less than {} ratings".format(MIN_RATINGS))
  49. grouped = df.groupby(USER_COLUMN)
  50. df = grouped.filter(lambda x: len(x) >= MIN_RATINGS)
  51. print("Mapping original user and item IDs to new sequential IDs")
  52. df[USER_COLUMN] = pd.factorize(df[USER_COLUMN])[0]
  53. df[ITEM_COLUMN] = pd.factorize(df[ITEM_COLUMN])[0]
  54. print("Creating list of items for each user")
  55. # Need to sort before popping to get last item
  56. df.sort_values(by='timestamp', inplace=True)
  57. # clean up data
  58. del df['rating'], df['timestamp']
  59. df = df.drop_duplicates() # assuming it keeps order
  60. # now we have filtered and sorted by time data, we can split test data out
  61. grouped_sorted = df.groupby(USER_COLUMN, group_keys=False)
  62. test_data = grouped_sorted.tail(1).sort_values(by='user_id')
  63. # need to pop for each group
  64. train_data = grouped_sorted.apply(lambda x: x.iloc[:-1])
  65. train_data = train_data.sort_values([USER_COLUMN, ITEM_COLUMN])
  66. train_data.to_pickle(args.output + '/train_ratings.pickle')
  67. test_data.to_pickle(args.output + '/test_ratings.pickle')
  68. if __name__ == '__main__':
  69. main()