T3J45
T3J45

Reputation: 761

Need to get the predicted values into a csv or Sframe or Dataframe in Python

Prerequisites

  1. Dataset I'm working with is MovieLens 100k
  2. Python Packages I'm using is Surprise, Io and Pandas
  3. Agenda is to test the recommendation system using KNN (+ K-Fold) on Algorithms: Vector cosine & Pearson, for both User based CF & Item based CF

Briefing

So far, I have coded for both UBCF & IBCF as below

Q1. IBCF Generates data as per input given to it, I need it to export a csv file since I need to find out the predicted values

Q2. UBCF needs to enter each data separately and doesn't work even with immediate below code:

csvfile = 'pred_matrix.csv'
    with open(csvfile, "w") as output:
        writer = csv.writer(output,lineterminator='\n')
#algo.predict(user_id, item_id, estimated_ratings)
        for val in algo.predict(str(range(1,943)),range(1,1683),1):
            writer.writerow([val])

Clearly it throws the error of lists, as it cannot be Comma separated.

Q3 Getting Precision & Recall on Evaluated and Recommended values

CODE

STARTS WITH

if ip == 1:
    one = 'cosine'
else:
    one = 'pearson'

choice = raw_input("Filtering Method: \n1.User based \n2.Item based \n Choice:")

if choice == '1':
    user_based_cf(one)
elif choice == '2':
    item_based_cf(one)
else:
    sim_op={}
    exit(0)

UBCF:

def user_based_cf(co_pe):
    # INITIALIZE REQUIRED PARAMETERS
    path = '/home/mister-t/Projects/PycharmProjects/RecommendationSys/ml-100k/u.user'
    prnt = "USER"
    sim_op = {'name': co_pe, 'user_based': True}
    algo = KNNBasic(sim_options=sim_op)

    # RESPONSIBLE TO EXECUTE DATA SPLITS Mentioned in STEP 4
    perf = evaluate(algo, df, measures=['RMSE', 'MAE'])
    print_perf(perf)
    print type(perf)

    # START TRAINING
    trainset = df.build_full_trainset()

    # APPLYING ALGORITHM KNN Basic
    res = algo.train(trainset)
    print "\t\t >>>TRAINED SET<<<<\n\n", res

    # PEEKING PREDICTED VALUES
    search_key = raw_input("Enter User ID:")
    item_id = raw_input("Enter Item ID:")
    actual_rating = input("Enter actual Rating:")

    print algo.predict(str(search_key), item_id, actual_rating)

IBCF

def item_based_cf(co_pe):
    # INITIALIZE REQUIRED PARAMETERS
    path = '/location/ml-100k/u.item'
    prnt = "ITEM"
    sim_op = {'name': co_pe, 'user_based': False}
    algo = KNNBasic(sim_options=sim_op)

 # RESPONSIBLE TO EXECUTE DATA SPLITS = 2
 perf = evaluate(algo, df, measures=['RMSE', 'MAE'])
 print_perf(perf)
 print type(perf)

 # START TRAINING
 trainset = df.build_full_trainset()

 # APPLYING ALGORITHM KNN Basic
 res = algo.train(trainset)
 print "\t\t >>>TRAINED SET<<<<\n\n", res

 # Read the mappings raw id <-> movie name
 rid_to_name, name_to_rid = read_item_names(path)

 search_key = raw_input("ID:")
 print "ALGORITHM USED : ", one
 toy_story_raw_id = name_to_rid[search_key]
 toy_story_inner_id = algo.trainset.to_inner_iid(toy_story_raw_id)

 # Retrieve inner ids of the nearest neighbors of Toy Story.
 k=5
 toy_story_neighbors = algo.get_neighbors(toy_story_inner_id, k=k)

 # Convert inner ids of the neighbors into names.
 toy_story_neighbors = (algo.trainset.to_raw_iid(inner_id)
                    for inner_id in toy_story_neighbors)
 toy_story_neighbors = (rid_to_name[rid]
                    for rid in toy_story_neighbors)
 print 'The ', k,' nearest neighbors of ', search_key,' are:'
 for movie in toy_story_neighbors:
     print(movie) 

Upvotes: 0

Views: 469

Answers (1)

Aditya
Aditya

Reputation: 2520

Q1. IBCF Generates data as per input given to it, I need it to export a csv file since I need to find out the predicted values

the easiest way to dump anything to a csv would be to use the csv module!

import csv

res = [x, y, z, ....]
csvfile = "<path to output csv or txt>"

#Assuming res is a flat list
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in res:
        writer.writerow([val])    

#Assuming res is a list of lists
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerows(res)

Upvotes: 1

Related Questions