Julia(未來可能替代Python與R語言) 導入CSV文件以及畫圖

關注微信公共號:小程在線

關注CSDN博客:程志偉的博客

Julia 版本V1.4.2

 

讀取CSV文件

(@v1.4) pkg> add "CSV"
   Updating registry at `C:\Users\cheng\.julia\registries\General`
   Updating git-repo `https://github.com/JuliaRegistries/General.git`
  Resolving package versions...
    Cloning [336ed68f-0bac-5ca0-87d4-7b16caf5d00b] CSV from https://github.com/JuliaData/CSV.jl.git
  Installed CSV ──────────── v0.6.2
    Cloning [48062228-2e41-5def-b9a4-89aafe57970f] FilePathsBase from https://github.com/rofinn/FilePathsBase.jl.git
  Installed FilePathsBase ── v0.8.0
    Cloning [ea10d353-3f73-51f8-a26c-33c1cb351aa5] WeakRefStrings from https://github.com/JuliaData/WeakRefStrings.jl.git
  Installed WeakRefStrings ─ v0.6.2
   Updating `C:\Users\cheng\.julia\environments\v1.4\Project.toml`
  [336ed68f] + CSV v0.6.2
   Updating `C:\Users\cheng\.julia\environments\v1.4\Manifest.toml`
  [336ed68f] + CSV v0.6.2
  [48062228] + FilePathsBase v0.8.0
  [ea10d353] + WeakRefStrings v0.6.2

 

退出下載包的程序

(@v1.4) pkg> ^C

 

查看文件的路徑

julia> pwd()
"E:\\Program Files\\Julia-1.4.2"

 

加載這個包

