不支持的操作数类型

时间:2016-03-09 03:49:17

标签: python

我正在尝试创建一个可以输入用户名的程序,它会使用人员距离来查找最近的人。

我一直在遇到这些错误,更具体地说是操作数错误。我该如何解决这个问题?

Traceback如下:

Traceback (most recent call last):    
    line 60, in <module> pprint(nearest_neighbor("Ricky", users))    
    line 52, in nearest_neighbor (pearson(ratings[username], ratings[other_user]),     
    line 22, in pearson square_sum1 = sum(pow(ratingsUser1.values(),2))    
TypeError: unsupported operand type(s) for ** or pow(): 'dict_values' and 'int'

这是我的代码:

users = {"Ricky": {"Bob Seger": 4.75, "CCR": 4.5, "Beatles": 5, "The Who": 4.25, "Taylor Swift": 4},
     "Meg": {"Bob Seger": 4, "CCR": 3, "Beatles": 5, "The Who": 2, "Taylor Swift": 1},
     "Jake": {"Bob Seger": 4, "CCR": 3, "Beatles": 5, "The Who": 3, "Taylor Swift": 3}
     }

def pearson(ratingsUser1, ratingsUser2):


# Summation over all attributes for both objects
sum_ratingsUser1 = sum(ratingsUser1.values())
sum_ratingsUser2 = sum(ratingsUser2.values())

# Sum the squares
square_sum1 = sum(pow(ratingsUser1.values(),2))
square_sum2 = sum(pow(ratingsUser2.values(),2))

# Add up the products
product = sum([ratingsUser1.values*ratingsUser2.values])

#Calculate Pearson Correlation score
numerator = product - (sum_ratingsUser1*sum_ratingsUser2/len(ratingsUser1))
denominator = ((square_sum1 - pow(sum_ratingsUser1,2)/len(ratingsUser1)) * (square_sum2 -
    pow(sum_ratingsUser2,2)/len(ratingsUser1))) ** 0.5

# Can"t have division by 0
if denominator == 0:
    return 0

result = numerator/denominator
return result
# Compute the Summation

# Compute the numerator

# Compute the denominator


def nearest_neighbor(username, ratings):
distances = []

for other_user in ratings:
    if other_user != username:
        distances.append(
            (pearson(ratings[username], ratings[other_user]), other_user)
        )
distances.sort()

return distances

from pprint import pprint

pprint(nearest_neighbor("Ricky", users))

2 个答案:

答案 0 :(得分:0)

你走了:

您的代码有一些问题......

  1. 在您编写 1.000000000000000000e+00,2.000000000000000000e+00 3.000000000000000000e+00,4.000000000000000000e+00 的行中,您试图获取字典值的总和,在这种情况下,最好通过for循环遍历它们。

  2. 在修复该错误后尝试运行后,我发现了另一个错误。在您编写square_sum1 = sum(pow(ratingsUser1.values(),2))的行中,您再次尝试将字典值的总和相乘,即使您可以这样做,也会错过product = sum([ratingsUser1.values*ratingsUser2.values])在{{{}的末尾1}}。

  3. ()


    编程愉快!
    信号

答案 1 :(得分:0)

您不能将pow与dict / list

一起使用
from pprint import pprint

users = {"Ricky": {"Bob Seger": 4.75, "CCR": 4.5, "Beatles": 5, "The Who": 4.25, "Taylor Swift": 4},
     "Meg": {"Bob Seger": 4, "CCR": 3, "Beatles": 5, "The Who": 2, "Taylor Swift": 1},
     "Jake": {"Bob Seger": 4, "CCR": 3, "Beatles": 5, "The Who": 3, "Taylor Swift": 3}
     }


def pearson(ratingsUser1, ratingsUser2):
    # Summation over all attributes for both objects
    sum_ratingsUser1 = sum(ratingsUser1.values())
    sum_ratingsUser2 = sum(ratingsUser2.values())
    # Sum the squares
    square_sum1 = sum(base ** 2 for base in ratingsUser1.values())
    square_sum2 = sum(base ** 2 for base in ratingsUser2.values())
    # Add up the products
    product = sum_ratingsUser1 * sum_ratingsUser2


    try:
        # Can"t have division by 0
        #Calculate Pearson Correlation score
        numerator = product - (sum_ratingsUser1*sum_ratingsUser2 / len(ratingsUser1))
        denominator = (
                          (square_sum1 - pow(sum_ratingsUser1, 2)/len(ratingsUser1)) *
                          (square_sum2 - pow(sum_ratingsUser2, 2)/len(ratingsUser1))) ** 0.5

        return numerator/denominator
    except ZeroDivisionError:
        return 0

# Compute the Summation
# Compute the numerator
# Compute the denominator

 def nearest_neighbor(username, ratings):    
    distances = [(pearson(ratings[username], ratings[other_user]), other_user) for other_user in ratings if other_user != username and username in ratings ]
    distances.sort()
    return distances




pprint(nearest_neighbor("Ricky", users))