在 Python 中,如何判断矩阵或向量是否包含在中括号内?
preds 的原值是
preds= [[0.46720526]
[0.99414194]
[0.26025357]
[0.99096197]
[0.99137177]]
经过下面的操作之后
preds[preds <= 0.5] = 0
preds[preds > 0] = 1
就把小于 0.5 的设置为 0,大于 0 的设置为 1 了
[[0.]
[1.]
[0.]
[1.]
[1.]]
请问这样的神操作学名叫什么呢?是什么原理呢?
在 Python 中,如何判断矩阵或向量是否包含在中括号内?
5 回复
preds 的原值八成不是 list of list,是 numpy.array([[…]])。
numpy.array 重写了 getitem。
重写了 getitem 和 setitem
叫 mask 赋值,另外 where 更好用
https://pandas.pydata.org/pandas-docs/stable/indexing.html#the-where-method-and-masking
numpy 里面叫 fancy indexing, 翻译成花式索引
仰慕三位大神,表示感谢,我去学习学习

