cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
lucy_luo_conoco
Level III

error when run python code(created through JMP formula depot )

JMP model would give me a formula and I want pass the formula and used it in python or R program.

Through formula depot, it generated Python code, and I tried to copy the python code and run it through Jupiter Notebook, but always give me error :

for example, I attached the test file and python code created from JMP and run. it didn't recognize  as array... please help. thanks!!! I thought it is easy to copy JMP created python code and run python with no problem. looks it is not.....

 

def score(indata, outdata):
_temp_0 = u""

if (indata[u"Num"] > 50):
_temp_0 = u"High"
else:
_temp_0 = u"Low"
outdata[u"class"] = _temp_0

return outdata[u"class"]

 

 

 890         raise ValueError("The truth value of a {0} is ambiguous. "
    891                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 892                          .format(self.__class__.__name__))    893 

 

the following is python code from JMP

from __future__ import division
from math import *
import jmp_score as jmp


""" ==================================================================
Copyright(C) 2018 SAS Institute Inc.All rights reserved.

Notice:
The following permissions are granted provided that the
above copyright and this notice appear in the score code and
any related documentation.Permission to copy, modify
and distribute the score code generated using
JMP(R) software is limited to customers of SAS Institute Inc. ("SAS")
and successive third parties, all without any warranty, express or
implied, or any other obligation by SAS.SAS and all other SAS
Institute.Inc.product and service names are registered
trademarks or trademarks of SAS Institute Inc.in the USA
and other countries.Except as contained in this notice,
the name of the SAS Institute Inc. and JMP shall not be used in
the advertising or promotion of products or services without
prior written authorization from SAS Institute Inc.
================================================================== """

""" Python code generated by JMP v13.2.1 """

def getModelMetadata():
return {"creator": u"Column class", "modelName": u"", "predicted": u"", "table": u"Sheet2", "version": u"13.2.1", "timestamp": u"2018-04-05T16:10:16Z"}


def getInputMetadata():
return {
u"Num": "float"
}


def getOutputMetadata():
return {
u"class": "str"
}


def score(indata, outdata):
_temp_0 = u""

if (indata[u"Num"] > 50):
_temp_0 = u"High"
else:
_temp_0 = u"Low"
outdata[u"class"] = _temp_0

return outdata[u"class"]

 

the data is looks like

Num Class

15 Low
17 Low
200 High
60 High
72 High
10 Low
20 Low
70 High
55 High
20 Low
854 High

 

14 REPLIES 14
nascif_jmp
Level VI

Re: error when run python code(created through JMP formula depot )

Hi @eranraz1,

 

I found two issues with the code you posted:

1) You provided a string as an input value for DESIGN_LAYERS. The scoring code expects a float, as stated in the getInputMetadata() method; the value should not be quoted.

indata_aa =  {'DESIGN_LAYERS': '3','EXISTS_LASER_DRILL' : 'YES' } 

should be instead:

indata_aa =  {'DESIGN_LAYERS': 3,'EXISTS_LASER_DRILL' : 'YES' } 

2) The return line at the end of the scoring method is missing an subscription so that the value is added to a dictionary element, not to the (output) dictionary itself. If this is the original generated code, it is a bug. Maybe it is a change introduced by mistake while creating the test script.

 outdata = 358.593374276362 + 26.8517927560789 * indata[u"DESIGN_LAYERS"] + _temp_0

should be

 outdata[u"Add"] = 358.593374276362 + 26.8517927560789 * indata[u"DESIGN_LAYERS"] + _temp_0

With these changes, (and after adding a Numpy import statement) the script works and returns a result:

***************************
569.3520768491067
[Finished in 0.3s]

See attached for the complete, updated script.

PS: Next time, it would be better to open a new discussion and refer back to this 2-year old post for context.

 

eranraz1
Level II

Re: error when run python code(created through JMP formula depot )

Thank you very much for the quick and detailed response ,

it's working great now

thanks

Eran

haviernick
Level I

Re: error when run python code(created through JMP formula depot )

Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.

example

5 == pd.Series([12,2,5,10])

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you'll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(5 == pd.Series([12,2,5,10])).all()
# False

or

(5 == pd.Series([12,2,5,10])).any()
# True

 

lucy_luo_conoco
Level III

Re: error when run python code(created through JMP formula depot )

Nascif, https://community.jmp.com/t5/Discovery-Summit-2016/Scoring-Outside-the-Box/ta-p/22381 the example in this presentation really help me a lot. Great presentation and thank you so much for sharing the example and code.
nascif_jmp
Level VI

Re: error when run python code(created through JMP formula depot )

So glad it helped, Lucy!