一个可视化随机森林结构和性能的工具
rfvis的Python项目详细描述
rfvis
用于可视化随机林的结构和性能的工具(以及基于决策树的其他集成方法)。
rfvis提供了一个命令行api和一个python api,分别在asklearn.ensemble.randomForestClassifier
开始
通过pip安装和更新rfvis
$ pip install rfvis
这将允许您在 浏览器。要从模型直接生成svg文件,还需要安装 node.js,有关详细信息,请参阅命令行界面。
命令行api
rfvis提供了一个命令行工具,可以直接从
您的输入数据(rfvis cli<;data>;
)或启动基于web的gui以获得更多
交互式分析(rfvis gui<;data>;
)。
要查看所有可用的命令运行:
$ rfvis --help
Usage: rfvis [OPTIONS] COMMAND [ARGS]...
A tool for visualizing the structure and performance of Random Forests
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
cli Command line interface to generate SVGs.
gui Web-based graphical user interface.
图形用户界面
要使用基于web的gui以交互方式分析您的林,请运行:
$ rfvis gui /path/to/data
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
现在,您可以打开浏览器http://localhost:8080" rel="nofollow">http://localhost:8080来查看类似的内容:
命令行界面
要使用命令行界面(cli),您需要 在您的系统上安装了node.jsv8+。这个 是由于渲染是用 JavaScript。不过,您不需要安装任何其他软件包,cli 集成到已通过pip安装的命令行工具中:
$ rfvis cli /path/to/data
>> Exported "/dev/random-forest-visualization/tree-0.svg"
>> Exported "/dev/random-forest-visualization/tree-1.svg"
>> Exported "/dev/random-forest-visualization/tree-2.svg"
>> Exported "/dev/random-forest-visualization/tree-3.svg"
...
使用--help
:
$ rfvis cli --help
Usage: rfvis cli [OPTIONS] FOREST_JSON
Web-based graphical user interface.
As Python is unable to render React components, we make a subprocess call to a small
Node.js application which will do the rendering and also store the created SVG
files. This command requires that Node.js is installed on your system!
FOREST_JSON: Path to the JSON file that contains the forest's data.
Options:
-o, --out PATH Output path of the SVG files. [default: (current
working directory)]
-w, --width INTEGER Width of the SVG. [default: 800]
-h, --height INTEGER Height of the SVG. [default: 800]
--trunk-length INTEGER Length of the trunk which influences the overall tree
size. [default: 100]
--display-depth INTEGER Maximum depth of the tree rendering. Cut of leaves are
visualized as pie chart consolidation nodes.
--branch-color [Impurity] Coloring of the branches. [default: Impurity]
--leaf-color [Impurity|Best Class]
Coloring of the leaves. [default: Impurity]
--help Show this message and exit.
输入数据
命令行api的数据必须作为json文件在文件系统中可用 对于林和每个树另外一个csv文件。两种数据格式都将 在将来使用属性进行扩展,这只是最小的集合。
您可以在examples/polsar
下找到一个工作示例。
森林json
mainforest.json
保存有关集成模型的所有信息:
- 名称(字符串):您的林的名称,将显示在图形用户界面中
- 错误(浮点):的错误(例如,袋外或验证错误) 整个集成模型将显示在图形用户界面中
- n_samples(int):在模型上训练的样本数
- 相关矩阵(float[]]):单树之间的相关性在内部
模型。具有维数
nxn
其中n
是树的数量。 这将用于计算森林地图。 - 类:输出类
- 名称(字符串):类的名称
- 颜色(int,int,int):0-255范围内的rgb值,其中 确定可视化中类的颜色
- 树:森林中的树
- 错误(浮动):错误(同样可能是袋外或 单个树的验证错误
- 数据(字符串):包含树数据的csv文件的相对路径
树csv
对于forest.json
rfvis中指定的每个树,都需要一个csv文件,其中
条目表示树中的一个节点。条目的格式如下:
- id(int):节点的id
- 深度(int)树中节点的深度(从
0
开始) - n节点样本(int):到达节点的训练样本数
- 杂质(float):节点的杂质(
0
-1
) - 值(int[]):节点内的类分布,即每个条目
表示节点中响应特定
班级。索引对应于
forest.classes
中的索引。
python api
rfvis还提供了一个python api,它直接在scikit-learn-randomforestclassifier上工作。
您可以在examples/scikit_learn.py
下找到一个工作示例。
函数rfvis.gui()
在基于web的图形用户界面中可视化一个合适的randomForestClassifier。
服务器在一个单独的进程中运行,可在http://localhost:<;port>;
gui(model,data=None,target=None,name=None,class_names=None,class_colors=None,port=8080)
- 模型(sklearn.ensemble.randomForestClassifier): 要可视化的模型。
- 数据(类似数组,shape=(n_samples,n_features)):用于拟合模型的训练输入样本。 用于计算单个树的包外误差和相关性。 如果没有提供,森林视图将没有意义。
- 目标(类似数组,shape=n_samples):用于拟合模型的目标值(类标签)。 用于计算单个树的包外误差和相关性。 如果没有提供,森林视图将没有意义。
- 名称(str):将显示在前端的模型的可选名称。
- 类名(list[str]):目标类名称的可选列表
- 类颜色(list[str]):目标类的浏览器可解释颜色的可选列表。 请参见https://developer.mozilla.org/en-us/docs/web/css/color\u value
- 端口(int):前端将在其上运行的端口。默认值为8080。
返回:
- 进程(multiprocessing.process):运行服务器的子进程。可以用 process.terminate()
开发
存储库包含一个pipfile
用于方便地创建virtualenv
为了发展。只需安装pipenv
运行:
$ pipenv install
您现在可以在默认端口8080上通过:
$ pipenv run rfvis gui <path_to_forest_json>
请注意,您需要先构建前端包,然后才能
实际查看应用程序处理http://localhost:8080
要构建前端,需要安装node.js。首先安装全部
通过运行以下命令开发依赖项
在/rfvis/client
目录中:
$ npm install
现在您可以通过:
$ pip install rfvis
0
如果运行了python服务器,现在应该可以看到 应用程序位于http://localhost:8080
为了更方便地在前端进行开发,请运行:
$ pip install rfvis
1
在http://localhost:3000