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:
我有一个14x5的数据矩阵,名为data。第一列(Y)是因变量,其次是4个自变量(X,S1,S2,S3)。当试图将一个回归模型拟合到一个独立变量子集['S2'][:T]时,我得到以下错误:
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)
1 个解决方案
#1
3
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都是序列对象(1维),所以如下。
[[X, S1, S2, S3]]
is 3-dimensional. sklearn
estimators expect an array of feature vectors (2-dimensional).
是三维的。sklearn估计器期望一个特征向量数组(二维)。
Try something like this:
试试这样:
# 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)
#1
3
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都是序列对象(1维),所以如下。
[[X, S1, S2, S3]]
is 3-dimensional. sklearn
estimators expect an array of feature vectors (2-dimensional).
是三维的。sklearn估计器期望一个特征向量数组(二维)。
Try something like this:
试试这样:
# 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)