1.进入Julia的REPL窗口的Julia模式中,并且导入Pkg包管理器;导入DecisionTree决策树包和RDatasets数据包,代码如下所示:
julia> using Pkg
julia> using DecisionTree
[ Info: Precompiling DecisionTree [7806a523-6efd-50cb-b5f6-3fa6f1930dbb]
julia> using RDatasets
[ Info: Precompiling RDatasets [ce6b1742-4840-55fa-b093-852dadbb1d8b]
2.查询和切换、设置工作路径
julia> pwd()
"/home/jingxinxing"
julia> cd("/public/hstore5/proteome/Personal_dir/jingxinxing/test/2020年/202002/julia")
julia> pwd()
"/public/hstore5/proteome/Personal_dir/jingxinxing/test/2020年/202002/julia"
3.导入数据集iris
julia> mydata = dataset("datasets", "iris")
150×5 DataFrame
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │
│ │ Float64 │ Float64 │ Float64 │ Float64 │ Categorical… │
├─────┼─────────────┼────────────┼─────────────┼────────────┼──────────────┤
│ 1 │ 5.1 │ 3.5 │ 1.4 │ 0.2 │ setosa │
│ 2 │ 4.9 │ 3.0 │ 1.4 │ 0.2 │ setosa │
│ 3 │ 4.7 │ 3.2 │ 1.3 │ 0.2 │ setosa │
│ 4 │ 4.6 │ 3.1 │ 1.5 │ 0.2 │ setosa │
│ 5 │ 5.0 │ 3.6 │ 1.4 │ 0.2 │ setosa │
?
│ 145 │ 6.7 │ 3.3 │ 5.7 │ 2.5 │ virginica │
│ 146 │ 6.7 │ 3.0 │ 5.2 │ 2.3 │ virginica │
│ 147 │ 6.3 │ 2.5 │ 5.0 │ 1.9 │ virginica │
│ 148 │ 6.5 │ 3.0 │ 5.2 │ 2.0 │ virginica │
│ 149 │ 6.2 │ 3.4 │ 5.4 │ 2.3 │ virginica │
│ 150 │ 5.9 │ 3.0 │ 5.1 │ 1.8 │ virginica │
4.获取前四列数据信息,并通过convert()函数转换为数组
julia> myf = convert(Array, mydata[:, 1:4])
150×4 Array{Float64,2}:
5.1 3.5 1.4 0.2
4.9 3.0 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2
5.0 3.6 1.4 0.2
5.4 3.9 1.7 0.4
4.6 3.4 1.4 0.3
5.0 3.4 1.5 0.2
4.4 2.9 1.4 0.2
4.9 3.1 1.5 0.1
5.4 3.7 1.5 0.2
?
6.7 3.1 5.6 2.4
6.9 3.1 5.1 2.3
5.8 2.7 5.1 1.9
6.8 3.2 5.9 2.3
6.7 3.3 5.7 2.5
6.7 3.0 5.2 2.3
6.3 2.5 5.0 1.9
6.5 3.0 5.2 2.0
6.2 3.4 5.4 2.3
5.9 3.0 5.1 1.8
5. 获取最后一列分组信息,并将其转换为数组
julia> myg = convert(Array, mydata[:,5])
150-element Array{String,1}:
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"setosa"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
"virginica"
6. 创建决策树模型
julia> mymod = build_tree(myg,myf)
Decision Tree
Leaves: 9
Depth: 5
julia> println(mymod)
Decision Tree
Leaves: 9
Depth: 5
#@ Leaves表示决策树的分裂叶子;Depth表示决策树的分层深度,即有几层
7.对创建的决策树(DT)进行修剪
julia> mymod2 = prune_tree(mymod, 0.9)
Decision Tree
Leaves: 8
Depth: 5
julia> println(mymod2)
Decision Tree
Leaves: 8
Depth: 5
julia> mymod3 = prune_tree(mymod, 0.6)
Decision Tree
Leaves: 3
Depth: 2
julia> println(mymod3)
Decision Tree
Leaves: 3
Depth: 2
8.查看修剪好的决策树结构
julia> print_tree(mymod,5)
Feature 4, Threshold 0.8
L-> setosa : 50/50
R-> Feature 4, Threshold 1.75
L-> Feature 3, Threshold 4.95
L-> Feature 4, Threshold 1.65
L-> versicolor : 47/47
R-> virginica : 1/1
R-> Feature 4, Threshold 1.55
L-> virginica : 3/3
R-> Feature 1, Threshold 6.95
L-> versicolor : 2/2
R-> virginica : 1/1
R-> Feature 3, Threshold 4.85
L-> Feature 1, Threshold 5.95
L-> versicolor : 1/1
R-> virginica : 2/2
R-> virginica : 43/43
julia> print_tree(mymod,3)
Feature 4, Threshold 0.8
L-> setosa : 50/50
R-> Feature 4, Threshold 1.75
L-> Feature 3, Threshold 4.95
L->
R->
R-> Feature 3, Threshold 4.85
L->
R-> virginica : 43/43
julia> print_tree(mymod3,2)
Feature 4, Threshold 0.8
L-> setosa : 50/50
R-> Feature 4, Threshold 1.75
L-> versicolor : 49/54
R-> virginica : 45/46
9.至此决策树模型已经构建完成了,接下来用iris自身的数据来预测一下是什么品种(setosa山鸢尾花,versicolor变色鸢尾花,virginica维吉尼亚鸢尾花)
提取iris数据集中的第90行的前4列数据用来测试决策树模型
julia> my90 = mydata[90,1:4]
DataFrameRow
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │
│ │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────┼────────────┼─────────────┼────────────┤
│ 90 │ 5.5 │ 2.5 │ 4.0 │ 1.3 │
julia> println(my90)
DataFrameRow
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │
│ │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────┼────────────┼─────────────┼────────────┤
│ 90 │ 5.5 │ 2.5 │ 4.0 │ 1.3 │
julia> typeof(my90)
DataFrameRow{DataFrame,DataFrames.SubIndex{DataFrames.Index,UnitRange{Int64},UnitRange{Int64}}}
julia> apply_tree(mymod, [5.5,2.5, 4.0, 1.3])
"versicolor"
#@ 第90行的品种是versicolor
我们来看一下原始的iris数据集
julia> mydata[90,5]
CategoricalString{UInt8} "versicolor"
10.再来测试我们的决策树模型
julia> my45
DataFrameRow
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │
│ │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────┼────────────┼─────────────┼────────────┤
│ 45 │ 5.1 │ 3.8 │ 1.9 │ 0.4 │
julia> my145
DataFrameRow
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │
│ │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼─────────────┼────────────┼─────────────┼────────────┤
│ 145 │ 6.7 │ 3.3 │ 5.7 │ 2.5 │
julia> apply_tree(mymod, [5.1, 3.8, 1.9, 0.4])
"setosa"
julia> apply_tree(mymod, [6.7, 3.3, 5.7, 2.5])
"virginica"
julia> mydata[45,5]
CategoricalString{UInt8} "setosa"
julia> mydata[145,5]
CategoricalString{UInt8} "virginica"
11.Julia模式下,按两下Tab按钮,会弹出下面的信息:
julia>
! apply_adaboost_stumps_proba ltoh
!= apply_forest macro
!== apply_forest_proba macroexpand
% apply_tree majority_vote
& apply_tree_proba map
* apropos map!
+ argmax mapcols
- argmin mapfoldl
/ ascii mapfoldr
// asec mapreduce
: asecd mapslices
< asech mark
<: asin match
<< asind max
<= asinh maximum
== asyncmap maximum!
=== asyncmap! maxintfloat
=> atan mean_squared_error
> atand melt
>: atanh meltdf
>= atexit merge
>> atreplinit merge!
>>> axes methods
@MIME_str b" methodswith
@__DIR__ backtrace min
@__FILE__ baremodule minimum
@__LINE__ basename minimum!
@__MODULE__ begin minmax
@__dot__ big missing
@allocated big" missings
@assert bind mkdir
@async binomial mkpath
@b_str bitstring mktemp
@big_str break mktempdir
@boundscheck broadcast mod
@cfunction broadcast! mod1
@cmd bswap mod2pi
@code_llvm build_adaboost_stumps modf
@code_lowered build_forest module
@code_native build_stump mtime
@code_typed build_tree muladd
@code_warntype by mutable struct
@csv2_str bytes2hex mv
@csv_str bytesavailable my145
@debug cat my45
@deprecate catch my90
@doc catch_backtrace mydata
@edit categorical myf
@elapsed categorical! myg
@enum cbrt mymod
@error ccall mymod2
@eval cd mymod3
@evalpoly ceil nameof
@fastmath cglobal names
@functionloc checkbounds names!
@generated checkindex ncodeunits
@gensym chmod ncol
@goto chomp ndigits
@html_str chop ndims
@inbounds chown nextfloat
@info circcopy! nextind
@inline circshift nextpow
@int128_str circshift! nextprod
@isdefined cis nfields
@label clamp nfoldCV_forest
@less clamp! nfoldCV_stumps
@macroexpand cld nfoldCV_tree
@macroexpand1 clipboard nonmissingtype
@noinline close nonunique
@nospecialize cmp normalize
@pkg_str coalesce normpath
@polly code_llvm nothing
@r_str code_lowered notify
@raw_str code_native nrow
@s_str code_typed ntoh
@show code_warntype ntuple
@simd codepoint numerator
@specialize codeunit objectid
@static codeunits occursin
@sync collect oftype
@task columnindex one
@text_str colwise ones
@threadcall combine oneunit
@time compact open
@timed completecases operm
@timev complex order
@tsv_str compress ordered
@uint128_str confusion_matrix ordered!
@v_str conj pairs
@view conj! parent
@views const parentindices
@warn continue parentmodule
@which convert parse
@wsv_str copy partialsort
ARGS copy! partialsort!
AbstractArray copysign partialsortperm
AbstractCategoricalArray copyto! partialsortperm!
AbstractCategoricalMatrix cos passmissing
AbstractCategoricalVector cosc pathof
AbstractChannel cosd peakflops
AbstractChar cosh permute!
AbstractDataFrame cospi permutecols!
AbstractDict cot permutedims
AbstractDisplay cotd permutedims!
AbstractFloat coth pi
AbstractIrrational count pipeline
AbstractMatrix count_ones pkg"
AbstractRange count_zeros pointer
AbstractSet countlines pointer_from_objref
AbstractString cp pop!
AbstractUnitRange csc popdisplay
AbstractVecOrMat cscd popfirst!
AbstractVector csch position
AdaBoostStumpClassifier csv" powermod
All csv2" precision
Any ctime precompile
ArgumentError cumprod predict
Array cumprod! predict_proba
AssertionError cumsum prepend!
Base cumsum! prevfloat
Between current_task prevind
BigFloat cut prevpow
BigInt dataset primitive type
BitArray decompress print
BitMatrix deepcopy print_tree
BitSet deg2rad println
BitVector delete! printstyled
Bool deleteat! process_exited
BoundsError deletecols process_running
Broadcast deletecols! prod
C_NULL deleterows! prod!
CapturedException denominator promote
CartesianIndex depth promote_rule
CartesianIndices describe promote_shape
CategoricalArray detach promote_type
CategoricalArrays devnull propertynames
CategoricalMatrix diff prune_tree
CategoricalPool digits push!
CategoricalString digits! pushdisplay
CategoricalValue dirname pushfirst!
CategoricalVector disable_sigint put!
Cchar disallowmissing pwd
Cdouble disallowmissing! quote
Cfloat display r"
Channel displayable rad2deg
Char displaysize rand
Cint div randn
Cintmax_t divrem range
Clong do rationalize
Clonglong download raw"
Cmd dropdims read
Colon droplevels! read!
Complex dropmissing readavailable
ComplexF16 dropmissing! readbytes!
ComplexF32 dump readchomp
ComplexF64 eachcol readdir
CompositeException eachindex readline
Condition eachline readlines
ConfusionMatrix eachmatch readlink
Core eachrow readtable
Cptrdiff_t eachslice readuntil
Cshort edit real
Csize_t else realpath
Cssize_t elseif recode
Cstring eltype recode!
Cuchar eltypes redirect_stderr
Cuint empty redirect_stdin
Cuintmax_t empty! redirect_stdout
Culong end redisplay
Culonglong endswith reduce
Cushort enumerate reenable_sigint
Cvoid eof reim
Cwchar_t eps reinterpret
Cwstring error relpath
DEPOT_PATH esc rem
DataFrame escape_string rem2pi
DataFrame! eval rename
DataFrameRow evalfile rename!
DataFrames exit repeat
DataType exp replace
DecisionTree exp10 replace!
DecisionTreeClassifier exp2 repr
DecisionTreeRegressor expanduser reset
DenseArray expm1 reshape
DenseMatrix exponent resize!
DenseVecOrMat export rethrow
DenseVector extrema retry
Dict factorial return
DimensionMismatch false reverse
Dims falses reverse!
DivideError fd reverseind
Docs fdio rm
DomainError fetch rot180
ENDIAN_BOM fieldcount rotl90
ENV fieldname rotr90
EOFError fieldnames round
Ensemble fieldoffset rounding
Enum fieldtype rpad
ErrorException fieldtypes rsplit
Exception filemode rstrip
ExponentialBackOff filesize run
Expr fill s"
Float16 fill! schedule
Float32 filter searchsorted
Float64 filter! searchsortedfirst
Function finalize searchsortedlast
GC finalizer sec
GlobalRef finally secd
GroupedDataFrame findall sech
HTML findfirst seek
IO findlast seekend
IOBuffer findmax seekstart
IOContext findmax! select
IOStream findmin select!
IdDict findmin! selectdim
IndexCartesian findnext set_zero_subnormals
IndexLinear findprev setdiff
IndexStyle first setdiff!
InexactError firstindex setenv
Inf fit! setfield!
Inf16 flatten setindex!
Inf32 fld setprecision
Inf64 fld1 setproperty!
InitError fldmod setrounding
InsertionSort fldmod1 show
Int flipsign showable
Int128 float showall
Int16 floatmax showerror
Int32 floatmin sign
Int64 floor signbit
Int8 flush signed
Integer fma significand
InteractiveUtils foldl similar
InterruptException foldr sin
InvalidStateException for sinc
InvertedIndex foreach sincos
InvertedIndices frexp sincosd
Irrational fullname sind
Iterators function sinh
KeyError functionloc sinpi
LOAD_PATH gcd size
Leaf gcdx sizehint!
LevelsException gensym sizeof
Libc get skip
LinRange get! skipchars
LineNumberNode get_classes skipmissing
LinearIndices get_zero_subnormals sleep
LoadError getfield something
MIME gethostname sort
MIME" getindex sort!
MathConstants getkey sortperm
Matrix getpid sortperm!
MergeSort getproperty sortslices
Meta global splice!
Method gperm split
MethodError graphemes splitdir
Missing groupby splitdrive
MissingException groupindices splitext
Missings groupvars splitpath
Module hasfield sprint
NTuple hash sqrt
NaN haskey stack
NaN16 hasmethod stackdf
NaN32 hasproperty stacktrace
NaN64 hcat startswith
NamedTuple head stat
Node hex2bytes stderr
Not hex2bytes! stdin
Nothing homedir stdout
Number html" step
OrderedLevelsException htol stride
OrdinalRange hton strides
OutOfMemoryError hvcat string
OverflowError hypot strip
PKGMODE_MANIFEST identity struct
PKGMODE_PROJECT if subtypes
PROGRAM_FILE ifelse success
PackageMode ignorestatus sum
PackageSpec im sum!
Pair imag summary
PartialQuickSort import supertype
PermutedDimsArray in symdiff
Pipe include symdiff!
PipeBuffer include_dependency symlink
Pkg include_string systemerror
ProcessFailedException indexin tail
Ptr insert! take!
QuickSort insertcols! tan
QuoteNode instances tand
R2 int128" tanh
RDatasets intersect task_local_storage
RandomForestClassifier intersect! tempdir
RandomForestRegressor inv tempname
Rational invmod text"
RawFD invoke textwidth
ReadOnlyMemoryError invperm thisind
Real invpermute! throw
ReentrantLock isa time
Ref isabspath time_ns
Regex isabstracttype timedwait
RegexMatch isapprox titlecase
Registry isascii to_indices
RegistrySpec isassigned touch
RoundDown isbits trailing_ones
RoundFromZero isbitstype trailing_zeros
RoundNearest isblockdev transcode
RoundNearestTiesAway ischardev transpose
RoundNearestTiesUp iscntrl true
RoundToZero isconcretetype trues
RoundUp isconst trunc
RoundingMode isdefined truncate
SegmentationFault isdigit try
Set isdir trylock
Signed isdirpath tryparse
Some isdispatchtuple tsv"
StackOverflowError isempty tuple
StackTraces isequal typeassert
StepRange iseven typeintersect
StepRangeLen isfifo typejoin
StridedArray isfile typemax
StridedMatrix isfinite typemin
StridedVecOrMat isimmutable typeof
StridedVector isinf uint128"
String isinteger uncompact
StringIndexError isinteractive undef
SubArray isless unescape_string
SubDataFrame isletter union
SubString islink union!
SubstitutionString islocked unique
Symbol islowercase unique!
Sys ismarked unlock
SystemError ismissing unmark
Task ismount unsafe_copyto!
TaskFailedException isnan unsafe_load
Text isnothing unsafe_pointer_to_objref
TextDisplay isnumeric unsafe_read
Threads isodd unsafe_store!
Timer isone unsafe_string
Tuple isopen unsafe_trunc
Type isordered unsafe_wrap
TypeError ispath unsafe_write
TypeVar isperm unsigned
UInt ispow2 unstack
UInt128 isprimitivetype uperm
UInt16 isprint uppercase
UInt32 ispunct uppercasefirst
UInt64 isqrt using
UInt8 isreadable v"
UPLEVEL_MAJOR isreadonly valtype
UPLEVEL_MINOR isready values
UPLEVEL_PATCH isreal varinfo
UndefInitializer issetequal vcat
UndefKeywordError issetgid vec
UndefRefError issetuid versioninfo
UndefVarError issocket view
Union issorted wait
UnionAll isspace walkdir
UnitRange issticky which
Unsigned isstructtype while
UpgradeLevel issubnormal widemul
VERSION issubset widen
Val istaskdone withenv
Vararg istaskfailed write
VecElement istaskstarted writetable
VecOrMat istextmime wsv"
Vector isuppercase xor
VersionNumber isvalid yield
WeakKeyDict iswritable yieldto
WeakRef isxdigit zero
\ iszero zeros
^ iterate zip
__precompile__ join |
abs joinpath |>
abs2 keys ~
abspath keytype ÷
abstract type kill π
accumulate kron ℯ
accumulate! last ∈
acos lastindex ∉
acosd lcm ∋
acosh ldexp ∌
acot leading_ones ∘
acotd leading_zeros √
acoth length ∛
acsc less ∩
acscd let ∪
acsch levelcode ≈
adjoint levels ≉
aggregate levels! ≠
all load_data ≡
all! local ≢
allowmissing lock ≤
allowmissing! log ≥
allunique log10 ⊆
angle log1p ⊇
ans log2 ⊈
any lowercase ⊉
any! lowercasefirst ⊊
append! lpad ⊋
applicable lstat ⊻
apply_adaboost_stumps lstrip