【问题标题】:TypeError for '-''-' 的类型错误
【发布时间】:2018-04-30 16:18:28
【问题描述】:

我收到此错误

File "C:\Users\Morakinyo\.vscode\extensions\ms-python.python-2018.3.1\pythonFiles\experimental\ptvsd\ptvsd\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)

文件“c:\Users\Morakinyo\Documents\recommend\Movie-Recommender-master\movie-reco.py”,第 26 行,在 train_data_matrix[line[0]-1, line[1]-1] = line[2] TypeError: 不支持的操作数类型 -: 'str' 和 'int'

执行此代码时:

import numpy as np
import pandas as pd
header = ['user_id', 'item_id', 'rating', 'timestamp']
df = pd.read_csv('/Users/Morakinyo/Documents/recommend/Movie-Recommender-master/u.data', sep='\t', names=header)
n_users = df.user_id.unique().shape[0]
n_items = df.item_id.unique().shape[0]
print ('Number of users = ' + str(n_users) + ' | Number of movies = ' + str(n_items)  )
from sklearn import model_selection as cv
train_data, test_data = cv.train_test_split(df, test_size=0.25)
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]
test_data_matrix = np.zeros((n_users, n_items))
for line in test_data:
test_data_matrix[line[0]-1, line[1]-1] = line[2]
from sklearn.metrics.pairwise import pairwise_distances
user_similarity = pairwise_distances(train_data_matrix, metric='cosine')
def predict(ratings, similarity, type='user'):
if type == 'user':
mean_user_rating = ratings.mean(axis=1)
ratings_diff = (ratings - mean_user_rating[:, np.newaxis]) 
pred = mean_user_rating[:, np.newaxis] + similarity.dot(ratings_diff) / np.array([np.abs(similarity).sum(axis=1)]).T   
return pred
user_prediction = predict(train_data_matrix, user_similarity, type='user')
from sklearn.metrics import mean_squared_error
from math import sqrt
def rmse(prediction, ground_truth):
prediction = prediction[ground_truth.nonzero()].flatten() 
ground_truth = ground_truth[ground_truth.nonzero()].flatten()
return sqrt(mean_squared_error(prediction, ground_truth))
print ('User-based CF RMSE: ' + str(rmse(user_prediction, test_data_matrix)))

我不知道问题出在哪里。

【问题讨论】:

  • 错误告诉你断线。是哪条线?
  • 第 25 行,exec(compile(contents+"\n", file, 'exec'), glob, loc)

标签: python


【解决方案1】:

你正试图从一个字符串中减去一个整数。错误消息应包括发生错误的实际行。 如果字符串确实是整数,则可以使用 int("2")

进行转换

始终尝试在问题中包含完整的错误消息。这样更容易直接指出错误。

编辑:因为您提供了完整的输出,所以这是失败的行:

train_data_matrix[line[0]-1, line[1]-1] = line[2]

如果行实际上包含您可以使用的数字:

train_data_matrix[int(line[0])-1, int(line[1])-1] = line[2]

另一种选择是在使用之前转换训练数据中的所有条目。

【讨论】:

  • 在添加后会带来这个错误 file.py", line 25, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "c: \Users\Morakinyo\Documents\recommend\Movie-Recommender-master\movie-reco.py",第 26 行,在 train_data_matrix[int(line[0])-1, int(line[1])-1 ] = line[2] ValueError: int() 基数为 10 的无效文字:'u'
  • 你确定 line[0] 包含一个数字(作为字符串)吗?这也可能是编码的问题。尝试打印出 line 的内容以确保您正确读取文件。
  • 我应该打印哪一行或者我应该使用什么打印命令?
  • 只需使用“print(line[0])”
  • 添加打印行后,它会显示此 Traceback(最近一次调用最后一次):文件“C:\Users\Morakinyo\Documents\recommend\Movie-Recommender-master\movie-reco.py”,第 26 行,在 train_data_matrix[int(line[0])-1, int(line[1])-1] = line[2] ValueError: int() 以 10 为底的无效文字:'u'
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多