TypeError: list indices must be integers, not tuple in Python SVD model -


i testing recommender based on svd model. got error message after running below:

here testing code:

import sys  sys import argv import csv import recsys.algorithm recsys.algorithm.verbose = true recsys.algorithm.factorize import svd recsys.datamodel.data import data  likes = [] open('/users/xps13mynotebook/desktop/w2v/likes.tsv', 'r') f:     line in f.readlines():         username,user_likes = line.strip().split('\t')         likes.append((username,user_likes)) data = data() value = 1.0 username in likes:     user_likes in likes[username]:         data.add_tuple((value, username, user_likes)) # tuple format is: <value, row, column>  svd = svd() svd.set_data(data) k = 5  svd.compute(k=k, min_values=3, pre_normalize=none, mean_center=false, post_normalize=true)  svd.similar('sheila') 

error:

typeerror                                 traceback (most recent call last) <ipython-input-30-913000ff4e0e> in <module>()      10 value = 1.0      11 username in likes: ---> 12     user_likes in likes[username]:      13         data.add_tuple((value, username, user_likes)) # tuple format is: <value, row, column>      14   typeerror: list indices must integers, not tuple  

when you're iterating on list of tuples, each value tuple itself. code suggests that's it's first element of tuple (or index, i'm not quite sure - what's clear it's blatantly wrong).

for username in likes:     # username tuple list     user_likes in likes[username]:  # list[tuple_stored_in_list] invalid , causes typeerror         pass # 

when iterating on list of tuples, use tuple unpacking achieve both element stored in tuple @ once:

for username, user_likes in likes:     data.add_tuple((value, username, user_likes)) # tuple format is: <value, row, column> 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -