将稀疏矩阵变换为张量

Ben*_*ler 5 pandas tensorflow

我有一个 pandas 数据框对象,其中包含一列,其中有一袋表示文本的单词,存储为 '' 类型的 29881x23947 稀疏矩阵。该列是使用 sklearn 和 fit_transform() 函数进行处理的。

我现在想使用 Convert_to_tensor() 函数将此列转换为 2D 张量。

x_train_tensor = tf.convert_to_tensor(x_train)
Run Code Online (Sandbox Code Playgroud)

获取错误消息:

类型错误:需要二进制或 unicode 字符串

将矩阵转换为张量需要哪种格式?

编辑:打印出它所说的列的类型:

<class 'scipy.sparse.csr.csr_matrix'>
Run Code Online (Sandbox Code Playgroud)

作为输出的数据帧示例:

0          (0, 6276)\t1\n  (0, 8209)\t1\n  (0, 14299)\t...
1          (0, 6276)\t1\n  (0, 8209)\t1\n  (0, 14299)\t...
2          (0, 6276)\t1\n  (0, 8209)\t1\n  (0, 14299)\t...
3          (0, 6276)\t1\n  (0, 8209)\t1\n  (0, 14299)\t...
Run Code Online (Sandbox Code Playgroud)

vij*_*y m 1

scipy这是将稀疏矩阵转换为密集格式的示例tensorflow

输入稀疏 scipy 矩阵

A = np.array([[1,2,0],[0,0,3],[4,0,0]])
sA = sparse.csr_matrix(A)

print (sA)
# (0, 0)    1
# (0, 1)    2
# (1, 2)    3
# (2, 0)    4

idx, idy, val = sparse.find(sA)

print(idx, idy, val)
#[0 2 0 1] [0 0 1 2] [1 4 2 3]
Run Code Online (Sandbox Code Playgroud)

到张量流

#merge idx and idy array to convert to [idx, idy] matrix    
full_indices = tf.stack([idx, idy], axis=1)

#Output matrix size
depth_x = 3
depth_y = 3

# sparse to dense matrix 
dense = tf.sparse_to_dense(full_indices,tf.constant([depth_x,depth_y]), val, validate_indices=False)

with tf.Session() as sess:
   print(sess.run(dense))
#[[1 2 0]
# [0 0 3]
# [4 0 0]]
Run Code Online (Sandbox Code Playgroud)