【问题标题】:Python - running into x_test y_test fit errorsPython - 遇到 x_test y_test 拟合错误
【发布时间】:2019-01-31 04:13:23
【问题描述】:

我已经建立了一个神经网络,它可以很好地处理大约 300,000 行的小型数据集,其中包含 2 个分类变量和 1 个自变量,但是当我将其增加到 650 万行时遇到了内存错误。所以我决定修改代码并且越来越接近,但现在我遇到了适合错误的问题。我有 2 个分类变量和 1 列用于 1 和 0 的因变量(可疑或不可疑。开始数据集如下所示:

DBF2
   ParentProcess                   ChildProcess               Suspicious
0  C:\Program Files (x86)\Wireless AutoSwitch\wrl...    ...            0
1  C:\Program Files (x86)\Wireless AutoSwitch\wrl...    ...            0
2  C:\Windows\System32\svchost.exe                      ...            1
3  C:\Program Files (x86)\Wireless AutoSwitch\wrl...    ...            0
4  C:\Program Files (x86)\Wireless AutoSwitch\wrl...    ...            0
5  C:\Program Files (x86)\Wireless AutoSwitch\wrl...    ...            0

我的代码跟随/出现错误:

import pandas as pd
import numpy as np
import hashlib
import matplotlib.pyplot as plt
import timeit

X = DBF2.iloc[:, 0:2].values
y = DBF2.iloc[:, 2].values#.ravel()

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 0] = labelencoder_X_1.fit_transform(X[:, 0])
labelencoder_X_2 = LabelEncoder()
X[:, 1] = labelencoder_X_2.fit_transform(X[:, 1])

onehotencoder = OneHotEncoder(categorical_features = [0,1])
X = onehotencoder.fit_transform(X)

index_to_drop = [0, 2039]
to_keep = list(set(xrange(X.shape[1]))-set(index_to_drop))
X = X[:,to_keep]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)

#ERROR
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sklearn/base.py", line 517, in fit_transform
    return self.fit(X, **fit_params).transform(X)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/preprocessing/data.py", line 590, in fit
    return self.partial_fit(X, y)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/preprocessing/data.py", line 621, in partial_fit
    "Cannot center sparse matrices: pass `with_mean=False` "
ValueError: Cannot center sparse matrices: pass `with_mean=False` instead. See docstring for motivation and alternatives.

X_test = sc.transform(X_test)

#ERROR
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sklearn/preprocessing/data.py", line 677, in transform
    check_is_fitted(self, 'scale_')
  File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 768, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

如果这有助于我打印 X_train 和 y_train:

X_train
<5621203x7043 sparse matrix of type '<type 'numpy.float64'>'
with 11242334 stored elements in Compressed Sparse Row format>

y_train
array([0, 0, 0, ..., 0, 0, 0])

【问题讨论】:

  • 您是否尝试过错误消息建议的修复? sc = StandardScalar(with_mean=False)?

标签: python arrays pandas scikit-learn


【解决方案1】:

X_train 是一个稀疏矩阵,非常适合您使用大型数据集(如您的情况)。问题在于documentation 解释说:

with_mean : 布尔值,默认为真

如果为 True,则在缩放之前将数据居中。这不起作用(并且会 引发异常)在稀疏矩阵上尝试时,因为 使它们居中需要建立一个密集的矩阵,该矩阵通常使用 案例可能太大而无法放入内存。

您可以尝试传递with_mean=False

sc = StandardScaler(with_mean=False)
X_train = sc.fit_transform(X_train)

以下行失败,因为 sc 仍然是未触及的 StandardScaler 对象。

X_test = sc.transform(X_test)

为了能够使用转换方法,您首先必须将StandardScaler 拟合到数据集。如果您的意图是将StandardScaler 拟合到您的训练集并使用它将训练集和测试集转换到相同的空间,那么您可以按如下方式进行:

sc = StandardScaler(with_mean=False)
X_train_sc = sc.fit(X_train)
X_train = X_train_sc.transform(X_train)
X_test = X_train_sc.transform(X_test)

【讨论】:

  • 真棒回答这个工作!非常感谢您花时间解释。 @tobsecret
  • 很高兴它有帮助!
猜你喜欢
  • 2022-09-23
  • 1970-01-01
  • 2018-01-02
  • 2018-11-28
  • 1970-01-01
  • 2020-06-23
  • 2019-06-08
  • 2021-01-13
  • 2021-02-15
相关资源
最近更新 更多