<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python Code unable to generate output in JSL (JMP 17 Pro) in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866106#M102908</link>
    <description>&lt;P&gt;Is there a way to convert the dataframe to JMP datatable, to visualize it outside the python block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Apr 2025 03:56:02 GMT</pubDate>
    <dc:creator>dmanu221993</dc:creator>
    <dc:date>2025-04-06T03:56:02Z</dc:date>
    <item>
      <title>Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866048#M102905</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a JSL script that creates a Datatable, and I have a python code block within the JSL script which I want to pass the DataTable to make predictions on the DataTable using a trained machine learning model. Before the python code block, the DataTable is accessible and even opens in JMP. However, after the python code block when I try to access the predictions made by the neural network model based on the DataTable using "Show(preds_df)", I get error "Name Unresolved: preds_df in access or evaluation of 'preds_df'". I am using JMP Pro 17. Below is my code block for review.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV style="color: black; font-size: 12pt; font-family: Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;"&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Clear Log();
Clear Symbols();
Names Default To Here( 1 );
 
// Define the list of options for the dropdown
SITE = {"SITE_1", "SITE_2"};
MODELS = {"NN_MODEL", "RNN_MODEL"};
 
PythonConnection = Python Connect();
conn = PythonConnection &amp;lt;&amp;lt; Is Connected;
PythonConnection &amp;lt;&amp;lt; Send( dt);
close(dt);
PythonConnection &amp;lt;&amp;lt; Send(MODELS);
 
PythonConnection &amp;lt;&amp;lt; Execute("
import pandas as pd
from sklearn.preprocessing import StandardScaler
import torch
import pickle
import numpy as np
 
// Model Selection
if 'NN_MODEL' in MODELS:
      model = 'NN_MODEL'
else:
      model = 'RNN_MODEL'
 
# Extract categorical and numerical features from data
categorical_features = ['NAME', 'COUNTRY', 'SCHOOL']
numerical_features = ['AGE', 'SIZE', 'HEIGHT', 'WEIGHT']
 
# One-Hot encoding for categorical features in place
process_df = pd.get_dummies(dt, columns=categorical_features, drop_first=True)
 
 
# Normalize numerical features
scaler = StandardScaler()
new_df[numerical_features] = scaler.fit_transform(process_df[numerical_features])
 
# Exclude bin columns from the model input
B_cols = ['B1', 'B8', 'B9', 'B10']
B_columns = [col for col in B_cols if col in process_df.columns]
input_df = process_df.drop(B_columns, axis=1)
 
labels = process_df[B_cols]
ids = process_df[B_cols].index
 
# Load the trained model based on user selection
if model == 'NN_MODEL':
    model_path = '.\\nn_model.pth'
    model = torch.load(model_path)
    model.eval()
 
    # Convert DataFrame to tensor
    in_tensor = torch.tensor(input_df.values, dtype=torch.float32)
 
    # Predict using the model
    with torch.no_grad():
      nn_preds = model(in_tensor)
      nn_preds = torch.round(nn_preds).to(torch.int).numpy()
     nn_preds = np.clip(nn_preds, a_min=0, a_max=None)
    preds_df = pd.DataFrame(nn_preds.numpy(), index=ids, columns=labels)
else:
    model_path = '.\\rnn_model.pkl'
    with open(model_path, 'rb') as file:
        model = pickle.load(file)
    rnn_preds = model.predict(input_df)
    rnn_preds = np.clip(rnn_preds, a_min=0, a_max=None)
    preds_df = pd.DataFrame(rnn_preds, index=ids, columns=labels)
 "
);
 
Show(preds_df)&lt;/CODE&gt;&lt;/PRE&gt;
&amp;gt;&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sun, 06 Apr 2025 04:03:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866048#M102905</guid>
      <dc:creator>dmanu221993</dc:creator>
      <dc:date>2025-04-06T04:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866059#M102906</link>
      <description>&lt;P&gt;Very quickly looking, preds_df is just a pandas dataframe, not JMP table, so you cannot access it from JMP.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Apr 2025 20:49:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866059#M102906</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-04-05T20:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866078#M102907</link>
      <description>&lt;P&gt;Try this at the end of your JSL script:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Python Get( preds_df );
