TypeError python:不支持的操作数

时间:2017-08-24 11:18:34

标签: python

from sklearn import cross_validation as cv
train_data, test_data = cv.train_test_split(df, test_size=0.25)

#Create two user-item matrices, one for training and another for testing
train_data_matrix = np.zeros((n_users, n_items))
for line in train_data:
                train_data_matrix[line[0]-1, line[1]-1] = line[2]

我收到错误:

File ".....", line 26, in <module>
    train_data_matrix[line[0]-1, line[1]-1] = line[2]
TypeError: unsupported operand type(s) for -: 'str' and 'int'

有谁知道为什么?

1 个答案:

答案 0 :(得分:1)

这是因为您的dftrain_data_matrix的子集,其中包含字符列。你不能从一个字符中减去一个数字。您可能希望在循环中使用train_data。在for循环中,您使用的是train_data_matrix而不是index_booking