SPSS+AMOS数据分析案例教程-关于中介模
SPSS视频教程内容目录和跳转链接
Meta分析辅导+代找数据
SPSS+AMOS数据分析案例教程-关于中介模
SPSS视频教程内容目录和跳转链接
R语言快速入门视频教程
Python智联招聘数据分析
LCA潜在类别分析和Mplus应用
Amos结构方程模型数据分析入门教程
倒U关系回归分析中介效应和调节效应分析SPSS视频教程
统计咨询(图文问答)

pydotplus安装和基本入门

在B站@mlln-cn, 我就能回答你的问题奥!

文章目录
  1. 1. 下载Graphviz
  2. 2. 安装Graphviz
  3. 3. 配置环境变量
  4. 4. 安装pydotplus
  5. 5. 引入模块
  6. 6. 绘制第一幅图
  7. 7. dot语法入门
    1. 7.1. 基本用法
    2. 7.2. 可用形状
    3. 7.3. 可用颜色
    4. 7.4. 箭头类型
  8. 8. 终极案例-绘制决策树

本文代码运行环境:

  • windows10
  • python3.6
  • jupyter notebook

下载Graphviz

pydotplus依赖Graphviz这个绘图库, 它是c开发的, 所以在安装pydotplus之前, 需要首先安装Graphviz, 不过不管哪个系统, 它都有编译好的二进制包, 可以直接安装。以windows为例, 我们首先需要现在Graphviz安装包: 点此下载.

然后找到Stable 2.38 Windows install packages, 这是稳定版的, 点击进入以后, 下载graphviz-2.38.msi即可。文件大概由34M。

安装Graphviz

安装过程很简单, 只要双击安装包, 一路下一步就行了。

配置环境变量

pydotplus实际上是在命令行调用Graphviz, 所以要想python能找到graphviz的安装路径, 必须配置环境变量。

通常, graphviz默认安装在这个路径: C:\Program Files (x86)\Graphviz2.38

我们需要将两个路径添加到环境变量中:

  • C:\Program Files (x86)\Graphviz2.38
  • C:\Program Files (x86)\Graphviz2.38\bin

安装pydotplus

pydotplus是python模块, 它的安装和安装普通的模块一样, 就是使用pip:

1
pip install pydotplus

因为本篇教程写在jupyter notebook 中, 可以在cell中安装pydotplus, 所以可以直接使用:

%pip install pydotplus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Looking in indexes: https://mirrors.ustc.edu.cn/pypi/web/simple
Collecting pydotplus
Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/60/bf/62567830b700d9f6930e9ab6831d6ba256f7b0b730acb37278b0ccdf
facf/pydotplus-2.0.2.tar.gz (278kB)
100% |████████████████████████████████| 286kB 560kB/s
Requirement already satisfied: pyparsing>=2.0.1 in d:\mysites\deeplearning.ai-master\.env\lib\site-packages (from pydotplu
s) (2.2.0)
Building wheels for collected packages: pydotplus
Running setup.py bdist_wheel for pydotplus ... done
Stored in directory: C:\Users\syd\AppData\Local\pip\Cache\wheels\27\e9\b6\40e2817c77acbd8ac9638ac4fd03dc3c34ea2fd8cbd762
2bb4
Successfully built pydotplus
Installing collected packages: pydotplus
Successfully installed pydotplus-2.0.2

引入模块

1
import pydotplus as pdp

因为我们的代码运行环境是jupyter notebook, 所以我们可以设置让图片实时展示在notebook中:

1
%matplotlib inline

绘制第一幅图

1
2
3
from IPython.display import display, Image
graph = pdp.graph_from_dot_data('digraph demo1{ a -> b -> c; c ->a; }')
display(Image(graph.create_png()))

png

dot语法入门

基本用法

  • 使用双斜杠注释
1
// 这里是注释
  • 指定有向图和无向图
1
2
3
4
5
6
// 有向图
digraph g {
a->b;
b->c;
c->a;
}
1
2
3
4
5
6
// 无向图
graph g {
a--b;
b--c;
c--a;
}
  • 节点之间的关系使用--或者->表示节点间关系

  • 定义属性使用node[attribute1=value1, attribute2=value2]

