Python Numpy : operands could not be broadcast together with shapes

房东的猫 提交于 2020-01-17 03:14:25

问题


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)

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

Error is ValueError:

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

回答1:


Reshape 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)


来源:https://stackoverflow.com/questions/27931017/python-numpy-operands-could-not-be-broadcast-together-with-shapes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!