JuliaPro操作界面
這是一篇使用Julia集成庫對數(shù)據(jù)集做探索的長筆記,有想法隨時更新.
目前打算學習的庫有:
Queryverse:
- Query:對標Python的pandas, R中的dplyr. 專注于對DataFrame對象的操作
- CSV: File IO, 讀寫CSV文件
StatsKit:
- 提供了各種統(tǒng)計學&機器學習庫
Gadfly
- 可視化庫,對DataFrame對象很友好
using Queryverse, StatsKit, Gadfly
- 載入CSV數(shù)據(jù)集
data = CSV.file("D:\\contest\\dianxin\\Train85p.csv") |> DataFrame
最后面的 "|> DataFrame" 中 "|>" 是管道操作符,相當于R中的"%>%", 作用是將上一個語句的結果傳遞入下一個語句. "DataFrame"本來是函數(shù)"DataFrame()", 但用在管道操作符后不需要括號
-
數(shù)據(jù)檢視
size()
data |> size
(3400, 20)
3400條記錄,20個變量
show()
data |> show
3400×20 DataFrame. Omitted printing of 7 columns
│ Row │ id │ state │ account_length │ area_code │ phone_number │ international_plan │ voice_mail_plan │ number_vmail_messages │ total_day_minutes │ total_day_calls │ total_day_charge │ total_eve_minutes │ total_eve_calls │
│ │ Int64 │ String │ Int64 │ Int64 │ String │ String │ String │ Int64 │ Float64 │ Int64 │ Float64 │ Float64 │ Int64 │
├──────┼───────┼────────┼────────────────┼───────────┼──────────────┼────────────────────┼─────────────────┼───────────────────────┼───────────────────┼─────────────────┼──────────────────┼───────────────────┼─────────────────┤
│ 1 │ 3 │ NJ │ 137 │ 415 │ 358-1921 │ no │ no │ 0 │ 243.4 │ 114 │ 41.38 │ 121.2 │ 110 │
│ 2 │ 4 │ OH │ 84 │ 408 │ 375-9999 │ yes │ no │ 0 │ 299.4 │ 71 │ 50.9 │ 61.9 │ 88 │
│ 3 │ 5 │ OK │ 75 │ 415 │ 330-6626 │ yes │ no │ 0 │ 166.7 │ 113 │ 28.34 │ 148.3 │ 122 │
│ 4 │ 7 │ MA │ 121 │ 510 │ 355-9993 │ no │ yes │ 24 │ 218.2 │ 88 │ 37.09 │ 348.5 │ 108 │
│ 5 │ 10 │ WV │ 141 │ 415 │ 330-8173 │ yes │ yes │ 37 │ 258.6 │ 84 │ 43.96 │ 222.0 │ 111 │
│ 6 │ 12 │ RI │ 74 │ 415 │ 344-9403 │ no │ no │ 0 │ 187.7 │ 127 │ 31.91 │ 163.4 │ 148 │
?
│ 3394 │ 4989 │ WA │ 80 │ 510 │ 397-4475 │ no │ no │ 0 │ 157.0 │ 101 │ 26.69 │ 208.8 │ 127 │
│ 3395 │ 4990 │ MN │ 150 │ 408 │ 407-6315 │ no │ no │ 0 │ 170.0 │ 115 │ 28.9 │ 162.7 │ 138 │
│ 3396 │ 4991 │ ND │ 140 │ 510 │ 364-8203 │ no │ no │ 0 │ 244.7 │ 115 │ 41.6 │ 258.6 │ 101 │
│ 3397 │ 4992 │ AZ │ 97 │ 510 │ 410-3888 │ no │ no │ 0 │ 252.6 │ 89 │ 42.94 │ 340.3 │ 91 │
│ 3398 │ 4994 │ WV │ 73 │ 408 │ 411-9655 │ no │ no │ 0 │ 177.9 │ 89 │ 30.24 │ 131.2 │ 82 │
│ 3399 │ 4999 │ DC │ 109 │ 510 │ 394-2206 │ no │ no │ 0 │ 188.8 │ 67 │ 32.1 │ 171.7 │ 92 │
│ 3400 │ 5000 │ VT │ 86 │ 415 │ 373-8058 │ no │ yes │ 34 │ 129.4 │ 102 │ 22.0 │ 267.1 │ 104 │
show()函數(shù)會依據(jù)terminal窗口大小輸出整潔的數(shù)據(jù)框
describe()
data |> describe |> print
20×8 DataFrame
│ Row │ variable │ mean │ min │ median │ max │ nunique │ nmissing │ eltype │
│ │ Symbol │ Union… │ Any │ Union… │ Any │ Union… │ Nothing │ DataType │
├─────┼───────────────────────┼─────────┼───────────┼────────┼───────────┼─────────┼──────────┼──────────┤
│ 1 │ id │ 2492.52 │ 3 │ 2511.5 │ 5000 │ │ │ Int64 │
│ 2 │ state │ │ AK │ │ WY │ 51 │ │ String │
│ 3 │ account_length │ 99.965 │ 1 │ 100.0 │ 238 │ │ │ Int64 │
│ 4 │ area_code │ 437.247 │ 408 │ 415.0 │ 510 │ │ │ Int64 │
│ 5 │ phone_number │ │ 327-1058 │ │ 422-9831 │ 3400 │ │ String │
│ 6 │ international_plan │ │ no │ │ yes │ 2 │ │ String │
│ 7 │ voice_mail_plan │ │ no │ │ yes │ 2 │ │ String │
│ 8 │ number_vmail_messages │ 7.76235 │ 0 │ 0.0 │ 52 │ │ │ Int64 │
│ 9 │ total_day_minutes │ 181.006 │ 0.0 │ 180.6 │ 351.5 │ │ │ Float64 │
│ 10 │ total_day_calls │ 99.8018 │ 0 │ 100.0 │ 163 │ │ │ Int64 │
│ 11 │ total_day_charge │ 30.7716 │ 0.0 │ 30.7 │ 59.76 │ │ │ Float64 │
│ 12 │ total_eve_minutes │ 201.037 │ 22.3 │ 201.25 │ 363.7 │ │ │ Float64 │
│ 13 │ total_eve_calls │ 99.8253 │ 12 │ 100.0 │ 164 │ │ │ Int64 │
│ 14 │ total_eve_charge │ 17.0883 │ 1.9 │ 17.105 │ 30.91 │ │ │ Float64 │
│ 15 │ total_night_minutes │ 200.325 │ 23.2 │ 200.8 │ 381.6 │ │ │ Float64 │
│ 16 │ total_night_calls │ 99.735 │ 12 │ 100.0 │ 175 │ │ │ Int64 │
│ 17 │ total_night_charge │ 9.01475 │ 1.04 │ 9.04 │ 17.17 │ │ │ Float64 │
│ 18 │ total_intl_minutes │ 10.2966 │ 0.0 │ 10.3 │ 20.0 │ │ │ Float64 │
│ 19 │ total_intl_calls │ 4.47059 │ 0 │ 4.0 │ 19 │ │ │ Int64 │
│ 20 │ total_intl_charge │ 2.7806 │ 0.0 │ 2.78 │ 5.4 │ │ │ Float64 │
describe函數(shù)會輸出每個變量的:
均值, 最小值, 中間值, 最大值, 唯一值數(shù)量, 缺失值數(shù)量, 變量類型
輸出各個州用戶統(tǒng)計數(shù)量, 費用, 變異系數(shù)
state = data |>
@mutate(total_charge = _.total_day_charge + _.total_eve_charge + _.total_night_charge + _.total_intl_charge) |>
@groupby(_.state) |>
@map({state = key(_),
count = length(_),
mean_charge = mean(_.total_charge),
vari_charge = variation(_.total_charge)}) |>
@orderby_descending(_.mean_charge) |>
DataFrame
state |> show
51×4 DataFrame
│ Row │ state │ count │ mean_charge │ vari_charge │
│ │ String │ Int64 │ Float64 │ Float64 │
├─────┼────────┼───────┼─────────────┼─────────────┤
│ 1 │ KS │ 73 │ 62.8116 │ 0.17163 │
│ 2 │ NJ │ 77 │ 62.0979 │ 0.186393 │
│ 3 │ MD │ 66 │ 62.0015 │ 0.169759 │
│ 4 │ IN │ 65 │ 61.6071 │ 0.167058 │
│ 5 │ GA │ 55 │ 61.4805 │ 0.158179 │
│ 6 │ OH │ 82 │ 61.0411 │ 0.167827 │
?
│ 45 │ AZ │ 61 │ 57.842 │ 0.183879 │
│ 46 │ SC │ 57 │ 57.7188 │ 0.212173 │
│ 47 │ WI │ 69 │ 57.6706 │ 0.197836 │
│ 48 │ MO │ 54 │ 57.5093 │ 0.208202 │
│ 49 │ LA │ 55 │ 57.4904 │ 0.148391 │
│ 50 │ CO │ 60 │ 57.4585 │ 0.189113 │
│ 51 │ IL │ 60 │ 57.3668 │ 0.174889 │
以州為分組, 對各州的用戶數(shù), 平均總費用, 用戶總費用變異系數(shù)進行聚合.
結果顯示:各州用戶在州間平均總費用及州內(nèi)各用戶總費用差異并不大.