python - Scipy sparse array from list of integers
问题描述
I have a list (actually, a panda Series) of integers which I want to turn into a sparse matrix for which row i
has only one non zero value at j
the index defined by the i
th element of the list. A little bit like in the question : Numpy: set one specific element of each column based on indexing by array
For now I am first turning the data in a dense matrix, like this :
def cat_to_mat(c, mat_length):
res = np.zeros((len(c), mat_length))
res[np.arange(len(c)), c.values % mat_length] = 1
return res
and then I call csr_matrix
on the resulting array. However, it seems a waste to generate a dense matrix and throw away most of its elements.
I am aware of tools like sklearn.preprocessing.OneHotEncoder
but they do not really suit because I would like to avoid calling the fit()
method: I want to be sure that the (sparse) representation of the data will only depend on c
and mat_length
(and not on some previous data provided to the OneHotEncoder
)
解决方案
Seems rather straightforward. Did I misunderstand the question?
from scipy import sparse
def cat_to_mat(c, mat_length):
return sparse.csc_matrix((c, (len(c), c % mat_length)), shape=(len(c), mat_length))
推荐阅读
- http - 通过 https 发出原始请求时,在 Location-header 中获取 http
- reactjs - 在 ReactJS 中使用 .match() - match 不是一个函数?
- javascript - 如何在Javascript中反转嵌套对象
- laravel - Laravel,Vue fetch什么都不返回,但控制台中有数据
- java - 在 Eclipse 中运行 Java 项目时出现“无法启动选择,并且没有最近启动”提示消息
- sql - 如何获取增量值并将其显示在jsp的输入字段中
- c++ - 如何正确退出使用 Boost Log 的程序?
- c++ - 在 ARM 上初始化 uint32x4_t 时出现错误 C2078?
- javascript - 如何在 html/css/js 中呈现跨媒体栏(PS3 UI)
- shopify - 如何在shopify中获取付款方式