我尝试对保险数据使用线性回归。但是在尝试使用 features 参数调用函数时出错。这是我的代码:
def h(x):
global w
return np.sum(np.transpose(w)*x)
raise NotImplementedError()
当尝试使用简单的数据时,它工作正常,
w, x = [1,2,3], [2,3,4]
h(x)
输出为:20
但是当尝试使用数据集时,它会出错:
features = dataset.drop(["charges"], axis=1).values
h(features )
它返回错误:
ValueError: operands could not be broadcast together with shapes (3,) (1338,)
所以功能看起来像这样:
array([[0.1173913 , 0. , 0.35698144, 0. , 1. ],
[0.1 , 1. , 0.48331988, 1. , 0. ],
[0.27391304, 1. , 0.46674738, 3. , 0. ],
...,
[0.1 , 0. , 0.5496099 , 0. , 0. ],
[0.15217391, 0. , 0.3117837 , 0. , 0. ],
[0.84782609, 0. , 0.38216303, 0. , 1. ]])
我使用的数据是来自 kaggle.com 的 insurance.csv