如何计算ndarray中每个数据点的元素数?
我想要做的是在我的ndarray中至少出现N次的所有值上运行OneHotEncoder.
我还想将所有出现少于N次的值替换为它没有出现在数组中的另一个元素(让我们称之为new_value).
所以我举例说:
import numpy as np
a = np.array([[[2], [2,3], [3,34]],
[[3], [4,5], [3,34]],
[[3], [2,3], [3,4] ]]])
Run Code Online (Sandbox Code Playgroud)
阈值N = 2我想要的东西:
b = [OneHotEncoder(a[:,[i]])[0] if count(a[:,[i]])>2
else OneHotEncoder(new_value) for i in range(a.shape(1)]
Run Code Online (Sandbox Code Playgroud)
所以只有理解我想要的替换,不考虑onehotencoder和使用new_value = 10我的数组应该是这样的:
a = np.array([[[10], [2,3], [3,34]],
[[3], [10], [3,34]],
[[3], [2,3], [10] ]]])
Run Code Online (Sandbox Code Playgroud)