比较布尔值的两个数据框列 [英] comparing two dataframe columns of booleans
问题描述
我有两个数据帧,每个数据帧分别表示实际降雨和预测的降雨情况。实际降雨数据帧是恒定的,因为这是已知结果。预测的降雨数据帧如下所示。
I have two dataframes each denoting actual rain and predicted rain condition. Actual rain dataframe is constant as it is a known result. Predicted rain dataframe They are given below.
actul =
index rain
Day1 True
Day2 False
Day3 True
Day4 True
预测的降雨数据帧在下面给出。该数据框会根据使用的预测模型不断变化。
Predicted rain dataframe is given below. This dataframe keeps on changing based on predicted model used.
prdt =
index rain
Day1 False
Day2 True
Day3 True
Day4 False
我正在开发上述给定模型的预测精度
I am developing prediction accuracy of above prediction model as given below:
#Following computes the number days on which raining was predicted correctly
a = sum(np.where(((actul['rain'] == True)&(prdt['rain']==True)),True,False))
#Following computes the number days on which no-rain was predicted correctly
b = sum(np.where(((actul['rain'] == False)&(prdt['rain']==False)),True,False))
#Following computes the number days on which raining was incorrectly predicted
c = sum(np.where(((actul['rain'] == True)&(prdt['rain']==False)),True,False))
#Following computes the number days on which no-rain was incorrectly predicted
d = sum(np.where(((actul['rain'] == False)&(prdt['rain']==True)),True,False))
predt_per = (a+b)*100/(a+b+c+d)
我上面的代码花了太多时间来计算。有没有更好的方法可以达到上述效果?
My above code is taking too much time to compute. Is there a better way to achieve above result?
现在,低于接受的答案可以解决上述问题。看起来下面的代码有问题,因为我得到所有数据帧的 100%
预测百分比。我的代码是:
Now, below accepted answer solved my above problem. Looks like something is wrong in my code given below because I am getting 100%
prediction percentage for all dataframes. My code is:
alldates_df =
index met1_r2 useful met1_r2>0.5
0 0.824113 True True
1 0.903828 True True
2 0.500765 True True
3 0.889757 True True
4 0.890102 True True
5 0.893995 True True
6 0.933482 True True
7 0.872847 True True
8 0.913142 True True
9 0.901424 True True
10 0.910941 True True
11 0.927310 True True
12 0.934538 True True
13 0.946092 True True
14 0.653831 True True
15 0.390702 True False
16 0.878493 True True
17 0.899739 True True
18 0.938481 True True
19 -850.978703 False False
20 -21.802518 False False
met1_detacu = [] # Method1_detection accuracy at various settings
var_flset = np.arange(-5,1,0.01) # various filter settings
for i in var_flset:
pdt_usefl = alldates_df.assign(result=alldates_df['met1_r2']>i)
x = pd.concat([alldates_df['useful'],pdt_usefl['result']],axis=1).sum(1).isin([0,2]).mean()*100
met1_detacu.append(x)
plt.plot(var_flset,met1_detacu)
我上面的代码可以正常工作,但我得到但我得到了所有<$在所有变量过滤器设置
中,c $ c> 100%的检测精度。这里不对劲。
获得的地块:
My above code is working fine but I am getting but I am getting all 100%
detection accuracy at all the varible filter settings
. Something is wrong here.
Obtained plot:
期望的图是:
@WeNYoBen
@WeNYoBen
推荐答案
在您的情况下,假设索引是df的索引,因此我们可以在 concat <之后使用
sum
/ code>,因为True + True == 2和False + False == 0
In your case assuming the index is the index of df , so we can using sum
after concat
, since True + True ==2 and False + False ==0
pd.concat([df1,df2],axis=1).sum(1).isin([0,2]).mean()*100
25.0
更新
Update
met1_detacu = [] # Method1_detection accuracy at various settings
var_flset = np.arange(-5,1,0.01) # various filter settings
for i in var_flset:
pdt_usefl = alldates_df.assign(result=alldates_df['met1_r2']>i)
x = pd.concat([alldates_df['useful'],pdt_usefl['result']],axis=1).sum(1).isin([0,2]).mean()*100
met1_detacu.append(x)
plt.plot(var_flset,met1_detacu)
这篇关于比较布尔值的两个数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!