dt &amp;lt;&amp;lt; New Data View;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also JMP 18 can run Python scripts natively so no need to embed them in JSL scripts.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Apr 2025 22:53:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866078#M102907</guid>
      <dc:creator>Mark_Zwald</dc:creator>
      <dc:date>2025-04-05T22:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866106#M102908</link>
      <description>&lt;P&gt;Is there a way to convert the dataframe to JMP datatable, to visualize it outside the python block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Apr 2025 03:56:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866106#M102908</guid>
      <dc:creator>dmanu221993</dc:creator>
      <dc:date>2025-04-06T03:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866107#M102909</link>
      <description>&lt;P&gt;I got "Send Expects Scriptable Object in access or evaluation of Send, "dt" &amp;lt;&amp;lt; /*###*/New Data View/*###*/ "&lt;/P&gt;</description>
      <pubDate>Sun, 06 Apr 2025 04:00:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866107#M102909</guid>
      <dc:creator>dmanu221993</dc:creator>
      <dc:date>2025-04-06T04:00:14Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866126#M102910</link>
      <description>&lt;P&gt;You can. I don't remember if JMP17 is able to do it directly but you can for example go through a .csv file: create .csv from the pandas dataframe and then pass that path to JMP and open it. &lt;/P&gt;</description>
      <pubDate>Sun, 06 Apr 2025 06:02:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866126#M102910</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-04-06T06:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: Python Code unable to generate output in JSL (JMP 17 Pro)</title>
      <link>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866586#M102923</link>
      <description>&lt;P&gt;First the syntax for &amp;nbsp;&amp;lt;&amp;lt; Execute ( ) is wrong. &amp;nbsp;See the example in the scripting index. &amp;nbsp;The first parameter for Execute is a JSL list of inputs, followed by a JSL list of output variables, followed by the script, and then optional echo(1 or 0) parameter.&lt;/P&gt;
&lt;P&gt;The script should be delineate by JSL's 'raw' string markers "\[ &amp;nbsp;script here &amp;nbsp;]\" to ensure white space and line endings are preserved. &amp;nbsp;I am surprised you are not getting a syntax error, but I see the same lack of error on 19... I'm looking into that. &amp;nbsp;The show preds_df for me simply printed&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dt = .;&lt;/P&gt;
&lt;P&gt;which is missing. This then of course means:&lt;/P&gt;
&lt;P&gt;dt &amp;lt;&amp;lt; New Data View&lt;/P&gt;
&lt;P&gt;Throws an error because dt &amp;nbsp;( missing ) is not scriptable.&lt;/P&gt;
&lt;P&gt;The below code replicates your issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;conn = Python Connect();
conn &amp;lt;&amp;lt; Get Version();

conn &amp;lt;&amp;lt; Execute( "

import pandas as pd
import numpy as np

pandas_df = pd.DataFrame(
    {
        "A": 1.0,
        "B": pd.Timestamp("20130102"),
        "C": pd.Series(1, index=list(range(4)), dtype="float32"),
        "D": np.array([3] * 4, dtype="int32"),
        "E": pd.Categorical(["test", "train", "test", "train"]),
        "F": "foo",
    }
)
print(pandas_df)
]\", echo(1) );

dt = Python Get(pandas_df);
show(dt);
dt &amp;lt;&amp;lt; New Data View;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code runs as expected&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;conn = Python Connect();
conn &amp;lt;&amp;lt; Get Version();
conn &amp;lt;&amp;lt; Execute( {},{},"\[

import pandas as pd
import numpy as np

pandas_df = pd.DataFrame(
    {
        "A": 1.0,
        "B": pd.Timestamp("20130102"),
        "C": pd.Series(1, index=list(range(4)), dtype="float32"),
        "D": np.array([3] * 4, dtype="int32"),
        "E": pd.Categorical(["test", "train", "test", "train"]),
        "F": "foo",
    }
)
print(pandas_df)

]\", echo(1) );

