y_hat1[i][j] = (y_hat1[i][j] - N1) / (M1 - N1) 对这个赋值,判断如果是负数就改为正数
时间: 2023-12-04 11:03:32 浏览: 25
如果你想要实现的是将反归一后的结果中的负数转换为正数,可以在反归一操作之后添加一个判断语句,将负数转换为正数。
例如,可以在反归一操作后加入以下代码:
```
for i in range(y_hat1.shape[0]):
for j in range(y_hat1.shape[1]):
y_hat1[i][j] = (y_hat1[i][j] - N1) / (M1 - N1)
if y_hat1[i][j] < 0:
y_hat1[i][j] = - y_hat1[i][j]
```
这段代码中,判断语句`if y_hat1[i][j] < 0:`会判断反归一后的结果是否小于0,如果是,则将其取反。这样就可以将负数转换为正数。
相关问题
y_g_hat1, y_g_hat2 = meta_net(x_val) y_g_hat1 = torch.softmax(interp(y_g_hat1), dim=1) y_g_hat2 = torch.softmax(interp(y_g_hat2), dim=1)
这段代码看起来是使用了一个元学习网络(meta-net)对输入数据(x_val)进行预测,预测结果分别保存在y_g_hat1和y_g_hat2中。接着使用了一个interp函数对y_g_hat1和y_g_hat2进行了插值操作,然后分别使用torch.softmax函数对插值后的结果进行了softmax归一化处理。softmax归一化可以将输出转化为概率分布,便于后续的分类和评估。不过具体的实现细节和输入输出的含义还需要结合具体的上下文进行理解。
def compute_pos_neg(y_actual, y_hat): TP = 0; FP = 0;TN = 0; FN = 0 for i in range(len(y_hat)): if y_actual[i]==y_hat[i]==1: TP += 1 if y_hat[i]==1 and y_actual[i]!=y_hat[i]: FP += 1 if y_actual[i]==y_hat[i]==0: TN += 1 if y_hat[i]==0 and y_actual[i]!=y_hat[i]: FN += 1 return TP,FP,TN,FN def metrics(TP,FP,TN,FN): a=TP+FP b=TP+FN c=TN+FP d=TN+FN #mcc=((TP*TN)-(FP*FN))/(math.sqrt(float(a*b*c*d)+0.0001)) F1=(2*TP)/float(2*TP+FP+FN+.0000001) # 在某些计算的分母中添加了一个小的正数 (0.0000001),以避免被零除错误。 precision=TP/float(TP+FP+.0000001) recall=TP/float(TP+FN+.0000001) TPR=TP/(TP+FN) FPR=FP/(FP+TN) print("**********************") print("F1 score:", F1) print("precision:", precision) print("recall:", recall) print("TPR:", TPR) print("FPR:", FPR) print("**********************") return TPR,FPR,F1,precision,recall代码为什么不执行
这是一个用Python编写的函数,用于计算二分类模型的混淆矩阵。输入参数为y_actual(实际标签)和y_hat(预测标签),输出结果为TP(真正样本数量)、FP(假正样本数量)、TN(真负样本数量)和FN(假负样本数量)。计算过程中通过循环遍历每一个样本,根据实际标签和预测标签的取值情况,对应地更新TP、FP、TN和FN的值。最后,通过调用metrics函数计算TPR(真正率,即TP/(TP+FN))和FPR(假正率,即FP/(FP+TN))等性能指标。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)