-2
我正在通過使用Movielens數據集進行協作過濾製作Movie Recommender系統。 於是我跟着this:http://blog.ethanrosenthal.com/2015/11/02/intro-to-collaborative-filtering/在Python中,TypeError:不支持的操作數類型爲 - :'str'和'int'
,但它並不在LN工作[8]:
import numpy as np
import pandas as pd
data = open('ratings.csv')
names = ['user_id','item_id','rating','timestamp']
df = pd.read_csv('ratings.csv', sep=',', names=names)
df.head()
n_users = df.user_id.unique().shape[0]
n_items = df.item_id.unique().shape[0]
print (str(n_users) + ' users')
print (str(n_items) + ' items')
ratings = np.zeros((n_users, n_items))
for row in df.itertuples():
ratings[row[1]-1, row[2]-1] = row[3]
ratings
sparsity = float(len(ratings.nonzero()[0]))
sparsity /= (ratings.shape[0] * ratings.shape[1])
sparsity *= 100
print ('Sparsity: {:4.2f}%'.format(sparsity))
並沒有錯誤這樣的:
line 17, in <module>
ratings[row[1]-1, row[2]-1] = row[3]
TypeError: unsupported operand type(s) for -: 'str' and 'int'
的'Exception'是明確的 - 你'row's舉行'str's。 – AChampion