【问题标题】:Replicate jupyter HTML output using IPython and Spyder instead改为使用 IPython 和 Spyder 复制 jupyter HTML 输出
【发布时间】:2018-04-15 00:22:12
【问题描述】:

以下 sn-p 将在 Jupyter 中产生以下输出:

display(HTML('<h2>Hello, world!</h2>'))

在 Spyder 的 IPython 控制台中运行相同的 sn-p 只会返回 &lt;IPython.core.display.HTML object&gt;,如下所示:

是否可以使用 Spyder 在 IPython 控制台中显示相同的输出? 我想我会用from IPython.core.display import display, HTML 找到某个地方,正如here 提到的那样,但我可能完全错过了这一点。

感谢您的任何建议!

【问题讨论】:

    标签: python ipython jupyter-notebook spyder


    【解决方案1】:

    Spyder 有一个集成笔记本的插件:Spyder-Notebook(我没用过)。 Pycharm 也有集成。

    【讨论】:

      【解决方案2】:

      (这里是 Spyder 维护者) Spyder IPython 控制台不支持 html 输出,所以上面的代码不起作用。

      【讨论】:

      • 感谢您的反馈!只是为了澄清;你是说我提供的sn-p,还是Igaud的建议?
      • @lgaud 的建议是关于 spyder-notebook,这是一个在 Spyder 中运行 Jupyter notebook 的插件。
      • 是否至少有一种简单的方法可以捕获对象并将其放入 matplotlib.pyplot.figure 中?
      • 我希望有更通用的东西。我正在使用天篷。例如 eli5.show_weights() 生成一个 IPython.core.display.HTML 对象,我希望它在 matplotlib 图中。很高兴通过 png 到达那里
      • 我一直想知道为什么 Spyder 和 PyCharm 仍然(主要)使用文本控制台。是否可以使用 Qt WebEngine 让 _repr_html_() 在可用时显示所有输出?这是常规 .py 文件,没有 Jupyter 笔记本。
      【解决方案3】:

      另一种想法是将排列特征重要性的输出存储到 html 文件中,并使用默认浏览器打开它。我从J Hudok 中的another thread 得到这个想法。以下是我的工作示例

      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)
      

      【讨论】:

        猜你喜欢
        • 2020-11-19
        • 2017-11-02
        • 1970-01-01
        • 2016-06-03
        • 2018-05-15
        • 2018-02-02
        • 2014-07-04
        • 1970-01-01
        • 2018-08-25
        相关资源
        最近更新 更多