【发布时间】:2021-06-01 09:13:55
【问题描述】:
我的目标是标准化下面Norm1 表示的概率密度函数。当我将方程直接导入ax.contour 时它有效,但在外部调用方程时无效。
尝试将 Norm1 传递到 ax.contour 时,我收到以下错误:
raise TypeError(f"Input z must be 2D, not {z.ndim}D")
TypeError: Input z must be 2D, not 3D
对于上下文,我有两个单独的组,其中包含不同的 XY 点。每个 XY 点都有一个标记为 id 的标识符。我为每个独特的点分配一个圆或半径。我想从每组中减去PDF。输出是一个介于 0 到 1 之间的归一化 PDF,其中 >0.5 由 A 组控制,
df = pd.DataFrame({'Int_1': [1.0, 2.0, 1.0, 3.0, 1.0, 2.0, 3.0, 2.0],
'Int_2': [1.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 2.0],
'Item_X': [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0],
'Item_Y': [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0],
'Period': [1, 1, 1, 1, 2, 2, 2, 2],
'Group': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
'Item': ['Y', 'Y', 'A', 'B', 'A', 'B', 'A', 'B'],
'id': ['1', '2', '3', '4', '1', '2', '3', '4']})
Group_A = [df[df['Group'] == 'A'][['Int_1','Int_2']].to_numpy()]
Group_B = [df[df['Group'] == 'B'][['Int_1','Int_2']].to_numpy()]
Item = [df[['Item_X','Item_Y']].to_numpy()]
period = df['Period'].drop_duplicates().reset_index(drop = True)
def impact_func(member_no, location, time_index, group):
if group == 'A':
data = Group_A.copy()
elif group == 'B':
data = Group_B.copy()
else:
return
if np.all(np.isfinite(data[member_no][[time_index,time_index + 1],:])) & np.all(np.isfinite(Item[0][time_index,:])):
sxy = (data[member_no][time_index + 1,:] - data[member_no][time_index,:]) / (period[time_index + 1] - period[time_index])
mu = data[member_no][time_index,:] + sxy * 0.5
out = mvn.pdf(location,mu) / mvn.pdf(data[member_no][time_index,:],mu)
else:
out = np.zeros(location.shape[0])
return out
xx,yy = np.meshgrid(np.linspace(-10,10,200),np.linspace(-10,10,200))
Z_GA = np.zeros(40000)
Z_GB = np.zeros(40000)
for k in range(1):
Z_GA += impact_func(k,np.c_[xx.flatten(),yy.flatten()],0,'A')
Z_GB += impact_func(k,np.c_[xx.flatten(),yy.flatten()],0,'B')
fig, ax = plt.subplots(figsize=(8,8))
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
Z_GA = Z_GA.reshape((200,200))
Z_GB = Z_GB.reshape((200,200))
Norm1 = xx,yy, 1 / (1 + np.exp(Z_GB - Z_GA))
Norm2 = Z_GA / (Z_GA + Z_GB)
#cfs = ax.contourf(Norm1, cmap = 'magma')
cfs = ax.contourf(xx,yy, 1 / (1 + np.exp(Z_GB - Z_GA)), cmap = 'magma')
fig.colorbar(cfs, ax = ax)
【问题讨论】:
-
嗨@chopin,我已经尝试运行代码,但它缺少 Z(第 78 行)和 normPDF()(第 82 行)。你能解决这个问题吗?
-
我的错,一切正常。
-
也许您可以提供更多信息? “空间影响”是什么意思?在这里很难说出您要建模的内容。
-
@他,现在怎么样?
标签: python pandas spatial gaussian probability-density