julia> using CSV
[ Info: Precompiling CSV [336ed68f-0bac-5ca0-87d4-7b16caf5d00b]

 

加載本地CSV文件,注意大小寫

julia> x = CSV.read("F:\\Julia\\data\\magic.csv")
19019×11 DataFrame. Omitted printing of 1 columns
│ Row   │ 28.7967 │ 16.0021 │ 2.6449  │ 0.3918  │ 0.1982  │ 27.7004  │ 22.011   │ -8.2027  │ 40.092  │ 81.8828 │
│       │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64  │ Float64  │ Float64  │ Float64 │ Float64 │
├───────┼─────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼──────────┼─────────┼─────────┤
│ 1     │ 31.6036 │ 11.7235 │ 2.5185  │ 0.5303  │ 0.3773  │ 26.2722  │ 23.8238  │ -9.9574  │ 6.3609  │ 205.261 │
│ 2     │ 162.052 │ 136.031 │ 4.0612  │ 0.0374  │ 0.0187  │ 116.741  │ -64.858  │ -45.216  │ 76.96   │ 256.788 │
│ 3     │ 23.8172 │ 9.5728  │ 2.3385  │ 0.6147  │ 0.3922  │ 27.2107  │ -6.4633  │ -7.1513  │ 10.449  │ 116.737 │
│ 4     │ 75.1362 │ 30.9205 │ 3.1611  │ 0.3168  │ 0.1832  │ -5.5277  │ 28.5525  │ 21.8393  │ 4.648   │ 356.462 │
│ 5     │ 51.624  │ 21.1502 │ 2.9085  │ 0.242   │ 0.134   │ 50.8761  │ 43.1887  │ 9.8145   │ 3.613   │ 238.098 │
│ 6     │ 48.2468 │ 17.3565 │ 3.0332  │ 0.2529  │ 0.1515  │ 8.573    │ 38.0957  │ 10.5868  │ 4.792   │ 219.087 │
│ 7     │ 26.7897 │ 13.7595 │ 2.5521  │ 0.4236  │ 0.2174  │ 29.6339  │ 20.456   │ -2.9292  │ 0.812   │ 237.134 │
│ 8     │ 96.2327 │ 46.5165 │ 4.154   │ 0.0779  │ 0.039   │ 110.355  │ 85.0486  │ 43.1844  │ 4.854   │ 248.226 │
│ 9     │ 46.7619 │ 15.1993 │ 2.5786  │ 0.3377  │ 0.1913  │ 24.7548  │ 43.8771  │ -6.6812  │ 7.875   │ 102.251 │
│ 10    │ 62.7766 │ 29.9104 │ 3.3331  │ 0.2475  │ 0.1261  │ -33.9065 │ 57.5848  │ 23.771   │ 9.9144  │ 323.094 │

│ 19009 │ 39.5223 │ 18.6327 │ 2.8341  │ 0.2462  │ 0.1414  │ 24.1819  │ 23.219   │ -12.9245 │ 46.268  │ 187.097 │
│ 19010 │ 32.4902 │ 10.6723 │ 2.4742  │ 0.4664  │ 0.2735  │ -27.0097 │ -21.1687 │ 8.4813   │ 69.173  │ 120.668 │
│ 19011 │ 79.5528 │ 44.9929 │ 3.5488  │ 0.1656  │ 0.09    │ -39.6213 │ 53.7866  │ -30.0054 │ 15.8075 │ 311.568 │
│ 19012 │ 31.8373 │ 13.8734 │ 2.8251  │ 0.4169  │ 0.1988  │ -16.4919 │ -27.1448 │ 11.1098  │ 11.3663 │ 100.057 │
│ 19013 │ 182.5   │ 76.5568 │ 3.6872  │ 0.1123  │ 0.0666  │ 192.268  │ 93.0302  │ -62.6192 │ 82.1691 │ 283.473 │
│ 19014 │ 43.298  │ 17.3545 │ 2.8307  │ 0.2877  │ 0.1646  │ -60.1842 │ -33.8513 │ -3.6545  │ 78.4099 │ 224.83  │
│ 19015 │ 21.3846 │ 10.917  │ 2.6161  │ 0.5857  │ 0.3934  │ 15.2618  │ 11.5245  │ 2.8766   │ 2.4229  │ 106.826 │
│ 19016 │ 28.9452 │ 6.702   │ 2.2672  │ 0.5351  │ 0.2784  │ 37.0816  │ 13.1853  │ -2.9632  │ 86.7975 │ 247.456 │
│ 19017 │ 75.4455 │ 47.5305 │ 3.4483  │ 0.1417  │ 0.0549  │ -9.3561  │ 41.0562  │ -9.4662  │ 30.2987 │ 256.517 │
│ 19018 │ 120.513 │ 76.9018 │ 3.9939  │ 0.0944  │ 0.0683  │ 5.8043   │ -93.5224 │ -63.8389 │ 84.6874 │ 408.317 │
│ 19019 │ 187.181 │ 53.0014 │ 3.2093  │ 0.2876  │ 0.1539  │ -167.312 │ -168.456 │ 31.4755  │ 52.731  │ 272.317 │

 

 

查看數據的前幾行

julia> using DataFrames

julia> head(x)
┌ Warning: `head(df::AbstractDataFrame)` is deprecated, use `first(df, 6)` instead.
│   caller = top-level scope at REPL[6]:1
└ @ Core REPL[6]:1
6×11 DataFrame
│ Row │ 28.7967 │ 16.0021 │ 2.6449  │ 0.3918  │ 0.1982  │ 27.7004 │ 22.011  │ -8.2027 │ 40.092  │ 81.8828 │ g      │
│     │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │ String │
├─────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼────────┤
│ 1   │ 31.6036 │ 11.7235 │ 2.5185  │ 0.5303  │ 0.3773  │ 26.2722 │ 23.8238 │ -9.9574 │ 6.3609  │ 205.261 │ g      │
│ 2   │ 162.052 │ 136.031 │ 4.0612  │ 0.0374  │ 0.0187  │ 116.741 │ -64.858 │ -45.216 │ 76.96   │ 256.788 │ g      │
│ 3   │ 23.8172 │ 9.5728  │ 2.3385  │ 0.6147  │ 0.3922  │ 27.2107 │ -6.4633 │ -7.1513 │ 10.449  │ 116.737 │ g      │
│ 4   │ 75.1362 │ 30.9205 │ 3.1611  │ 0.3168  │ 0.1832  │ -5.5277 │ 28.5525 │ 21.8393 │ 4.648   │ 356.462 │ g      │
│ 5   │ 51.624  │ 21.1502 │ 2.9085  │ 0.242   │ 0.134   │ 50.8761 │ 43.1887 │ 9.8145  │ 3.613   │ 238.098 │ g      │
│ 6   │ 48.2468 │ 17.3565 │ 3.0332  │ 0.2529  │ 0.1515  │ 8.573   │ 38.0957 │ 10.5868 │ 4.792   │ 219.087 │ g      │

 

#將數據分成兩部分,一部分是數值型,一部分是分類型

julia> N ,n = size(x)
(19019, 11)

julia> I=Array{Float64,2}(undef,N,n-1)
19019×10 Array{Float64,2}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 ⋮                        ⋮
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

julia> O = x[:,end]
19019-element PooledArrays.PooledArray{String,UInt32,1,Array{UInt32,1}}:
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 "g"
 ⋮
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"
 "h"

julia> for j=1:(n-1)
       for i = 1:N
       I[i,j] = Float64(x[i,j])
              end
              end

 

對數據集進行數據探索

julia> describe(x)
11×8 DataFrame
│ Row │ variable │ mean     │ min      │ median  │ max     │ nunique │ nmissing │ eltype   │
│     │ Symbol   │ Union…   │ Any      │ Union…  │ Any     │ Union…  │ Nothing  │ DataType │
├─────┼──────────┼──────────┼──────────┼─────────┼─────────┼─────────┼──────────┼──────────┤
│ 1   │ 28.7967  │ 53.2514  │ 4.2835   │ 37.149  │ 334.177 │         │          │ Float64  │
│ 2   │ 16.0021  │ 22.1813  │ 0.0      │ 17.1406 │ 256.382 │         │          │ Float64  │
│ 3   │ 2.6449   │ 2.82503  │ 1.9413   │ 2.7396  │ 5.3233  │         │          │ Float64  │
│ 4   │ 0.3918   │ 0.380326 │ 0.0131   │ 0.3541  │ 0.893   │         │          │ Float64  │
│ 5   │ 0.1982   │ 0.214658 │ 0.0003   │ 0.1965  │ 0.6752  │         │          │ Float64  │
│ 6   │ 27.7004  │ -4.33343 │ -457.916 │ 4.0119  │ 575.241 │         │          │ Float64  │
│ 7   │ 22.011   │ 10.5449  │ -331.78  │ 15.3094 │ 238.321 │         │          │ Float64  │
│ 8   │ -8.2027  │ 0.25017  │ -205.895 │ 0.6898  │ 179.851 │         │          │ Float64  │
│ 9   │ 40.092   │ 27.6451  │ 0.0      │ 17.677  │ 90.0    │         │          │ Float64  │
│ 10  │ 81.8828  │ 193.824  │ 1.2826   │ 191.857 │ 495.561 │         │          │ Float64  │
│ 11  │ g        │          │ g        │         │ h       │ 2       │          │ String   │

 

加載畫圖的包

julia> using Gadfly

 

條形圖,多用於分類變量,統計不同的個數

julia> plot(df,x="class",Geom.bar,Guide.ylabel("count"),Guide.title("Class for magic"))

折線圖,一般主要應用於時間類型的數據集中

plot(df,y="Fsize",Geom.line,Guide.xlabel("data point"),Guide.ylabel("Fsize"),Guide.title("Fsize for date"))

散點圖,散點圖可以看出數據的分佈以及是否具有相關性

plot(x=df[:FM3Long],y=df[:FM3Trans],Geom.point,Guide.xlabel("FM3Long"),Guide.ylabel("FM3Trans"),Guide.title("Two for X_Y"))

直方圖,可以看出數據分分佈以及數量的多少

plot(x=df[:FAlpha],Geom.histogram,Guide.xlabel("FAlpha"),Guide.ylabel("frequency"))

bincount=20這參數可以將直方圖分成20份

plot(x=df[:FAlpha],Geom.histogram(bincount=20),Guide.xlabel("FAlpha"),Guide.ylabel("frequency"))

 

 

查看文件的位置

julia> pwd()
"E:\\Program Files\\Julia-1.4.2"

 

 

使用Cairo包

julia> using Cairo
[ Info: Precompiling Cairo [159f3aea-2a34-519c-b102-8c37f9878175]

julia> myplot = plot(x=[1,2,3,4,5],y=[2,3.5,7,8,10])

 

將圖保存在本地

julia> draw(PNG("myplot.png",5inch,2.5inch),myplot)

在本機查看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章