Python Numpy:操作数不能与形状一起广播 [英] Python Numpy : operands could not be broadcast together with shapes

查看:501
本文介绍了Python Numpy:操作数不能与形状一起广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此代码,我收到此错误操作数不能与形状一起广播"

I am getting this error "operands could not be broadcast together with shapes" for this code

import numpy as np
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
beantown = load_boston()
x=beantown.data
y=beantown.target
model = LinearRegression()
model = model.fit(x,y)

def mse(truth, predictions): 
    return ((truth - predictions) ** 2).mean(None)
print model.score(x,y)
print mse(x,y)

错误出现在行打印mse(x,y)

The error is on the line print mse(x,y)

错误是ValueError:

Error is ValueError:

operands could not be broadcast together with shapes (506,13) (506,)

推荐答案

重塑y:

from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
beantown = load_boston()
x = beantown.data
y = beantown.target
y = y.reshape(y.size, 1)
model = LinearRegression()
model = model.fit(x, y)


def mse(truth, predictions):
    return ((truth - predictions) ** 2).mean(None)
print model.score(x, y)
print mse(x, y)

这篇关于Python Numpy:操作数不能与形状一起广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