我有一个字符串,它是causalimpact包的输出,我需要获得这个字符串中某处的实际平均和累积效果的数字:
“后验推断{因果影响}\n平均累积\n实际3.43 13.7\n预测(s.d.) 3.38 (0.02) 13.53 (0.08)\n95%CI 3.34,3.42\n\n绝对效应(s.d.) 0.04 (0.02) 0.17 (0.08)\n95% CI 0.01,0.08\n\n相对效应(s.d.) 1.27% (0.57%) 1.27% (0.57%)\n95% CI 0.18%,2.41%\n\n后部尾部概率p: 0.01\n后部探针。因果关系:99.2%\n\n有关详细信息,请运行以下命令: print(impact.summary('report'))“
当我打印(ci.summary)时,我得到一个很好的、有条理的表格,如下所示:
Posterior Inference {Causal Impact}
Average Cumulative
Actual 3.43 13.7
Prediction (s.d.) 3.38 (0.02) 13.53 (0.08)
95% CI [3.34, 3.42] [13.38, 13.68]
Absolute effect (s.d.) 0.04 (0.02) 0.17 (0.08)
95% CI [0.01, 0.08] [0.02, 0.33]
Relative effect (s.d.) 1.27% (0.57%) 1.27% (0.57%)
95% CI [0.18%, 2.41%] [0.18%, 2.41%]
Posterior tail-area probability p: 0.01
Posterior prob. of a causal effect: 99.2%
For more details run the command: print(impact.summary('report'))如何做一个re.search并获得实际的、绝对的和相对的效果?
这就是我目前使用的p值:
r1 = re.search('tail-area probability p: (.+?)\nPosterior prob.', ci.summary())发布于 2021-06-24 07:18:08
您可以使用r'-?\d+(?:\.\d+)?'与finditer和正则表达式命名组一起查找文本中的数字。
import re
s = '''
Posterior Inference {Causal Impact}\n Average Cumulative\nActual 3.43 13.7\nPrediction (s.d.) 3.38 (0.02) 13.53 (0.08)\n95% CI [3.34, 3.42] [13.38, 13.68]\n\nAbsolute effect (s.d.) 0.04 (0.02) 0.17 (0.08)\n95% CI [0.01, 0.08] [0.02, 0.33]\n\nRelative effect (s.d.) 1.27% (0.57%) 1.27% (0.57%)\n95% CI [0.18%, 2.41%] [0.18%, 2.41%]\n\nPosterior tail-area probability p: 0.01\nPosterior prob. of a causal effect: 99.2%\n\nFor more details run the command: print(impact.summary('report'))
'''
# get digits optionally follow by a decimal part and prefixed with a negative sign.
nums = r'-?\d+(?:\.\d+)?'
regex = re.compile(
rf"Actual\s(?P<actual_avg>{nums}).*?"
rf"(?P<actual_cumul>{nums})\n.*?"
rf"Absolute effect \(s\.d\.\)\s(?P<abs_avg>{nums}).*?"
rf"\(.*?\).*?(?P<abs_cumul>{nums}).*?"
rf"Relative effect \(s\.d\.\)\s(?P<relative_avg>{nums})%.*?"
rf"\(.*?\).*?(?P<relative_cumul>{nums})%.*?"
, re.DOTALL)
for r in regex.finditer(s):
print(r.groupdict()){
"actual_avg": "3.43",
"actual_cumul": "13.7",
"abs_avg": "0.04",
"abs_cumul": "0.17",
"relative_avg": "1.27",
"relative_cumul": "1.27",
}https://stackoverflow.com/questions/68107769
复制相似问题