Numpy 将概率转换为预测标签的最简单、最快的方法是什么?
简而言之,我正在为以下代码寻找一行/两行代码:Numpy 将概率转换为预测标签的最简单、最快的方法是什么?,numpy,machine-learning,classification,vectorization,logistic-regression,Numpy,Machine Learning,Classification,Vectorization,Logistic Regression,简而言之,我正在为以下代码寻找一行/两行代码: for i in range(A.shape[1]): # Convert probabilities A[0,i] to actual predictions p[0,i] ### START CODE HERE ### (≈ 4 lines of code) if(A[i] > .5) Y_prediction[i] = 1 else Y_prediction[i] = 0
for i in range(A.shape[1]):
# Convert probabilities A[0,i] to actual predictions p[0,i]
### START CODE HERE ### (≈ 4 lines of code)
if(A[i] > .5)
Y_prediction[i] = 1
else
Y_prediction[i] = 0
您希望将概率值转换为0-1标签。赋值
V_预测[0,:]=A[0,:]>0.5
应该足够了;如果目标数组V_预测为数值,则布尔值A[0,:]>0.5
(真/假)将变为数字1,0。例如:
V_prediction = np.zeros((3, 10))
A = np.random.uniform(size=(3, 10))
V_prediction[0, :] = A[0, :] > 0.5
V_预测现在是(随机的)
如果所有标签都是整数,则可以使用整数数据类型声明V_预测
一种更复杂的方法是使用
V_prediction[0,:]=np.分段(A[0,:],[A[0,:]>0.5,A[0,:]numpyargmax
函数应该在这里派上用场。只需将数组作为参数传递给argmax
函数,它就会根据概率给出标签
更多信息,请参见。添加具有预期输出的示例案例?来自Andrew Ng的DL专门化的代码。。。?
array([[ 1., 0., 1., 1., 0., 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
V_prediction[0, :] = np.piecewise(A[0, :], [A[0, :] > 0.5, A[0, :] <= 0.5], [0, 1])