错误:发现数组具有暗淡3.估计量应小于等于2 [英] Error: Found array with dim 3. Estimator expected <= 2

查看:195
本文介绍了错误:发现数组具有暗淡3.估计量应小于等于2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标题为data的14x5数据矩阵.第一列(Y)是因变量,后跟4个自变量(X,S1,S2,S3).尝试将回归模型拟合到自变量['S2'] [:T]的子集时,出现以下错误:

I have a 14x5 data matrix titled data. The first column (Y) is the dependent variable followed by 4 independent variables (X,S1,S2,S3). When trying to fit a regression model to a subset of the independent variables ['S2'][:T] I get the following error:

ValueError: Found array with dim 3. Estimator expected <= 2.

如果对修复有任何见解,我将不胜感激.下面的代码.

I'd appreciate any insight on a fix. Code below.

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression


data = pd.read_csv('C:/path/Macro.csv')
T=len(data['X'])-1

#Fit variables
X = data['X'][:T]
S1 = data['S1'][:T]
S2 = data['S2'][:T]
S3 = data['S3'][:T]
Y = data['Y'][:T]

regressor = LinearRegression()
regressor.fit([[X,S1,S2,S3]], Y)

推荐答案

您正在将3维数组作为第一个参数传递给fit(). X,S1,S2,S3都是Series对象(一维),因此以下内容

You are passing a 3-dimensional array as the first argument to fit(). X, S1, S2, S3 are all Series objects (1-dimensional), so the following

[[X, S1, S2, S3]]

是3维的. sklearn估计量期望一个特征向量数组(二维).

is 3-dimensional. sklearn estimators expect an array of feature vectors (2-dimensional).

尝试这样的事情:

# pandas indexing syntax
# data.ix[ row index/slice, column index/slice ]

X = data.ix[:T, 'X':]  # rows up to T, columns from X onward
y = data.ix[:T, 'Y']   # rows up to T, Y column
regressor = LinearRegression()
regressor.fit(X, y)

这篇关于错误:发现数组具有暗淡3.估计量应小于等于2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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