0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

予測モデルの構築

Posted at

自分用の学習のメモ書きですが、予測モデルの構築について、ざっと勉強してみたことをまとめてみました。

クロスバリデーションのスキーム
・K分割クロスバリデーション(Kfold関数)
・層別K分割クロスバリデーション(StratifiedKfold関数)
・一つ抜きクロスバリデーション(LOOCV関数)
・ランダムサンプリング(RandomSub関数)
・層別ランダムサンプリング(StratifiedRandomSum関数)

Juliaを使用した予測モデルの構築
参考ページ
[WindowsへのJuliaインストール] (https://qiita.com/JKirisaka/items/c0736a9e2838710e8aaa)

必要パッケージのインストール
DataFrames.jl・・データフレームの扱いに使用
MLBase.jl・・機械学習の予測モデルの構築に必要な様々な機能を提供(pythonのscikit-learnのようなもの)
DecisionTree.jl・・ランダムフォレストの実行に使用

Pkg.add("DataFrames")
Pkg.add("MLBase")
Pkg.add("DecisionTree")

訓練データとテストデータに使用するデータの取得
*下記の例ではkaggleの参加ページから取得したcsvをtargetdata.csvという名称に置き換えて、カレントディレクトリのdataフォルダ内に配置し、データをインポートしています

using DataFrames
srand(123)
cd("~/data")
getData = readtable("targetdata.csv", separator=';')
*ファイル配置先、csvファイル名は適宜読み替えてください。
N=Size(test,1)
inds_train = rand(1:N, Int(0.7*N))
*Int(0.7*N)がerrorとなる場合は、Int64(0.7*N)を単独計算し、四捨五入した値をInt(0.7*N)の箇所に置き換えます。
X_train = convert(Array, test[inds_train, 1:16])
y_train = convert(Array, test[inds_train, :y])

ランダムフォレストによる予測モデルの構築

using DecisionTree
srand(123)
fit_rf = build_forest(y_train, X_train, 10, 100, 0.7)
fit_rf

inds_test = setdiff(1:N, inds_train)
X_test = convert(Array, bank[inds_test, 1:16])
y_test = convert(Array, bank[inds_test, :y])
pred = apply_forest(fit_rf, X_test)
conf_mat = confusion_matrix(pred, y_test)
conf_mat
conf_mat = conf_mat.matrix
prec = conf_mat[2,2]/sum(conf_mat[2,:])
prec
rec = conf_mat[2,2]/sum(conf_mat[:,2])
rec
# f値
f_value = 2 * prec * rec / (prec + rec)
f_value
acc = sum(diag(conf_mat))/sum(conf_mat)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?