关注微信公共号:小程在线
关注CSDN博客:程志伟的博客
版本V1.4.2
基本抽样
julia> using CSV
julia> data = CSV.read("F:\\Julia\\data\\magic.csv")
19020×11 DataFrame. Omitted printing of 1 columns
│ Row │ FLength │ Fwidth │ Fsize │ FConc │ FConcl │ FAsym │ FM3Long │ FM3Trans │ FAlpha │ FDist │
│ │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │
├───────┼─────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼──────────┼─────────┼─────────┤
│ 1 │ 28.7967 │ 16.0021 │ 2.6449 │ 0.3918 │ 0.1982 │ 27.7004 │ 22.011 │ -8.2027 │ 40.092 │ 81.8828 │
│ 2 │ 31.6036 │ 11.7235 │ 2.5185 │ 0.5303 │ 0.3773 │ 26.2722 │ 23.8238 │ -9.9574 │ 6.3609 │ 205.261 │
│ 3 │ 162.052 │ 136.031 │ 4.0612 │ 0.0374 │ 0.0187 │ 116.741 │ -64.858 │ -45.216 │ 76.96 │ 256.788 │
│ 4 │ 23.8172 │ 9.5728 │ 2.3385 │ 0.6147 │ 0.3922 │ 27.2107 │ -6.4633 │ -7.1513 │ 10.449 │ 116.737 │
│ 5 │ 75.1362 │ 30.9205 │ 3.1611 │ 0.3168 │ 0.1832 │ -5.5277 │ 28.5525 │ 21.8393 │ 4.648 │ 356.462 │
│ 6 │ 51.624 │ 21.1502 │ 2.9085 │ 0.242 │ 0.134 │ 50.8761 │ 43.1887 │ 9.8145 │ 3.613 │ 238.098 │
│ 7 │ 48.2468 │ 17.3565 │ 3.0332 │ 0.2529 │ 0.1515 │ 8.573 │ 38.0957 │ 10.5868 │ 4.792 │ 219.087 │
│ 8 │ 26.7897 │ 13.7595 │ 2.5521 │ 0.4236 │ 0.2174 │ 29.6339 │ 20.456 │ -2.9292 │ 0.812 │ 237.134 │
│ 9 │ 96.2327 │ 46.5165 │ 4.154 │ 0.0779 │ 0.039 │ 110.355 │ 85.0486 │ 43.1844 │ 4.854 │ 248.226 │
│ 10 │ 46.7619 │ 15.1993 │ 2.5786 │ 0.3377 │ 0.1913 │ 24.7548 │ 43.8771 │ -6.6812 │ 7.875 │ 102.251 │
⋮
│ 19010 │ 39.5223 │ 18.6327 │ 2.8341 │ 0.2462 │ 0.1414 │ 24.1819 │ 23.219 │ -12.9245 │ 46.268 │ 187.097 │
│ 19011 │ 32.4902 │ 10.6723 │ 2.4742 │ 0.4664 │ 0.2735 │ -27.0097 │ -21.1687 │ 8.4813 │ 69.173 │ 120.668 │
│ 19012 │ 79.5528 │ 44.9929 │ 3.5488 │ 0.1656 │ 0.09 │ -39.6213 │ 53.7866 │ -30.0054 │ 15.8075 │ 311.568 │
│ 19013 │ 31.8373 │ 13.8734 │ 2.8251 │ 0.4169 │ 0.1988 │ -16.4919 │ -27.1448 │ 11.1098 │ 11.3663 │ 100.057 │
│ 19014 │ 182.5 │ 76.5568 │ 3.6872 │ 0.1123 │ 0.0666 │ 192.268 │ 93.0302 │ -62.6192 │ 82.1691 │ 283.473 │
│ 19015 │ 43.298 │ 17.3545 │ 2.8307 │ 0.2877 │ 0.1646 │ -60.1842 │ -33.8513 │ -3.6545 │ 78.4099 │ 224.83 │
│ 19016 │ 21.3846 │ 10.917 │ 2.6161 │ 0.5857 │ 0.3934 │ 15.2618 │ 11.5245 │ 2.8766 │ 2.4229 │ 106.826 │
│ 19017 │ 28.9452 │ 6.702 │ 2.2672 │ 0.5351 │ 0.2784 │ 37.0816 │ 13.1853 │ -2.9632 │ 86.7975 │ 247.456 │
│ 19018 │ 75.4455 │ 47.5305 │ 3.4483 │ 0.1417 │ 0.0549 │ -9.3561 │ 41.0562 │ -9.4662 │ 30.2987 │ 256.517 │
│ 19019 │ 120.513 │ 76.9018 │ 3.9939 │ 0.0944 │ 0.0683 │ 5.8043 │ -93.5224 │ -63.8389 │ 84.6874 │ 408.317 │
│ 19020 │ 187.181 │ 53.0014 │ 3.2093 │ 0.2876 │ 0.1539 │ -167.312 │ -168.456 │ 31.4755 │ 52.731 │ 272.317 │
julia> p = 0.1
0.1
julia> n = round(Int64,19020*p)
1902
julia> ind = sample(1:19020,n)
ERROR: UndefVarError: sample not defined
Stacktrace:
[1] top-level scope at REPL[5]:1
julia> using StatsBase
julia> ind = sample(1:19020,n)
1902-element Array{Int64,1}:
2970
11148
9734
14263
12341
13879
6768
1292
8636
18273
14879
11416
2331
⋮
12753
2972
10233
15387
593
5142
17606
17563
6762
15756
7153
460
julia> SampleData = data[ind,:]
1902×11 DataFrame
│ Row │ FLength │ Fwidth │ Fsize │ FConc │ FConcl │ FAsym │ FM3Long │ FM3Trans │ FAlpha │ FDist │ class │
│ │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ String │
├──────┼─────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼──────────┼─────────┼─────────┼────────┤
│ 1 │ 28.9104 │ 15.2102 │ 2.2889 │ 0.4576 │ 0.2442 │ 4.2429 │ 20.2883 │ -10.6637 │ 21.912 │ 175.706 │ g │
│ 2 │ 36.5643 │ 16.2328 │ 2.9682 │ 0.3206 │ 0.1856 │ 3.9841 │ 19.8322 │ -8.438 │ 6.074 │ 185.927 │ g │
│ 3 │ 26.7645 │ 19.9991 │ 2.7173 │ 0.3317 │ 0.185 │ -11.8517 │ -17.942 │ -16.4464 │ 17.682 │ 187.65 │ g │
│ 4 │ 13.4944 │ 10.1614 │ 2.2978 │ 0.8111 │ 0.4307 │ 14.4597 │ 4.8933 │ 11.8212 │ 67.339 │ 138.325 │ h │
│ 5 │ 49.8986 │ 12.6365 │ 2.5172 │ 0.3191 │ 0.1626 │ -47.4998 │ 17.2189 │ -8.8468 │ 35.934 │ 204.903 │ h │
│ 6 │ 53.3661 │ 24.7587 │ 3.3111 │ 0.2169 │ 0.0909 │ 5.2783 │ 50.6287 │ -16.9234 │ 80.8215 │ 82.4875 │ h │
│ 7 │ 53.6356 │ 18.1699 │ 2.9235 │ 0.2361 │ 0.1258 │ 56.2031 │ 36.8787 │ -11.4993 │ 5.4268 │ 136.178 │ g │
│ 8 │ 20.2899 │ 16.0567 │ 2.8546 │ 0.464 │ 0.2509 │ 1.4769 │ -6.0144 │ 5.3061 │ 0.159 │ 181.475 │ g │
│ 9 │ 70.9121 │ 14.6773 │ 3.0145 │ 0.2998 │ 0.1784 │ 56.122 │ 69.7887 │ -5.1467 │ 0.5949 │ 211.786 │ g │
│ 10 │ 24.8463 │ 15.8127 │ 2.5873 │ 0.4904 │ 0.313 │ -6.1687 │ 6.6229 │ -12.7957 │ 74.1206 │ 240.741 │ h │
⋮
│ 1892 │ 25.254 │ 0.0 │ 2.1746 │ 0.709 │ 0.3645 │ 0.0 │ 0.0 │ 8.482 │ 81.0836 │ 167.628 │ g │
│ 1893 │ 20.778 │ 7.1662 │ 2.0107 │ 0.7122 │ 0.3561 │ 2.0146 │ -9.7705 │ 5.9924 │ 87.7875 │ 129.671 │ g │
│ 1894 │ 36.5524 │ 29.9973 │ 3.0915 │ 0.2348 │ 0.1247 │ 9.6851 │ 11.4084 │ -17.1411 │ 86.9033 │ 182.502 │ h │
│ 1895 │ 15.5071 │ 11.6106 │ 2.3385 │ 0.6422 │ 0.4243 │ 1.4462 │ 2.6585 │ 8.4352 │ 77.7643 │ 149.374 │ g │
│ 1896 │ 99.1508 │ 36.1225 │ 3.995 │ 0.1123 │ 0.0567 │ 100.579 │ 82.4632 │ 25.5191 │ 4.199 │ 268.046 │ g │
│ 1897 │ 48.1061 │ 43.2783 │ 3.462 │ 0.09 │ 0.0714 │ 3.5902 │ -34.4019 │ 31.1157 │ 20.8127 │ 136.076 │ h │
│ 1898 │ 17.6918 │ 7.5442 │ 2.4103 │ 0.5977 │ 0.3226 │ -5.5609 │ 1.0832 │ 5.8182 │ 49.5886 │ 241.559 │ h │
│ 1899 │ 28.043 │ 15.4297 │ 2.6924 │ 0.4223 │ 0.2244 │ -23.4542 │ 22.7079 │ -13.9534 │ 2.826 │ 164.066 │ g │
│ 1900 │ 33.7819 │ 2.7118 │ 2.1491 │ 0.7356 │ 0.4102 │ 2.3256 │ 2.7114 │ 10.3511 │ 11.6642 │ 103.596 │ h │
│ 1901 │ 94.2048 │ 15.4658 │ 2.9297 │ 0.468 │ 0.2581 │ 39.4315 │ -64.4438 │ -13.7153 │ 2.8858 │ 401.134 │ g │
│ 1902 │ 12.6846 │ 11.7208 │ 2.1055 │ 0.7137 │ 0.3725 │ -15.4833 │ 7.3755 │ 11.1393 │ 4.176 │ 200.477 │ g
混淆矩阵
julia> t = [1,1,1,1,2,2,2,2,2,2,2,2]
12-element Array{Int64,1}:
1
1
1
1
2
2
2
2
2
2
2
2
julia> y = [1,2,2,2,2,2,2,2,1,2,2,1]
12-element Array{Int64,1}:
1
2
2
2
2
2
2
2
1
2
2
1
(@v1.4) pkg> add MLBase
Updating registry at `C:\Users\cheng\.julia\registries\General`
Updating git-repo `https://github.com/JuliaRegistries/General.git`
Resolving package versions...
Installed MLBase ─ v0.8.0
Updating `C:\Users\cheng\.julia\environments\v1.4\Project.toml`
[f0e99cf1] + MLBase v0.8.0
Updating `C:\Users\cheng\.julia\environments\v1.4\Manifest.toml`
[f0e99cf1] + MLBase v0.8.0
julia> using MLBase
[ Info: Precompiling MLBase [f0e99cf1-93fa-52ec-9ecc-5026115318e0]
julia> CM = confusmat(2,t,y)
2×2 Array{Int64,2}:
1 3
2 6
准确度:所有样本中被预测正确的样本的比率
julia> correctrate(t,y)
0.5833333333333334
精确度
julia> CM[1,1]/sum(CM[:,1])
0.3333333333333333
召回率
julia> CM[1,1]/sum(CM[1,:])
0.25
F1指标
julia> 2*CM[1,1] / (sum(CM[:,1]) + sum(CM[1,:]))
0.2857142857142857