1
2
//定义a节点为长方形, 节点显示的文本为"Hello world"样式为填充, 填充颜色为#ABACBA
a[shape=box,label="Hello world",style=filled,fillcolor="#ABACBA"];
  • 定义节点形状使用node[shape=形状名称]

使用上面提到的功能, 绘制下面的图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
dot = '''
//定义节点属性
digraph g {
//==========定义节点关系============
a->b;
b->c;
c->a;
c->d->e->f;
d->g;
e->h;
//==========定义节点属性============
//定义a节点为长方形, 样式为填充, 填充颜色为#ABACBA
a[shape=box,label="Server1\nWebServer",fillcolor="#ABACBA",style=filled];
//定义b为5边形, 标签为"bb", 样式为填充, 填充色为red
b[shape=polygon,sides=5,label="bb",style=filled,fillcolor=red];
//c, 默认为椭圆
d[shape=circle]; //园
e[shape=triangle]; //三角形
f[shape=polygon, sides=4, skew=0.5]; //平行四边形
g[shape=polygon, distortion=0.5]; //梯形, 上边长
h[shape=polygon, distortion=-.5]; //梯形, 下边长
}
'''

graph = pdp.graph_from_dot_data(dot)
# 下面这行用于在notebook中显示图片
Image(graph.create_png())

png

可用形状

下面是可以在shape属性中使用的:

box polygon ellipse oval
circle point egg triangle
plaintext plain diamond trapezium
parallelogram house pentagon hexagon
septagon octagon doublecircle doubleoctagon
tripleoctagon invtriangle invtrapezium invhouse
Mdiamond Msquare Mcircle rect
rectangle square star none
underline cylinder note tab
folder box3d component promoter
cds terminator utr primersite
restrictionsite fivepoverhang threepoverhang noverhang
assembly signature insulator ribosite
rnastab proteasesite proteinstab rpromoter
rarrow larrow lpromoter

可用颜色

下面是可以在属性color中使用的:

aliceblue antiquewhite antiquewhite1 antiquewhite2 antiquewhite3
antiquewhite4 aquamarine aquamarine1 aquamarine2 aquamarine3
aquamarine4 azure azure1 azure2 azure3
azure4 beige bisque bisque1 bisque2
bisque3 bisque4 black blanchedalmond    blue   
blue1 blue2 blue3 blue4 blueviolet
brown brown1 brown2 brown3 brown4
burlywood burlywood1 burlywood2 burlywood3 burlywood4
cadetblue cadetblue1 cadetblue2 cadetblue3 cadetblue4
chartreuse chartreuse1 chartreuse2 chartreuse3 chartreuse4
chocolate chocolate1 chocolate2 chocolate3 chocolate4
coral coral1 coral2 coral3 coral4
cornflowerblue cornsilk cornsilk1 cornsilk2 cornsilk3
cornsilk4 crimson    cyan    cyan1 cyan2
cyan3 cyan4 darkgoldenrod darkgoldenrod1 darkgoldenrod2
darkgoldenrod3 darkgoldenrod4 darkgreen darkkhaki darkolivegreen
darkolivegreen1 darkolivegreen2 darkolivegreen3 darkolivegreen4 darkorange
darkorange1 darkorange2 darkorange3 darkorange4 darkorchid
darkorchid1 darkorchid2 darkorchid3 darkorchid4 darksalmon
darkseagreen darkseagreen1 darkseagreen2 darkseagreen3 darkseagreen4
darkslateblue darkslategray darkslategray1 darkslategray2 darkslategray3
darkslategray4 darkslategrey darkturquoise darkviolet deeppink
deeppink1 deeppink2 deeppink3 deeppink4 deepskyblue
deepskyblue1 deepskyblue2 deepskyblue3 deepskyblue4 dimgray
dimgrey dodgerblue dodgerblue1 dodgerblue2 dodgerblue3
dodgerblue4 firebrick firebrick1 firebrick2 firebrick3
firebrick4 floralwhite forestgreen gainsboro ghostwhite
   gold    gold1 gold2 gold3 gold4
goldenrod goldenrod1 goldenrod2 goldenrod3 goldenrod4
   gray    gray0 gray1 gray10 gray100
