3-cricket-chirp-rate

简介

雪树蟋蟀的鸣叫实际是大腿摩擦发出的声音, 经过数据收集,发现鸣叫的频率和环境温度正相关.

经过线性拟合得到的函数为:C(t)=4.25t-157.8

1. load pacakge

Code
import FileIO:load
import MLJ:fit!,match,predict,table,fitted_params
using GLMakie, CSV,DataFrames,MLJ,FileIO
img=load("./data/snowy-cricket.jpg");

2. process data

Code
df=CSV.File("./data/CricketChirps.csv") |> DataFrame |> dropmissing;
X=MLJ.table(reshape(df[:,1],7,1))
y=Vector(df[:,2])

test_X=range(extrema(df[:,1])...,50)
test_X=MLJ.table(reshape(test_X,50,1))
cols=names(df)
2-element Vector{String}:
 "Temperature"
 "Chirps"

3. MLJ workflow

3.1 fitting model

Code
    LinearRegressor = @load LinearRegressor pkg=MLJLinearModels
    mach = fit!(machine(LinearRegressor(), X, y))
    report(mach)
[ Info: For silent loading, specify `verbosity=0`. 
┌ Warning: The number and/or types of data arguments do not match what the specified model
│ supports. Suppress this type check by specifying `scitype_check_level=0`.
│ 
│ Run `@doc MLJLinearModels.LinearRegressor` to learn more about your model's requirements.
│ 
│ Commonly, but non exclusively, supervised models are constructed using the syntax
│ `machine(model, X, y)` or `machine(model, X, y, w)` while most other models are
│ constructed with `machine(model, X)`.  Here `X` are features, `y` a target, and `w`
│ sample or class weights.
│ 
│ In general, data in `machine(model, data...)` is expected to satisfy
│ 
│     scitype(data) <: MLJ.fit_data_scitype(model)
│ 
│ In the present case:
│ 
│ scitype(data) = Tuple{Table{AbstractVector{Continuous}}, AbstractVector{Count}}
│ 
│ fit_data_scitype(model) = Tuple{Table{<:AbstractVector{<:Continuous}}, AbstractVector{Continuous}}
└ @ MLJBase ~/.julia/packages/MLJBase/fEiP2/src/machines.jl:230
[ Info: Training machine(LinearRegressor(fit_intercept = true, …), …).
┌ Info: Solver: MLJLinearModels.Analytical
│   iterative: Bool false
└   max_inner: Int64 200
import MLJLinearModels ✔

3.2 plot fitting curve

Code
yhat=predict(mach,test_X).|>(d->round(d,digits=2))
function plot_fitting_curve(df,yhat)
    X=df[:,1]
    test_X=range(extrema(df[:,1])...,50)
    cols=names(df)
    fig=Figure()
    ax=Axis(fig[1:3,1:3];xlabel="$(cols[1])",ylabel="$(cols[2])",title="cricket-chirp")
    ax2 = Axis(fig[2,4],title="snowy-tree-cricket")
    scatter!(ax, X,y,markersize=16,color=(:red,0.8))
    lines!(ax, test_X,yhat,color=:blue)
    image!(ax2,img)
    hidespines!(ax2)
    hidedecorations!(ax2)
    fig
end
plot_fitting_curve(df,yhat)