【发布时间】:2017-05-04 23:24:14
【问题描述】:
我正在尝试使用网格搜索来选择数据的主要成分的数量,然后再进行线性回归。我很困惑如何制作我想要的主要成分数量的字典。我将列表放入 param_grid 参数中的字典格式,但我认为我做错了。到目前为止,我收到了关于我的数组包含 infs 或 NaNs 的警告。
我正在遵循将线性回归流水线化到 PCA 的说明:http://scikit-learn.org/stable/auto_examples/plot_digits_pipe.html
ValueError: 数组不能包含 infs 或 NaNs
我能够在可重现的示例中得到相同的错误,我的真实数据集更大:
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
df2 = pd.DataFrame({ 'C' : pd.Series(1, index = list(range(8)),dtype = 'float32'),
'D' : np.array([3] * 8,dtype = 'int32'),
'E' : pd.Categorical(["test", "train", "test", "train",
"test", "train", "test", "train"])})
df3 = pd.get_dummies(df2)
lm = LinearRegression()
pipe = [('pca',PCA(whiten=True)),
('clf' ,lm)]
pipe = Pipeline(pipe)
param_grid = {
'pca__n_components': np.arange(2,4)}
X = df3.as_matrix()
CLF = GridSearchCV(pipe, param_grid = param_grid, verbose = 1, cv = 3)
y = np.random.normal(0,1,len(X)).reshape(-1,1)
CLF.fit(X,y)
ValueError: array must not contain infs or NaNs
编辑:我为 fit 语句输入了 y,但它仍然给了我同样的错误。但是,这是针对我的数据集而不是可重现的示例。
【问题讨论】:
-
你能运行
np.any(np.isnan(X_mat))并告诉我返回值吗? -
在我的数据集上,它返回 'False'
标签: python scikit-learn pca