Python TypeError: an integer is required
Posted
by kartiku
on Stack Overflow
See other posts from Stack Overflow
or by kartiku
Published on 2010-05-06T04:08:10Z
Indexed on
2010/05/06
4:18 UTC
Read the original article
Hit count: 465
import scipy,array
def try_read_file():
def line_reader(lines):
for l in lines:
i = l.find('#')
if i != -1: l = l[:i]
l = l.strip()
if l: yield l
def column_counter():
inputer = (line.split() for line in line_reader(file('/home/kartik/Downloads/yahoo_dataset/set1.train.txt'.strip())))
loopexit = 0
for line in inputer:
feature_tokens = (token.split(':') for token in line[6:])
feature_ids = array.array('I')
for t in feature_tokens:
feature_ids.append(int (t[0]))
tmpLength = feature_ids[-1]
print feature_ids
loopexit = loopexit + 1
if loopexit > 0:
break
return tmpLength
def line_counter():
inputer = (line.split() for line in line_reader(file('/home/kartik/Downloads/yahoo_dataset/set1.train.txt'.strip())))
noOfRows = 0
for line in inputer:
noOfRows = noOfRows + 1
return noOfRows
inputer = (line.split() for line in line_reader(file('/home/kartik/Downloads/yahoo_dataset/set1.train.txt'.strip())))
feature_id_list = []
feature_value_list = []
relevance_list = []
noOfRows = line_counter()
noOfCols = column_counter()
print noOfRows
print noOfCols # line 52
#Create the feature array
feature_array = scipy.zeros((noOfRows,noOfCols), float)
rowCounter = 1;
for line in inputer:
feature_tokens = (token.split(':') for token in line[6:])
feature_ids = array.array('I')
feature_values = array.array('f')
for t in feature_tokens:
feature_ids.append(int(t[0]))
if (t[0]!=colCounter):
feature_array[rowCounter,colCounter] = 0
else:
feature_array[rowCounter,colCounter] = t[1]
feature_values.append(float(t[1]))
colCounter = colCounter + 1;
label = float(line[0])
assert(line[1].startswith('qid:'))
query_id = int(line[1][4:])
feature_id_list.append(feature_ids)
feature_value_list.append(feature_values)
relevance_list.append(label)
rowCounter = rowCounter + 1;
return feature_array
Error:
Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
try_read_file()
File "/home/kartik/Python/prelim_read.py", line 52, in try_read_file
print noOfCols
TypeError: an integer is required
What is the problem, i couldn't figure it out?
I tried to debug it, but it doesnt really go inside those methods. It gives me an address in place of those variables.
© Stack Overflow or respective owner