【问题标题】:PCA in Sklearn - ValueError: array must not contain infs or NaNsSklearn 中的 PCA - ValueError:数组不得包含 infs 或 NaN
【发布时间】: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


【解决方案1】:

这是我写的一些代码。它似乎对我有用。请注意,当您调用 fit 时,您需要为其提供训练数据(即 Y 向量)。

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)

print(CLF.best_params_)

打印语句将向您展示最好的n_components。没有 y,就无法计算 RSS,也无法判断什么是“最佳”。

【讨论】:

  • 谢谢@Demetri P,你说得对,我没有为 GridsearchCV 包括 y。我试图将 fit 参数用于 PCA 而不是 GridsearchCV。但是,我试图将 ymatrix 拟合到我的数据集中(但它在我的可重现示例中有效),但仍然出现相同的错误。我会将编辑放入
  • @ScottDavis 嗯,这很难。恐怕我对这个没有想法。
【解决方案2】:

我可能对scikit-learn 0.18.1. 中的 PCA 实施有问题

查看错误报告https://github.com/scikit-learn/scikit-learn/issues/7568

描述的解决方法是使用带有svd_solver='full' 的 PCA。 所以试试这个代码:

pipe = [('pca',PCA(whiten=True,svd_solver='full')),
       ('clf' ,lm)]

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 2016-06-16
    • 2021-07-07
    • 2016-06-20
    • 2019-10-31
    • 2013-10-13
    • 1970-01-01
    • 2021-03-17
    • 2017-12-25
    相关资源
    最近更新 更多