python - scipy 稀疏矩阵上的 sklearn train_test_split 给出了错误的结果

标签 python scipy scikit-learn

我有一个形状为 (346679, 86) 的稀疏矩阵。

<346679x86 sparse matrix of type '<type 'numpy.int8'>' with 470018 stored elements in COOrdinate format>

为了训练和评估我的模型,我需要将它分别分成训练集和测试集。

from sklearn.model_selection import train_test_split
x_train, x_test = train_test_split(sparse_matrix, test_size=0.2, random_state=11)

完成此操作后,我发现 x_train 和 x_test 发生了变化,即一些整行变为 0。我使用以下代码检查了原始矩阵中具有零非零值的行的出现情况:

def get_zero_rows(sparse_matrix):
    sparse_matrix = sparse_matrix.tocsr()
    count = 0
    for index, each in enumerate(sparse_matrix):
        if each.getnnz() < 1:
            count += 1
    return count

它为原始矩阵返回 0,但为拆分矩阵返回非零值。我不明白为什么会这样?

最佳答案

根据 train_test_split 文档中的示例构建:

In [895]: X, y = sparse.random(50,10,.2,'csr'), range(50)
In [896]: X_train, X_test, y_train, y_test = train_test_split(
     ...: ...     X, y, test_size=0.33, random_state=42)
     ...:     
In [897]: X
Out[897]: 
<50x10 sparse matrix of type '<class 'numpy.float64'>'
    with 100 stored elements in Compressed Sparse Row format>
In [898]: X_train
Out[898]: 
<33x10 sparse matrix of type '<class 'numpy.float64'>'
    with 68 stored elements in Compressed Sparse Row format>
In [899]: X_test
Out[899]: 
<17x10 sparse matrix of type '<class 'numpy.float64'>'
    with 32 stored elements in Compressed Sparse Row format>

非零总数没有变化

In [900]: np.count_nonzero(X.sum(1)==0)
Out[900]: 4
In [901]: np.count_nonzero(X_test.sum(1)==0)
Out[901]: 2
In [902]: np.count_nonzero(X_train.sum(1)==0)
Out[902]: 2

0 行总和也保持不变。

当我尝试同样的方法时

X = (sparse.random(50,10,.2,'csr')*10).astype('int8') 

0 行计数保持一致,但我得到的 nnz 元素更少。 int8 的稀疏数学运算可能会出现问题。标准 intfloat dtypes 可能更安全。

稀疏 使用矩阵乘法(使用 extractor 矩阵)的行索引,我相信它是为 32/64 位数据类型编译的。


我看到的“问题”是我如何构建整数稀疏矩阵的产物。我没有正确地消除零

In [20]: from scipy import sparse
In [21]: M = sparse.random(100,10,.2,'csr')
In [22]: M
Out[22]: 
<100x10 sparse matrix of type '<class 'numpy.float64'>'
    with 200 stored elements in Compressed Sparse Row format>
In [23]: idx=np.arange(100)
In [24]: M[idx,:]
Out[24]: 
<100x10 sparse matrix of type '<class 'numpy.float64'>'
    with 200 stored elements in Compressed Sparse Row format>

通过缩放 float 生成一个随机整数矩阵:

In [25]: M1 = (M*10).astype(int)
In [26]: M1
Out[26]: 
<100x10 sparse matrix of type '<class 'numpy.int64'>'
    with 200 stored elements in Compressed Sparse Row format>

索引减少了元素的数量:

In [27]: M1[idx,:]
Out[27]: 
<100x10 sparse matrix of type '<class 'numpy.int64'>'
    with 183 stored elements in Compressed Sparse Row format>

但这与 count_nonzero 找到的数字相同。如果我应用 elimnate_zeros,我会得到什么:

In [29]: M1.count_nonzero()
Out[29]: 183
In [30]: M1.eliminate_zeros()
In [31]: M1
Out[31]: 
<100x10 sparse matrix of type '<class 'numpy.int64'>'
    with 183 stored elements in Compressed Sparse Row format>
In [32]: M1[idx,:]
Out[32]: 
<100x10 sparse matrix of type '<class 'numpy.int64'>'
    with 183 stored elements in Compressed Sparse Row format>

使用这个缩放构造函数,像 0.04 这样的浮点值变为 0,但在我们明确这样做之前不会从稀疏性中删除。

关于python - scipy 稀疏矩阵上的 sklearn train_test_split 给出了错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892250/

相关文章:

python - 来自 C 的 Python 中的按位运算

python - scipy ndimage.rotate 不适用于 np.nan 值

python - 是什么导致 symfit 出现此警告?

tensorflow - 当设置 n_job=-1 并且 TF 在单个 GPU 上运行时,带有 TF 模型的 KerasClassifier 可以与 sklearn.cross_val_score 一起使用吗?

python - 消息不适合 sklearn k-means 收敛实现

python - 在 subprocess.call() 中使用带反引号的命令替换

java - NLTK 和 MaltParser 退出,错误代码为 1

python - 在 Python 中检测两个图像在视觉上是否相同的快速有效方法

python - SciPy - 子类化 rv_continuous 时出现 'Object too deep for desired array'

python - 无法理解 sklearn 的 PolynomialFeatures