Created
April 29, 2020 13:01
-
-
Save DSCF-1224/f408b094fee976f867b0e760bca1dbd3 to your computer and use it in GitHub Desktop.
ガウス過程と機械学習 第1章 linear.py version.02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ================================================================================================================================== | |
# ISBN 978-4-06-152926-7 | |
# ガウス過程と機械学習 | |
# | |
# [reference] | |
# http://chasen.org/~daiti-m/gpbook/python/linear.py | |
# http://chasen.org/~daiti-m/gpbook/data/linear.dat | |
# https://nbviewer.jupyter.org/gist/genkuroki/a37894d5669ad13b4cd27da16096bfd2 | |
# | |
# [How to execute] | |
# Base.MainInclude.include("?\\GitHub\\ISBN9784061529267\\julia\\chap02\\linear_v02.jl") | |
# ================================================================================================================================== | |
module ISBN9784061529267 | |
import CSV # v0.6.1 | |
import HTTP # v0.8.14 | |
import Printf | |
export main | |
struct ObservedData | |
size :: Core.Int64 | |
inpt :: Base.Vector{Core.Float64} # observed input | |
otpt :: Base.Vector{Core.Float64} # observed output | |
end | |
mutable struct LearnedData | |
weight :: Base.Vector{Core.Float64} # weight vector of linear regression | |
otpt :: Base.Vector{Core.Float64} # estimated output | |
end | |
function read_data(file_path) | |
# STEP.01 | |
# get the sample data from online | |
obj_CSV = CSV.read(HTTP.get(file_path).body; delim="\t", header=[:inpt, :otpt], footerskip=1) | |
# STEP.02 | |
# convert the read data to the array on Julia | |
data_size = Base.size(obj_CSV.inpt, 1) | |
buffer_inpt = Base.zeros(data_size) | |
buffer_otpt = Base.zeros(data_size) | |
for itr in 1:1:data_size | |
buffer_inpt[itr] = obj_CSV.inpt[itr] | |
buffer_otpt[itr] = obj_CSV.otpt[itr] | |
end | |
# STEP.END | |
return ObservedData(data_size, buffer_inpt, buffer_otpt) | |
end | |
function compute_learned_data(observed_data::ISBN9784061529267.ObservedData) | |
design_matrix = [ Base.ones(observed_data.size) observed_data.inpt ] | |
design_matrix_transposed = Base.transpose(design_matrix) | |
buffer_weight = Base.inv( design_matrix_transposed * design_matrix ) * design_matrix_transposed * observed_data.otpt | |
buffer_otpt_estimated = buffer_weight[1] .+ buffer_weight[2] .* observed_data.inpt | |
return LearnedData(buffer_weight, buffer_otpt_estimated) | |
end | |
function main() | |
# STEP.01 | |
# get the sample data from online | |
obj_observed_data = ISBN9784061529267.read_data("http://chasen.org/~daiti-m/gpbook/data/linear.dat") | |
obj_learned_data = ISBN9784061529267.compute_learned_data(obj_observed_data) | |
Base.println("") | |
Base.println(obj_learned_data.weight) | |
Base.println("") | |
Printf.@printf("%3s %23s %23s %23s\n", "No.", "input", "output(obs)", "output(est)") | |
for itr in 1:1:obj_observed_data.size | |
Printf.@printf("%3d %23.15e %23.15e %23.15e\n", itr, obj_observed_data.inpt[itr], obj_observed_data.otpt[itr], obj_learned_data.otpt[itr]) | |
end | |
end | |
end | |
# ================================================================================================================================== | |
@time ISBN9784061529267.main() | |
# ================================================================================================================================== | |
# EOF | |
# ================================================================================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> Base.MainInclude.include("?\\GitHub\\ISBN9784061529267\\julia\\chap02\\linear_v02.jl") | |
WARNING: replacing module ISBN9784061529267. | |
[1.2161236702127658, 0.38065159574468094] | |
No. input output(obs) output(est) | |
1 -2.000000000000000e+00 1.200000000000000e+00 4.548204787234039e-01 | |
2 -9.000000000000000e-01 -3.000000000000000e-01 8.735372340425529e-01 | |
3 4.000000000000000e-01 1.700000000000000e+00 1.368384308510638e+00 | |
4 2.000000000000000e+00 2.500000000000000e+00 1.977426861702128e+00 | |
5 2.500000000000000e+00 1.100000000000000e+00 2.167752659574468e+00 | |
6 3.000000000000000e+00 3.000000000000000e+00 2.358078457446808e+00 | |
0.232875 seconds (1.13 k allocations: 108.984 KiB) | |
julia> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment