티스토리 뷰
ulia> using MLJ
julia> using MLJLinearModels
julia> using DataFrames
julia> using CSV
julia> data = DataFrame(
x1 = rand(100),
x2 = rand(100),
y = 2.0 .* rand(100) .+ 3.0 .* rand(100) .+ 0.5 .* randn(100)
)
100×3 DataFrame
Row │ x1 x2 y
│ Float64 Float64 Float64
─────┼──────────────────────────────────
1 │ 0.192061 0.182668 1.83995
2 │ 0.239412 0.241827 2.85724
3 │ 0.861052 0.599743 -0.210635
4 │ 0.564033 0.408944 1.77965
5 │ 0.252953 0.508077 2.79553
6 │ 0.253743 0.455737 3.77589
7 │ 0.18083 0.875397 3.67911
8 │ 0.540491 0.843854 2.93323
9 │ 0.234287 0.0899058 2.24417
10 │ 0.880303 0.239799 2.62155
11 │ 0.571448 0.068163 1.82923
12 │ 0.719927 0.499856 2.53813
13 │ 0.801466 0.475962 1.79804
14 │ 0.836205 0.0899224 2.75879
15 │ 0.929159 0.63812 3.33808
16 │ 0.818031 0.693345 2.73288
17 │ 0.805675 0.555696 2.75188
18 │ 0.765726 0.712669 3.45325
19 │ 0.776436 0.938539 1.95507
20 │ 0.690194 0.565326 3.81251
21 │ 0.749909 0.105393 0.983134
⋮ │ ⋮ ⋮ ⋮
80 │ 0.897222 0.924209 2.29332
81 │ 0.244506 0.762634 0.762134
82 │ 0.346327 0.256684 2.73695
83 │ 0.68947 0.979288 1.1264
84 │ 0.361654 0.981354 2.19927
85 │ 0.115589 0.81703 4.1167
86 │ 0.0308383 0.557071 0.476793
87 │ 0.0919847 0.942047 2.32308
88 │ 0.373633 0.450567 2.29001
89 │ 0.129606 0.474587 2.99922
90 │ 0.731564 0.1403 0.29342
91 │ 0.952878 0.683653 1.47864
92 │ 0.682344 0.556616 3.18233
93 │ 0.0386295 0.231528 3.69097
94 │ 0.0527703 0.282725 3.36231
95 │ 0.659491 0.697843 3.13355
96 │ 0.778358 0.0259722 3.96647
97 │ 0.486306 0.94641 3.43425
98 │ 0.929504 0.53474 1.4491
99 │ 0.426442 0.168707 3.36641
100 │ 0.510757 0.19042 3.66507
58 rows omitted
julia> X = select(data, Not(:y))
100×2 DataFrame
Row │ x1 x2
│ Float64 Float64
─────┼───────────────────────
1 │ 0.192061 0.182668
2 │ 0.239412 0.241827
3 │ 0.861052 0.599743
4 │ 0.564033 0.408944
5 │ 0.252953 0.508077
6 │ 0.253743 0.455737
7 │ 0.18083 0.875397
8 │ 0.540491 0.843854
9 │ 0.234287 0.0899058
10 │ 0.880303 0.239799
11 │ 0.571448 0.068163
12 │ 0.719927 0.499856
13 │ 0.801466 0.475962
14 │ 0.836205 0.0899224
15 │ 0.929159 0.63812
16 │ 0.818031 0.693345
17 │ 0.805675 0.555696
18 │ 0.765726 0.712669
19 │ 0.776436 0.938539
20 │ 0.690194 0.565326
21 │ 0.749909 0.105393
⋮ │ ⋮ ⋮
80 │ 0.897222 0.924209
81 │ 0.244506 0.762634
82 │ 0.346327 0.256684
83 │ 0.68947 0.979288
84 │ 0.361654 0.981354
85 │ 0.115589 0.81703
86 │ 0.0308383 0.557071
87 │ 0.0919847 0.942047
88 │ 0.373633 0.450567
89 │ 0.129606 0.474587
90 │ 0.731564 0.1403
91 │ 0.952878 0.683653
92 │ 0.682344 0.556616
93 │ 0.0386295 0.231528
94 │ 0.0527703 0.282725
95 │ 0.659491 0.697843
96 │ 0.778358 0.0259722
97 │ 0.486306 0.94641
98 │ 0.929504 0.53474
99 │ 0.426442 0.168707
100 │ 0.510757 0.19042
58 rows omitted
julia> y = data.y
100-element Vector{Float64}:
1.8399533785030409
2.857235824645789
-0.2106348117611867
1.7796470633322703
2.7955270919671054
3.7758895549875042
3.6791101393247576
2.933231848766845
2.244167567179388
2.621553441888179
1.8292252369627446
2.5381338606611865
1.7980388596087666
2.758794004480519
3.338075507971434
2.7328808778996683
2.7518751341867134
3.453254889171504
1.9550650912176089
3.8125096425478864
0.9831337933118044
2.78160772446768
3.2881945406543642
⋮
1.3589371441326445
4.339439570019815
2.2933181424299436
0.7621342069637103
2.7369502642512127
1.126401727423646
2.199268870556245
4.116698919898103
0.4767926632394386
2.323080232196837
2.290007685628936
2.99922106268776
0.29341990370227444
1.4786415321589095
3.182330163507364
3.6909748455645794
3.3623077366597136
3.133550666253239
3.966471336990114
3.4342464797699424
1.4491021737413607
3.3664101852373194
3.665074537904651
julia> model = LinearRegressor()
LinearRegressor(
fit_intercept = true,
solver = nothing)
julia> mach = machine(model, X, y)
untrained Machine; caches model-specific representations of data
model: LinearRegressor(fit_intercept = true, …)
args:
1: Source @768 ⏎ Table{AbstractVector{Continuous}}
2: Source @973 ⏎ AbstractVector{Continuous}
julia> fit!(mach)
[ Info: Training machine(LinearRegressor(fit_intercept = true, …), …).
┌ Info: Solver: Analytical
│ iterative: Bool false
└ max_inner: Int64 200
trained Machine; caches model-specific representations of data
model: LinearRegressor(fit_intercept = true, …)
args:
1: Source @768 ⏎ Table{AbstractVector{Continuous}}
2: Source @973 ⏎ AbstractVector{Continuous}
julia> new_data = DataFrame(
x1 = rand(5),
x2 = rand(5)
)
5×2 DataFrame
Row │ x1 x2
│ Float64 Float64
─────┼────────────────────
1 │ 0.477914 0.320929
2 │ 0.722891 0.690786
3 │ 0.9236 0.666092
4 │ 0.675041 0.270055
5 │ 0.994656 0.145013
julia> ypred = predict(mach, new_data)
5-element Vector{Float64}:
2.3583707898980606
2.4832743860385493
2.4271165756894395
2.290340178183913
2.1594574192231777
julia> println(ypred)
[2.3583707898980606, 2.4832743860385493, 2.4271165756894395, 2.290340178183913, 2.1594574192231777]
julia>
안녕하세요. Julia 언어를 사용한 머신 러닝 예제에 대해 설명드리겠습니다. 아래는 간단한 머신 러닝 예제를 통해, Julia 언어로 머신 러닝 모델을 학습시키는 방법을 보여드리겠습니다.
### 예제: 선형 회귀 (Linear Regression)
이 예제에서는 `MLJ` 패키지를 사용하여 간단한 선형 회귀 모델을 구축하고 학습시키겠습니다.
1. 환경 설정
```julia
using Pkg
Pkg.add("MLJ")
Pkg.add("MLJLinearModels")
Pkg.add("DataFrames")
Pkg.add("CSV")
```
2. 패키지 불러오기
```julia
using MLJ
using MLJLinearModels
using DataFrames
using CSV
```
3. 데이터 로드 및 전처리
```julia
# 예시 데이터 생성
data = DataFrame(
x1 = rand(100),
x2 = rand(100),
y = 2.0 .* rand(100) .+ 3.0 .* rand(100) .+ 0.5 .* randn(100)
)
# 특징(feature)과 타겟(target) 변수를 나누기
X = select(data, Not(:y))
y = data.y
```
4. 모델 정의 및 학습
```julia
# 모델 정의
model = LinearRegressor()
# 데이터 셋을 MLJ 형식으로 변환
mach = machine(model, X, y)
# 모델 학습
fit!(mach)
```
5. 예측 수행
```julia
# 새로운 데이터 생성
new_data = DataFrame(
x1 = rand(5),
x2 = rand(5)
)
# 예측 수행
ypred = predict(mach, new_data)
# 예측 결과 출력
println(ypred)
```
위 예제는 간단하게 선형 회귀 모델을 만들고, 학습시키고, 예측하는 과정을 설명하였습니다.
### 참고 문서
- [MLJ.jl Documentation](https://alan-turing-institute.github.io/MLJ.jl/dev/)
- [MLJLinearModels.jl Documentation](https://github.com/alan-turing-institute/MLJLinearModels.jl)
질문이 있으시거나 추가적인 도움이 필요하시면 언제든지 말씀해 주세요. 감사합니다.
'언어 > Julia' 카테고리의 다른 글
bond pricing with chatGPT vs claude in Adot. (0) | 2025.03.17 |
---|---|
julia vs c (0) | 2024.05.19 |
괜찮은 자료 (0) | 2024.05.05 |
basic (0) | 2024.02.13 |
ref (0) | 2024.02.13 |