不能以形状(19,)(0,)-KNN广播操作数

问题描述 投票:-1回答:1

我正在研究如何使用KNN来预测电影的收视率。我用视频和书来教自己如何做]

我试图运行在书中找到的代码,但它给了我错误消息。我用错误信息搜索了一下,以便理解它并解决我的问题,但是我认为我不知道如何使解决方案适应我的问题。

import numpy as np

import pandas as pd

r_cols = ['user_id', 'movie_id', 'rating']

ratings = pd.read_csv('C:/Users/dell/Downloads/DataScience/DataScience-Python3/ml-100k/u.data', sep='\t', engine='python', names=r_cols, usecols=range(3))  # please enter your file path here. The file is u.data

print(ratings.head())   

movieProperties = ratings.groupby('movie_id').agg({'rating': [np.size, np.mean]})

print(movieProperties.head())

movieNumRatings = pd.DataFrame(movieProperties['rating']['size'])

movieNormalizedNumRatings = movieNumRatings.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))

print(movieNormalizedNumRatings.head())

movieDict = {}

with open('C:/Users/dell/Downloads/DataScience/DataScience-Python3/ml-100k/u.item') as f:     # The file is u.item

    temp = ''

    for line in f:

        fields = line.rstrip('\n').split('|')

        movieID = int(fields[0])

        name = fields[1]

        genres = fields[5:25]

        genres = map(int, genres)

        movieDict[movieID] = (name, genres, movieNormalizedNumRatings.loc[movieID].get('size'), movieProperties.loc[movieID].rating.get('mean'))

print(movieDict[1])

from scipy import spatial

def ComputeDistance(a, b):

    genresA = np.array(list(a[1]))

    genresB = np.array(list(b[1]))

    genreDistance = spatial.distance.cosine(genresA, genresB)

    popularityA = np.array(a[2])

    popularityB = np.array(b[2])

    popularityDistance = abs(popularityA - popularityB)

    return genreDistance + popularityDistance 

print(ComputeDistance(movieDict[2], movieDict[4])) 

import operator

def getNeighbors(movieID, K):

    distances = []

    for movie in movieDict:

        if (movie != movieID):

            dist = ComputeDistance(movieDict[movieID], movieDict[movie])

            distances.append((movie, dist))

    distances.sort(key=operator.itemgetter(1))

    neighbors = []

    for x in range(K):

        neighbors.append(distance[x][0])

    return neighbors

K = 10

avgRating = 0

neighbors = getNeighbors(1, K)

我从PowerShell得到此错误消息:

回溯(最近通话最近:)

neighbors = getNeighbors(1, K)

dist = ComputeDistance(movieDict[movieID], movieDict[movie])

genreDistance = spatial.distance.cosine(genresA, genresB)

return correlation(u, v, w=w, centered=False)

uv = np.average(u*v, weights=w)

ValueError: operands could not be broadcast together with shape (19,)(0,)

[当我尝试从ipython终端调试问题时收到此错误消息:

c:\programdata\anaconda3\lib\site-packages\scipy\spatial\distance.py(695)correlation()

     693        u = u - umu

     694        v = v - vmu

---> 695        uv = np.average(u*v, weights=w)

     696        uu = np.average(np.square(u), weights=w)

     697        vv = np.average(np.square(v), weights=w)


**Note**: The code ran fine and produced results up until *print(Cprint(ComputeDistance(movieDict[2], movieDict[4]))*

My guess is the problem is with this part of the code: 


import operator
def getNeighbors(movieID, K):
    distances = []
    for movie in movieDict:
        if (movie != movieID):
            dist = ComputeDistance(movieDict[movieID], movieDict[movie])
            distances.append((movie, dist))
    distances.sort(key=operator.itemgetter(1))
    neighbors = []
    for x in range(K):
        neighbors.append(distance[x][0])
    return neighbors

K = 10
avgRating = 0
neighbors = getNeighbors(1, K) 


The code can be found in this link: https://hendra-herviawan.github.io/Movie-Recommendation-based-on-KNN-K-Nearest-Neighbors.html
numpy error-handling ipython classification knn
1个回答
0
投票

“操作数不能与形状(x,)(y,)一起广播的错误通常在您试图在必须具有相同形状但不具有相同形状的两个数组之间执行操作时出现。在您的情况下,您尝试在两个数组u和v之间进行加权平均。数组u和v没有长度。

[我看到您通过用“ |”分隔行来解析电影列表字符,然后将这些结果存储在字典中。可能是此文件或其以“ |”分隔的部分返回不同的结果。

错误日志显示第二个数组没有任何元素,这可能是由电影文件上的空行生成的。

© www.soinside.com 2019 - 2024. All rights reserved.