gray11 gray12 gray13 gray14 gray15
gray16 gray17 gray18 gray19 gray2
gray20 gray21 gray22 gray23 gray24
gray25 gray26 gray27 gray28 gray29
gray3 gray30 gray31 gray32 gray33
gray34 gray35 gray36 gray37 gray38
gray39 gray4 gray40 gray41 gray42
gray43 gray44 gray45 gray46 gray47
gray48 gray49 gray5 gray50 gray51
gray52 gray53 gray54 gray55 gray56
gray57 gray58 gray59 gray6 gray60
gray61 gray62 gray63 gray64 gray65
gray66 gray67 gray68 gray69 gray7
gray70 gray71 gray72 gray73 gray74
gray75 gray76 gray77 gray78 gray79
gray8 gray80 gray81 gray82 gray83
gray84 gray85 gray86 gray87 gray88
gray89 gray9 gray90 gray91 gray92
gray93 gray94 gray95 gray96 gray97
gray98 gray99 green green1 green2
green3 green4 greenyellow    grey    grey0
grey1 grey10 grey100 grey11 grey12
grey13 grey14 grey15 grey16 grey17
grey18 grey19 grey2 grey20 grey21
grey22 grey23 grey24 grey25 grey26
grey27 grey28 grey29 grey3 grey30
grey31 grey32 grey33 grey34 grey35
grey36 grey37 grey38 grey39 grey4
grey40 grey41 grey42 grey43 grey44
grey45 grey46 grey47 grey48 grey49
grey5 grey50 grey51 grey52 grey53
grey54 grey55 grey56 grey57 grey58
grey59 grey6 grey60 grey61 grey62
grey63 grey64 grey65 grey66 grey67
grey68 grey69 grey7 grey70 grey71
grey72 grey73 grey74 grey75 grey76
grey77 grey78 grey79 grey8 grey80
grey81 grey82 grey83 grey84 grey85
grey86 grey87 grey88 grey89 grey9
grey90 grey91 grey92 grey93 grey94
grey95 grey96 grey97 grey98 grey99
honeydew honeydew1 honeydew2 honeydew3 honeydew4
hotpink hotpink1 hotpink2 hotpink3 hotpink4
indianred indianred1 indianred2 indianred3 indianred4
indigo invis ivory ivory1 ivory2
ivory3 ivory4 khaki khaki1 khaki2
khaki3 khaki4 lavender lavenderblush lavenderblush1
lavenderblush2 lavenderblush3 lavenderblush4 lawngreen lemonchiffon
lemonchiffon1 lemonchiffon2 lemonchiffon3 lemonchiffon4 lightblue
lightblue1 lightblue2 lightblue3 lightblue4 lightcoral
lightcyan lightcyan1 lightcyan2 lightcyan3 lightcyan4
lightgoldenrod lightgoldenrod1 lightgoldenrod2 lightgoldenrod3 lightgoldenrod4
lightgoldenrodyellow lightgray lightgrey lightpink lightpink1
lightpink2 lightpink3 lightpink4 lightsalmon lightsalmon1
lightsalmon2 lightsalmon3 lightsalmon4 lightseagreen lightskyblue
lightskyblue1 lightskyblue2 lightskyblue3 lightskyblue4 lightslateblue
lightslategray lightslategrey lightsteelblue lightsteelblue1 lightsteelblue2
lightsteelblue3 lightsteelblue4 lightyellow lightyellow1 lightyellow2
lightyellow3 lightyellow4 limegreen linen magenta
magenta1 magenta2 magenta3 magenta4 maroon
maroon1 maroon2 maroon3 maroon4 mediumaquamarine
mediumblue mediumorchid mediumorchid1 mediumorchid2 mediumorchid3
mediumorchid4 mediumpurple mediumpurple1 mediumpurple2 mediumpurple3
mediumpurple4 mediumseagreen mediumslateblue mediumspringgreen mediumturquoise
mediumvioletred midnightblue mintcream mistyrose mistyrose1
mistyrose2 mistyrose3 mistyrose4 moccasin navajowhite
navajowhite1 navajowhite2 navajowhite3 navajowhite4    navy   
navyblue    none    oldlace olivedrab olivedrab1
olivedrab2 olivedrab3 olivedrab4 orange orange1
orange2 orange3 orange4 orangered orangered1
orangered2 orangered3 orangered4 orchid orchid1
orchid2 orchid3 orchid4 palegoldenrod palegreen
palegreen1 palegreen2 palegreen3 palegreen4 paleturquoise
paleturquoise1 paleturquoise2 paleturquoise3 paleturquoise4 palevioletred
palevioletred1 palevioletred2 palevioletred3 palevioletred4 papayawhip
peachpuff peachpuff1 peachpuff2 peachpuff3 peachpuff4
   peru       pink    pink1 pink2 pink3
