cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
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

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
nascif_jmp
Level VI

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

Hi Lucy,

The short answer is that you have to use a data structure to hold your data that is compatible with the code generated by JMP. It expects the input and output arguments to be of a dictionary-like type, where values can be accessed and written by key. The ideal data structure to use for a table is a Pandas DataFrame. Here is how you would add a new column to a DataFrame using the JMP score function from your example:

 

 

import pandas as pd

indata_df = pd.read_table(testdata, sep = ',') indata_df['predicted_class'] = indata_df.apply(lambda s: score(s, {}), axis=1)

Please see the attached files for a complete example. Also, please take a look at the example code attached to the Discovery presentations where the Formula Depot code generation was covered, Scoring Outside the Box and Sharing the Ultimate Boon.

 

 

View solution in original post

nascif_jmp
Level VI

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

Glad you find it helpful! I would suggest that you do not copy and paste the JMP code into the notebook - that only works for very small models and the two you mentioned can be quite large. Instead, just import the generated file as in the Discovery notebook examples I mentioned. 

 

Here is a tip: start your notebook in the same directory you saved the JMP-generated python code, as well as a copy of the helper file jmp_score.py file that comes with the product, so that Python can find them more easily. See this discussion for more details.

View solution in original post

14 REPLIES 14
nascif_jmp
Level VI

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

Hi Lucy,

The short answer is that you have to use a data structure to hold your data that is compatible with the code generated by JMP. It expects the input and output arguments to be of a dictionary-like type, where values can be accessed and written by key. The ideal data structure to use for a table is a Pandas DataFrame. Here is how you would add a new column to a DataFrame using the JMP score function from your example:

 

 

import pandas as pd

indata_df = pd.read_table(testdata, sep = ',') indata_df['predicted_class'] = indata_df.apply(lambda s: score(s, {}), axis=1)

Please see the attached files for a complete example. Also, please take a look at the example code attached to the Discovery presentations where the Formula Depot code generation was covered, Scoring Outside the Box and Sharing the Ultimate Boon.

 

 

lucy_luo_conoco
Level III

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

Nasif,

 

Thank you so much for your knowledgable and quick reply.  It works very well for this. I will try more complicated python code created by JMP (Boosted tree/Random Forest formula, although looks like take long time even copy and paste jupyter notebook) 

nascif_jmp
Level VI

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

Glad you find it helpful! I would suggest that you do not copy and paste the JMP code into the notebook - that only works for very small models and the two you mentioned can be quite large. Instead, just import the generated file as in the Discovery notebook examples I mentioned. 

 

Here is a tip: start your notebook in the same directory you saved the JMP-generated python code, as well as a copy of the helper file jmp_score.py file that comes with the product, so that Python can find them more easily. See this discussion for more details.

lucy_luo_conoco
Level III

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

yeah, it is really a good suggestion!!! I am not good at python since I have been writing SAS codes for 20 years. 

lucy_luo_conoco
Level III

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

Nasif, 

 

one more question, in the newer version, Formular Depot will generate R code? thanks!

nascif_jmp
Level VI

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

Hi again,

 

We have had requests for R language code generation but unfortunately, it is not on the product or on the roadmap yet.

I would suggest adding an entry to the JMP Wish List so that we can track the general interest in this feature.

lucy_luo_conoco
Level III

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

that would be good if JMP could add R code. especially our industry use Spotfire and R codes will be easily embeded with Spotfire. 

Thanks!

nascif_jmp
Level VI

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

I don't have access to Spotfire so it is not possible for me to try it, but apparently, it is possible to run Python code in SpotFire too.

Please take a look at these links, they might help:

https://community.tibco.com/wiki/how-deploy-custom-python-modules-use-tibco-spotfirer-clients

https://community.tibco.com/questions/call-local-python-spotfire

eranraz1
Level II

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

Hi , 

was triyng to implement the same method using a 'Dict' with 2 parameters, and got the error :

"TypeError: can't multiply sequence by non-int of type 'float'"

please assist

attached the PY code :

#set parameters
indata_aa =  {'DESIGN_LAYERS': '3','EXISTS_LASER_DRILL' : 'YES' } 
 
def getModelMetadata():
	return {"creator": u"Formula Editor", "modelName": u"", "predicted": u"", "table": u"NEW JMP Pricing 30 RIGID SHORT", "version": u"15.0.0", "timestamp": u"2020-08-30T10:46:03Z"}


def getInputMetadata():
    return {
        u"DESIGN_LAYERS": "float",
        u"EXISTS_LASER_DRILL": "str"
        
	}


def getOutputMetadata():
    return {
        u"Add": "float"
	}


def score(indata, outdata):
    _temp_0 = np.nan

    if (indata[u"EXISTS_LASER_DRILL"] == u"NO"):
        _temp_0 = -130.203324304508
    elif (indata[u"EXISTS_LASER_DRILL"] == u"YES"):
        _temp_0 = 130.203324304508
    else:
        _temp_0 = np.nan
        print (_temp_0)
    outdata = 358.593374276362 + 26.8517927560789 * indata[u"DESIGN_LAYERS"] + _temp_0

    return outdata["Add"]

print('***************************')
print(score(indata_aa, {}))

thanks 

Eran