我想将2D数组传递给线性回归:
x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 3]]
y = [3.9857, 3.6877, 3.6877]
x = numpy.array(x)
y = numpy.array(y)
model = LinearRegression(z,y).fit(z,y)
我没有使用重塑(-1,1),因为它使2D阵列变为3D
但是我遇到了错误:
ValueError: setting an array element with a sequence
TypeError: float() argument must be a string or a number, not 'list'
如何正确地将二维数组传递给线性回归?
答案 0 :(得分:0)
键入注释会有点长,因此,如果您查看x,那么在转换为numpy数组之前:
.map
print([len(i) for i in x])
[36, 10, 10]
的长度为3。可以进行线性回归,但是每次观察您的自变量必须具有相同数量的变量。
对于您而言,列表y
的第一个元素应该有10个条目,就像其他条目一样。
例如:
x
您将获得10个系数,x的每一列一个。