pink4    plum    plum1 plum2 plum3
plum4 powderblue purple purple1 purple2
purple3 purple4    red       red1       red2   
   red3       red4    rosybrown rosybrown1 rosybrown2
rosybrown3 rosybrown4 royalblue royalblue1 royalblue2
royalblue3 royalblue4 saddlebrown salmon salmon1
salmon2 salmon3 salmon4 sandybrown seagreen
seagreen1 seagreen2 seagreen3 seagreen4 seashell
seashell1 seashell2 seashell3 seashell4 sienna
sienna1 sienna2 sienna3 sienna4 skyblue
skyblue1 skyblue2 skyblue3 skyblue4 slateblue
slateblue1 slateblue2 slateblue3 slateblue4 slategray
slategray1 slategray2 slategray3 slategray4 slategrey
   snow    snow1 snow2 snow3 snow4
springgreen springgreen1 springgreen2 springgreen3 springgreen4
steelblue steelblue1 steelblue2 steelblue3 steelblue4
   tan       tan1       tan2       tan3       tan4   
thistle thistle1 thistle2 thistle3 thistle4
tomato tomato1 tomato2 tomato3 tomato4
transparent turquoise turquoise1 turquoise2 turquoise3
turquoise4 violet violetred violetred1 violetred2
violetred3 violetred4 wheat wheat1 wheat2
wheat3 wheat4 white whitesmoke yellow
yellow1 yellow2 yellow3 yellow4 yellowgreen

箭头类型

通常可以这样指定箭头形状: [arrowhead=dot]

"box" "crow" "curve" "diamond"
"dot" "icurve" "inv" "none"
"normal" "tee" "vee"

下面是一个案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dot = '''
digraph graphname {
subgraph clusterA {
node [shape=plaintext,style=filled];
1 -> 2 [arrowhead=dot];
2 -> 3 -> X2 -> 5[arrowhead=tee];
6;
7;
label = "A";
color=blue
}
}
'''
graph = pdp.graph_from_dot_data(dot)
# 下面这行用于在notebook中显示图片
Image(graph.create_png())

png

终极案例-绘制决策树

使用pydotplus可以很方便的绘制决策树的决策图, 下面直接上代码就可以了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#用于数据处理和分析的工具包
import pandas as pd
#引入用于数据预处理/特征工程的工具包
from sklearn import preprocessing
#import决策树建模包
from sklearn import tree
from IPython.display import display, Image
adult_data = pd.read_csv('DecisionTree.csv')
feature_columns = [u'workclass', u'education', u'marital-status', u'occupation', u'relationship', u'race', u'gender', u'native-country']
label_column = ['income']
features = adult_data[feature_columns]
label = adult_data[label_column]
features = pd.get_dummies(features)
#初始化一个决策树分类器
clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=3)
#用决策树分类器拟合数据
clf = clf.fit(features.values, label.values)
dot_data = tree.export_graphviz(clf,
out_file=None,
feature_names=features.columns,
class_names = ['<=50k', '>50k'],
filled = True,
rounded =True
)
graph = pdp.graph_from_dot_data(dot_data)
display(Image(graph.create_png()))

png

注意
本文由jupyter notebook转换而来, 您可以在这里下载notebook
统计咨询请加QQ 2726725926, 微信 mllncn, SPSS统计咨询是收费的
微博上@mlln-cn可以向我免费题问
请记住我的网址: mlln.cn 或者 jupyter.cn

统计咨询

统计咨询请加入我的星球,有问必回

加入星球向我提问(必回),下载资料,数据,软件等

赞助

持续创造有价值的内容, 我需要你的帮助