0

I'm trying to do some plots of some symbolic data. I have some expression from a regression in the form:

expr =  '(((((((((1.0)*(2.0)))-(ER)))-(-0.37419122066665467))*0.006633039574629684)*(0.006633039574629684*((((T)-(((1.0)+(P)))))-(P))))+0.1451920626347467)'

Where expr here is some prediction: f = f(T, P, ER). I know this particular example is a crazy expression but it's not really super important. Basically, supposing I have some dataframe, plotdata, I am trying to produce plots with:

import pandas
import sympy
import numexpr
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

expr =  '(((((((((1.0)*(2.0)))-(ER)))-(-0.37419122066665467))*0.006633039574629684)*(0.006633039574629684*((((T)-(((1.0)+(P)))))-(P))))+0.1451920626347467)'

#Extract some data for surface plot but fixing one variable
plotdata = plotdata.loc[(plotdata.P == 1)]

#Extract data as lists for plotting
x = list(plotdata['T'])
y = list(plotdata['ER'])
f_real = list(plotdata['f'])

T_sympy = sympy.Symbol('T')
P_sympy = sympy.Symbol('P')
ER_sympy = sympy.Symbol('ER')
f_pred = numexpr.evaluate(expr)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(x,y,f_real, alpha = 0.3)
ax.plot_surface(x,y,f_pred)

However, I am getting an error with f_pred.

numexpr.evaluate(expr)
Traceback (most recent call last):

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 744, in getArguments
    a = local_dict[name]

KeyError: 'ER'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-100-c765b0f1e5ce>", line 1, in <module>
    numexpr.evaluate(expr)

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 818, in evaluate
    arguments = getArguments(names, local_dict, global_dict)

  File "/anaconda3/lib/python3.7/site-packages/numexpr/necompiler.py", line 746, in getArguments
    a = global_dict[name]

KeyError: 'ER'

I am not super familiar with the numexpr package. However, I have been building this up from a 1D regression to now a 3D regression. ER was my 1D variable and was working fine. I have obviously slightly altered my code since the 1D case but I am still slightly at a loss as to why this error is popping up.

Any pointers would be greatly appreciated.

  • I don't see `expr` in your code. Your talk about a building up from a working 1d case doesn't help us (help you), since you don't show the working case or identify what's new. Remember, **you** have the code, working or not, not us! – hpaulj Nov 10 '20 at 16:51
  • Yeah, good points. I'm not sure how to present the changes clearly and succinctly is the thing. The actual code is somewhat long and involved. However, the only real change should be that there are two more symbols, `T` and `P` on top of the `ER` that was originally in the expression. Indeed, the rest of the code works fine, it is just this one call that I am using for plotting that is breaking down. `expr` is presented in the top box though. I'll edit my question to make it clearer. – imwellhowareyou Nov 10 '20 at 20:38

1 Answers1

0

I've figured it out. Pretty silly error in the end. I needed to change:

#Extract data as lists for plotting
x = list(plotdata['T'])
y = list(plotdata['ER'])

to:

T = list(plotdata['T'])
ER = list(plotdata['ER'])
P = list(plotdata['P'])

i.e. numexpr.evaluate was looking for the input data, not the symbol!