Python 用给定轴的最小值替换给定轴中的所有值
我试图用python实现这一点。 我最终得到了这段代码:Python 用给定轴的最小值替换给定轴中的所有值,python,python-3.x,numpy,scikit-learn,aggregate,Python,Python 3.x,Numpy,Scikit Learn,Aggregate,我试图用python实现这一点。 我最终得到了这段代码: def unknown(a, axis=0, p=0): if p >= 0 and p <=1: if not isinstance(a, np.ndarray): a = np.array(a, dtype=dtype) size = a.shape[axis] return (p/size)*(np.sum((a + (1 - p)), ax
def unknown(a, axis=0, p=0):
if p >= 0 and p <=1:
if not isinstance(a, np.ndarray):
a = np.array(a, dtype=dtype)
size = a.shape[axis]
return (p/size)*(np.sum((a + (1 - p)), axis=axis, dtype=dtype))
else:
raise ValueError("parameter p should be in range 0,1")
def未知(a轴=0,p轴=0):
如果p>=0且p以获得指定轴的最小值:
a = np.array([[1,2],[3,4]])
# min value per row
np.min(a, axis=1)
您的公式可以是:
(p/size)*np.sum(a, axis=axis, dtype=dtype) + (1-p)*np.min(a, axis=axis)
到目前为止你试过什么?