numpy - 无法以形状(19,)(0,)广播操作数— KNN

标签 numpy error-handling ipython classification knn

我正在研究如何使用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

最佳答案

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

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

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

关于numpy - 无法以形状(19,)(0,)广播操作数— KNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58779984/

相关文章:

python - 移植旧的fortran程序以与python + numpy一起使用

Python linprog最小化--单纯形法

python - 离散化为 N 个类别,每个类别具有相同数量的观察值

java - 官方Windows Azure SDK导致的Android捕获错误

python - Pandas 按降序绘制 x 或 index_column

python - 发生异常后在最旧的堆栈帧中启动 python 调试器

python - 如何用矢量化计算距离矩阵的种类

file-upload - 没有上传整个文件的IIS7.5中的自定义404.13错误

c++ - 发生内存不足错误时怎么办?

python - 从 `%timeit` ipython magic 获取平均运行时间