Collaborative Filtering Code---loadmovielens.py file

Source: Internet
Author: User

#Coding=utf-8ImportSYSImportOS##==================================#load the specified training set file#parameter filename for a training set file##==================================defLoadmovielenstrain (filename='U1.base'): str1='./movielens/'                         #relative address of the directoryprefer= {}     forLineinchOpen (Str1+filename,'R'):#Open the specified file(UserID, MovieID, rating,ts) = Line.split ('\ t')#4 items per row in the data setPrefer.setdefault (userid, {})#set the default format of the dictionary, the element is user:{} dictionaryPrefer[userid][movieid] =Float (rating)returnPrefer#format such as {: ' user1 ' {itemid:rating, itemid2:rating,,,}, {,,,}}##==================================#load the corresponding test set file#the parameter, filename, represents a test set file, such as U1.test##==================================defLoadmovielenstest (filename='u1.test'): str1='./movielens/'prefer= {}     forLineinchOpen (Str1+filename,'R'): (UserID, MovieID, Rating,ts)= Line.split ('\ t')#4 items per row in the data setPrefer.setdefault (userid, {}) Prefer[userid][movieid]=Float (rating)returnpreferif __name__=="__main__":    Print("""This section can perform the above 2 function tests""") Traindict=Loadmovielenstrain () testdict=loadmovielenstest ()Print(Len (traindict))Print(Len (testdict))Print("""Test Pass""")

Collaborative Filtering Code---loadmovielens.py file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.