dt = Python Get(pandas_df);
show(dt);
dt &amp;lt;&amp;lt; New Data View;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A couple things of note JMP 17 is the last version that utilized Python from the user's environment, rather than an embedded Python installed with JMP 18 and newer. &amp;nbsp;In JMP 14-17 the &lt;STRONG&gt;Python Send (dt);&amp;nbsp;&lt;/STRONG&gt;created a pandas dataframe in the Python environment by saving the Data Table to a temporary CSV file, then causing pandas to read that CSV file into a dataframe.&amp;nbsp;Python Get( df ); behaved analogously buy having pandas write the dataframe to a CSV file and have JMP import the CSV file. &amp;nbsp;This meant that you had 2 copies of the data the JMP data table, and the pandas dataframe. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This changed in JMP 18 with the introduction of the embedded Python 3.11.x and a builtin&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;jmp&lt;/STRONG&gt;&lt;/EM&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;import package that provides a jmp.DataTable class. This introduces an in-memory live reference to the JMP data table accessible from Python. &amp;nbsp;Python Send( dt ); in JMP 18+ now creates a jmp.DataTable object instead of a pandas datagram. &amp;nbsp;dt2 = Python Get( dt ) just gets the data table reference assigning to the variable. &amp;nbsp;dfdt = Python Get( data_frame ); still uses the old mechanism to convert and load the dataframe as a data table via CSV file. &amp;nbsp;In JMP 18 there is a Python sample script in the scripts directory shown how to do the original dt-&amp;gt;csv-&amp;gt;df &amp;nbsp;and df-&amp;gt;csv-&amp;gt;dt conversions. &amp;nbsp;JMP 19 supports the Portable Dataframe Protocol so it's a one-liner to go dt-&amp;gt;df or df-&amp;gt;dt in memory. &amp;nbsp;Samples named dt2pandas.jsl, dt2pands.py, dt2pandas2dt.py&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For JMP 17:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

// I have multiple Python's on system so I choose 3.10 where I have 
// matplotlib, pandas, numpy, ... installed
Python Init(Use Python Version("3.10"));
Python Send(dt);
close(dt);

dtdf = Python Get("dt");
Show(dtdf);
dtdf &amp;lt;&amp;lt; New Data View;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works fine on my machine. &amp;nbsp;If you can't get this to run please examine your Log to see if you are showing errors. Modifying Python Init() either for your environment if necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;JMP 19&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;conn = Python Connect();
conn &amp;lt;&amp;lt; Get Version();

conn &amp;lt;&amp;lt; Execute( {},{},"\[
import jmp

import pandas as pd
import numpy as np

pandas_df = pd.DataFrame(
    {
        "A": 1.0,
        "B": pd.Timestamp("20130102"),
        "C": pd.Series(1, index=list(range(4)), dtype="float32"),
        "D": np.array([3] * 4, dtype="int32"),
        "E": pd.Categorical(["test", "train", "test", "train"]),
        "F": "foo",
    }
)
print( pandas_df )

# JMP 19 create a data table direcly from a dataframe
dt = jmp.from_dataframe(pandas_df)
print( dt )
]\", echo(1) );

show(dt);
// dt &amp;lt;&amp;lt; New Data View;   // already on screen&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Much more can be done with just running the Python script from the editor no JSL wrapper needed in JMP 18+&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Apr 2025 15:31:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Python-Code-unable-to-generate-output-in-JSL-JMP-17-Pro/m-p/866586#M102923</guid>
      <dc:creator>Paul_Nelson</dc:creator>
      <dc:date>2025-04-07T15:31:10Z</dc:date>
    </item>
  </channel>
</rss>

