Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- error when run python code(created through JMP formula depot )

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 5, 2018 9:27 AM
(3291 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.