首页 > 解决方案 > 从熊猫数据框中的 ufloat 中提取名义偏差和标准偏差

问题描述

为方便起见,我使用 pandas 数据帧来对大量数据执行不确定性传播。

然后我希望绘制我的数据集的标称值,但类似的东西myDF['colLabel'].n不起作用。如何从数据框中提取名义偏差和标准偏差以绘制名义值和误差线?

这是一个更一致的 MWE:

#%% MWE
import pandas as pd
from uncertainties import ufloat
import matplotlib.pyplot as plt

# building of a dataframe filled with ufloats
d = {'value1': [ufloat(1,.1),ufloat(3,.2),ufloat(5,.6),ufloat(8,.2)], 'value2': [ufloat(10,5),ufloat(50,2),ufloat(30,3),ufloat(5,1)]}
df = pd.DataFrame(data = d)

# plot of value2 vs. value1 with errobars.
plt.plot(x = df['value1'].n, y = df['value2'].n)
plt.errorbar(x = df['value1'].n, y = df['value2'].n, xerr = df['value1'].s, yerr = df['value2'].s)
# obviously .n and .s won't work.

我得到一个错误AttributeError: 'Series' object has no attribute 'n',建议从每个系列中提取值,有没有比通过循环将标称值和标准值分成两个单独的向量更短的方法?

谢谢。

编辑:使用包中的这些功能也不起作用:uncertainties.nominal_value(df['value2'])uncertainties.std_dev(df['value2'])

标签: pythonpandasuncertainty

解决方案


实际上用不确定性的 unumpy.nominal_values(arr)unumpy.std_devs(arr)函数解决了它。


推荐阅读