Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a <IPython.core.display.HTML object> in spyder IPython console?

I am trying to run the code:

perm = PermutationImportance(clf).fit(X_test, y_test)
eli5.show_weights(perm)

to get an idea of which features are the most important in a model, but the output is

<IPython.core.display.HTML object> 

Any solutions or workarounds to this problem?

Thank you for your suggestions!

like image 843
Evan Avatar asked Feb 27 '19 14:02

Evan


People also ask

How do you display output on a Spyder?

We can see the output in the Python Console [Show IPython console] as well as the path of the file that we are running and the working directory where this code was run. We can also run any Python code that is entered directly in the IPython Console. For example, we can type print("Hello") and see the output.

How do I use IPython console on Spyder?

Connecting to a console. Spyder can launch new IPython instances itself, through “Open an IPython console” under the Consoles menu, the IPython Console pane menu or its context menu ( Ctrl - T by default), to take advantage of the full suite of Spyder's features.

Can Spyder run HTML?

(Spyder maintainer here) The Spyder IPython console doesn't support html output, so the above code doesn't work on it. Save this answer. Show activity on this post. Save this answer.

What is IPython display in Python?

IPython (Interactive Python) is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history.


3 Answers

(Spyder maintainer here) There are no workarounds nor solutions available at the moment (February 2019) to display web content in our consoles, sorry.

Note: We are considering how to make this possible, but most probably it won't be available until 2023.

like image 130
Carlos Cordoba Avatar answered Oct 28 '22 19:10

Carlos Cordoba


A kiudge is to just display the HTML:

with open('C:\Temp\disppage.htm','wb') as f:   # Use some reasonable temp name
    f.write(htmlobj.html.encode("UTF-8"))

# open an HTML file on my own (Windows) computer
url = r'C:\Temp\disppage.htm'
webbrowser.open(url,new=2)
like image 30
J Hudock Avatar answered Oct 28 '22 20:10

J Hudock


Thanks for the idea J Hudok. The following is my working example

from sklearn.datasets import load_iris
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import eli5
from eli5.sklearn import PermutationImportance
from sklearn.model_selection import train_test_split
import webbrowser

# Load iris data & convert to dataframe
iris_data = load_iris()
data = pd.DataFrame({
    'sepal length': iris_data.data[:,0],
    'sepal width': iris_data.data[:,1],
    'petal length': iris_data.data[:,2],
    'petal width': iris_data.data[:,3],
    'species': iris_data.target
})
X = data[['sepal length', 'sepal width', 'petal length', 'petal width']]
y = data['species']

# Split train & test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# Initialize classifier
clf = RandomForestClassifier(n_estimators=56, max_depth=8, random_state=1, verbose=1)
clf.fit(X_train, y_train)

# Compute permutation feature importance
perm_importance = PermutationImportance(clf, random_state=0).fit(X_test, y_test)

# Store feature weights in an object
html_obj = eli5.show_weights(perm_importance, feature_names = X_test.columns.tolist())

# Write html object to a file (adjust file path; Windows path is used here)
with open('C:\\Tmp\\Desktop\iris-importance.htm','wb') as f:
    f.write(html_obj.data.encode("UTF-8"))

# Open the stored HTML file on the default browser
url = r'C:\\Tmp\\Desktop\iris-importance.htm'
webbrowser.open(url, new=2)
like image 4
Ziaul Chowdhury Avatar answered Oct 28 '22 21:10

Ziaul Chowdhury