【问题标题】:Python float error with numerical data带有数值数据的 Python 浮点错误
【发布时间】:2018-08-27 13:45:45
【问题描述】:

我正在尝试使用 sklearn 在具有 162 列和 69,000 行的数据集上运行 PCA。我不断收到下面的浮动错误消息,我已经检查以确保我只有数字数据。我可能做错了什么?任何帮助将不胜感激。

    >>> data = np.loadtxt("PCAdata.txt")
    >>> trans = data.transpose()
    >>> trans
    array([[0., 0., 1., ..., 0., 0., 1.],
           [0., 0., 1., ..., 1., 0., 2.],
           [0., 0., 1., ..., 0., 0., 1.],
           ...,
           [1., 0., 1., ..., 0., 0., 1.],
           [0., 0., 1., ..., 0., 0., 2.],
           [0., 0., 1., ..., 0., 0., 2.]])
    >>> sscaler = preprocessing.StandardScaler().fit(trans)
    >>> sscaler
    StandardScaler(copy=True, with_mean=True, with_std=True)
    >>> pca = PCA(n_components=2)
    >>> pca.fit(sscaler)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\sklearn\decomposition\pca.py", line 329, i
    n fit
        self._fit(X)
      File "C:\Python27\lib\site-packages\sklearn\decomposition\pca.py", line 370, i
    n _fit
        copy=self.copy)
      File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 433, in
     check_array
        array = np.array(array, dtype=dtype, order=order, copy=copy)
    TypeError: float() argument must be a string or a number

【问题讨论】:

    标签: python-2.7 scikit-learn pca


    【解决方案1】:

    fit 方法不返回矩阵。 Sklearn 会出错,因为您输入的参数sscaler 不是数字矩阵。如果你想得到缩放的数据矩阵,你可以使用fit_transform方法或者分别使用fittransform方法。

    例子:

    data = np.random.randint(0, 3, (100, 10))
    scaler = StandardScaler()
    data = scaler.fit_transform(data)
    pca = PCA()
    data = pca.fit_transform(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2015-04-02
      相关资源
      最近更新 更多