相关疑难解决方法(0)

将Python序列转换为NumPy数组,填充缺失值

可变长度列表的Python序列隐式转换为NumPy数组会导致该数组为object类型.

v = [[1], [1, 2]]
np.array(v)
>>> array([[1], [1, 2]], dtype=object)
Run Code Online (Sandbox Code Playgroud)

试图强制其他类型将导致异常:

np.array(v, dtype=np.int32)
ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

通过使用给定的占位符填充"缺失"值,获得int32类型的密集NumPy数组的最有效方法是什么?

从我的样本序列中v,我想得到类似的东西,如果0是占位符

array([[1, 0], [1, 2]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

python arrays numpy sequence variable-length-array

17
推荐指数
3
解决办法
6383
查看次数

标签 统计

arrays ×1

numpy ×1

python ×1

sequence ×1

variable-length-array ×1