Unable to output anything after Execute Python operator

AndrewChin99
AndrewChin99 New Altair Community Member
edited November 5 in Community Q&A
I am trying to make a lasso regression plot with my dataset. However, it just does not show anything for my output in my Execute Python operator. My code is supposed to return the coefficient of the regression.

There is no way for me to connect my Python block with any other operators as the other inputs are all expecting ExampleSet but Python can only output as PythonNativeObject.  I have tried connecting with Execute R to convert it into ExampleSet but to no avail as well. Any suggestions? This is my RMP File. 

Answers

  • Roland Jones_21245
    Roland Jones_21245
    Altair Employee
    Hi @AndrewChin99,

    The extra step was that you were trying to output a numpy array. I've built a quick example where you convert it to a pandas dataframe and now you will get an output. Of course within the Execute Python you can adjust the output, this is just to get you started.

    Best,
    Roland

    <?xml version="1.0" encoding="UTF-8"?><process version="10.3.001">
    
    <context>
    <input/>
    <output/>
    <macros/>
    </context>
    <operator activated="true" class="process" compatibility="10.3.001" expanded="true" name="Process">
    <parameter key="logverbosity" value="init"/>
    <parameter key="random_seed" value="2001"/>
    <parameter key="send_mail" value="never"/>
    <parameter key="notification_email" value=""/>
    <parameter key="process_duration_for_mail" value="30"/>
    <parameter key="encoding" value="SYSTEM"/>
    <process expanded="true">
    <operator activated="true" class="read_csv" compatibility="10.2.000" expanded="true" height="68" name="Read CSV (4)" width="90" x="45" y="34">
    <parameter key="csv_file" value="C:/Users/rjones/Downloads/airplane_cleaned.csv"/>
    <parameter key="column_separators" value=";"/>
    <parameter key="trim_lines" value="false"/>
    <parameter key="multiline_text" value="false"/>
    <parameter key="use_quotes" value="true"/>
    <parameter key="quotes_character" value="&quot;"/>
    <parameter key="escape_character" value="\"/>
    <parameter key="skip_comments" value="true"/>
    <parameter key="comment_characters" value="#"/>
    <parameter key="starting_row" value="1"/>
    <parameter key="parse_numbers" value="true"/>
    <parameter key="decimal_character" value="."/>
    <parameter key="grouped_digits" value="false"/>
    <parameter key="grouping_character" value=","/>
    <parameter key="infinity_representation" value=""/>
    <parameter key="date_format" value=""/>
    <parameter key="use_header_row" value="true"/>
    <parameter key="header_row" value="1"/>
    <parameter key="first_row_as_names" value="true"/>
    <list key="annotations"/>
    <parameter key="time_zone" value="SYSTEM"/>
    <parameter key="locale" value="English (United States)"/>
    <parameter key="encoding" value="windows-1252"/>
    <parameter key="read_all_values_as_polynominal" value="false"/>
    <list key="data_set_meta_data_information">
    <parameter key="0" value="Gender.true.polynominal.attribute"/>
    <parameter key="1" value="Age.true.real.attribute"/>
    <parameter key="2" value="Customer Type.true.polynominal.attribute"/>
    <parameter key="3" value="Type of Travel.true.polynominal.attribute"/>
    <parameter key="4" value="Class.true.polynominal.attribute"/>
    <parameter key="5" value="Flight Distance.true.real.attribute"/>
    <parameter key="6" value="Departure and Arrival Time Convenience.true.real.attribute"/>
    <parameter key="7" value="Ease of Online Booking.true.real.attribute"/>
    <parameter key="8" value="Check-in Service.true.real.attribute"/>
    <parameter key="9" value="Online Boarding.true.real.attribute"/>
    <parameter key="10" value="Gate Location.true.real.attribute"/>
    <parameter key="11" value="On-board Service.true.real.attribute"/>
    <parameter key="12" value="Seat Comfort.true.real.attribute"/>
    <parameter key="13" value="Leg Room Service.true.real.attribute"/>
    <parameter key="14" value="Cleanliness.true.real.attribute"/>
    <parameter key="15" value="Food and Drink.true.real.attribute"/>
    <parameter key="16" value="In-flight Service.true.real.attribute"/>
    <parameter key="17" value="In-flight Wifi Service.true.real.attribute"/>
    <parameter key="18" value="In-flight Entertainment.true.real.attribute"/>
    <parameter key="19" value="Baggage Handling.true.real.attribute"/>
    <parameter key="20" value="Satisfaction.true.integer.attribute"/>
    </list>
    <parameter key="read_not_matching_values_as_missings" value="false"/>
    </operator>
    <operator activated="true" class="python_scripting:execute_python" compatibility="10.0.001" expanded="true" height="103" name="Execute Python (2)" width="90" x="179" y="34">
    <parameter key="script" value="import pandas as pd&#10;from sklearn.linear_model import Lasso&#10;from sklearn.model_selection import train_test_split&#10;from sklearn.metrics import mean_squared_error&#10;&#10;def rm_main(data):&#10; # Load your data&#10; target_column = 'Satisfaction'&#10; feature_columns = ['Age', 'Flight Distance', 'In-flight Wifi Service', &#10; 'Departure and Arrival Time Convenience', 'Ease of Online Booking', 'Gate Location',&#10; 'Food and Drink', 'Online Boarding', 'Seat Comfort',&#10; 'In-flight Entertainment', 'On-board Service', 'Leg Room Service',&#10; 'Baggage Handling', 'Check-in Service', 'In-flight Service', 'Cleanliness']&#10;&#10; # Split the data into training and testing sets&#10; X_train, X_test, y_train, y_test = train_test_split(data[feature_columns], data[target_column], test_size=0.2, random_state=42)&#10;&#10; # Initialize the Lasso Regression model&#10; lasso_model = Lasso(alpha=0.1)&#10;&#10; # Fit the model on the training data&#10; lasso_model.fit(X_train, y_train)&#10;&#10; # Make predictions on the test data&#10; predictions = lasso_model.predict(X_test)&#10;&#10; # Create a data frame with the results&#10; results_df = pd.DataFrame({'Actual': y_test, 'Predicted': predictions})&#10; results_df.to_csv('output.csv', index=False)&#10;&#10; # Log the coefficients and intercept&#10; print(&quot;Coefficients:&quot;, lasso_model.coef_)&#10; print(&quot;Intercept:&quot;, lasso_model.intercept_)&#10;&#10; print(type(lasso_model.coef_))&#10;&#10; # Output the coefficients&#10; return pd.DataFrame(lasso_model.coef_)"/>
    <parameter key="notebook_cell_tag_filter" value=""/>
    <parameter key="use_default_python" value="true"/>
    <parameter key="package_manager" value="conda (anaconda)"/>
    <parameter key="use_macros" value="false"/>
    </operator>
    <connect from_op="Read CSV (4)" from_port="output" to_op="Execute Python (2)" to_port="input 1"/>
    <connect from_op="Execute Python (2)" from_port="output 1" to_port="result 1"/>
    <portSpacing port="source_input 1" spacing="0"/>
    <portSpacing port="sink_result 1" spacing="0"/>
    <portSpacing port="sink_result 2" spacing="0"/>
    </process>
    </operator>
    </process>