LoginSignup
3
3

More than 3 years have passed since last update.

「Rによる機械学習」の勉強履歴(2)

Last updated at Posted at 2019-06-17

Rによる機械学習

(ソフトウェア品質技術者のための)データ分析勉強会で、書籍『Rによる機械学習 (Machine Learning with R)』を使用して機械学習を学ぶ。
https://www.amazon.co.jp/dp/4798145114/

第3章 遅延学習-最近傍法を使った分類

疑問点

  • kの選択をする方法の1つに「訓練インスタンスの個数の平方根を最初のkにするという方法」があるらしいが、どのような考え方で”平方根”使うことにしたのかは不明。
  • 遅延学習という言葉が、自分では説明できない。
    • 書籍での説明

学習の厳格な定義の下では、遅延学習器は何も学んでいない。単に訓練データを逐語的に格納しているだけだ。そのため、実際には何もしていない訓練フェーズは非常に高速に通過することができる。もちろん、欠点は予測が訓練と比べて遅くなりがちなことである。

CSVファイルのインポート

RSutdioのポップアップメニュー[Import Dataset...]でCSVファイルのデータの読み込みを試みたが失敗。
image.png

> library(readr)
> datasettest <- read_csv("~/data/wisc_bc_data.csv")
Error: '~/data/wisc_bc_data.csv' does not exist.

試行錯誤するのが手間だったので、自分の知っているRcmdrを使ったCSVファイルのデータ読み込みの方法を使用。

> library(Rcmdr)

Rcmdrのメニュー[データ]-[データのインポート]-[テキストファイルまたはクリップボード、URLから...]でCSVファイルのデータの読み込み。
image.png
image.png

読み込み成功。RSutdioでもデータの確認可能。
image.png

> str(wbcd)
'data.frame':   569 obs. of  32 variables:
 $ id               : int  87139402 8910251 905520 868871 9012568 906539 925291 87880 862989 89827 ...
 $ diagnosis        : Factor w/ 2 levels "B","M": 1 1 1 1 1 1 1 2 1 1 ...
 $ radius_mean      : num  12.3 10.6 11 11.3 15.2 ...
 $ texture_mean     : num  12.4 18.9 16.8 13.4 13.2 ...
 $ perimeter_mean   : num  78.8 69.3 70.9 73 97.7 ...
 $ area_mean        : num  464 346 373 385 712 ...
 $ smoothness_mean  : num  0.1028 0.0969 0.1077 0.1164 0.0796 ...
 $ compactness_mean : num  0.0698 0.1147 0.078 0.1136 0.0693 ...
 $ concavity_mean   : num  0.0399 0.0639 0.0305 0.0464 0.0339 ...
 $ points_mean      : num  0.037 0.0264 0.0248 0.048 0.0266 ...
 $ symmetry_mean    : num  0.196 0.192 0.171 0.177 0.172 ...
 $ dimension_mean   : num  0.0595 0.0649 0.0634 0.0607 0.0554 ...
 $ radius_se        : num  0.236 0.451 0.197 0.338 0.178 ...
 $ texture_se       : num  0.666 1.197 1.387 1.343 0.412 ...
 $ perimeter_se     : num  1.67 3.43 1.34 1.85 1.34 ...
 $ area_se          : num  17.4 27.1 13.5 26.3 17.7 ...
 $ smoothness_se    : num  0.00805 0.00747 0.00516 0.01127 0.00501 ...
 $ compactness_se   : num  0.0118 0.03581 0.00936 0.03498 0.01485 ...
 $ concavity_se     : num  0.0168 0.0335 0.0106 0.0219 0.0155 ...
 $ points_se        : num  0.01241 0.01365 0.00748 0.01965 0.00915 ...
 $ symmetry_se      : num  0.0192 0.035 0.0172 0.0158 0.0165 ...
 $ dimension_se     : num  0.00225 0.00332 0.0022 0.00344 0.00177 ...
 $ radius_worst     : num  13.5 11.9 12.4 11.9 16.2 ...
 $ texture_worst    : num  15.6 22.9 26.4 15.8 15.7 ...
 $ perimeter_worst  : num  87 78.3 79.9 76.5 104.5 ...
 $ area_worst       : num  549 425 471 434 819 ...
 $ smoothness_worst : num  0.139 0.121 0.137 0.137 0.113 ...
 $ compactness_worst: num  0.127 0.252 0.148 0.182 0.174 ...
 $ concavity_worst  : num  0.1242 0.1916 0.1067 0.0867 0.1362 ...
 $ points_worst     : num  0.0939 0.0793 0.0743 0.0861 0.0818 ...
 $ symmetry_worst   : num  0.283 0.294 0.3 0.21 0.249 ...
 $ dimension_worst  : num  0.0677 0.0759 0.0788 0.0678 0.0677 ...

RStudioでProjectを作成し、Projectフォルダからの相対パスでファイルパスを指定すれば、Read.csv()関数で、読み込める。

「準備」と「数値データの正規化」と「データによるモデルの訓練」

書籍通りにコマンドを実行するだけで、書籍通りの結果が得られる。

table

table()コマンドで、集計ができる。

> table(wbcd$diagnosis)
  B   M 
357 212 

function

関数を作ることもできる。

 normalize <- function(x) {return ((x - min(x))/(max(x)-min(x)))}

image.png

knn

k近傍法分類の構文

分類器の構築と予測
p <- knn(train, test, class, k)
train:数値訓練データを格納するデータフレーム
test:数値テストデータを格納するデータフレーム
class:訓練データの各行のクラスを格納する因子ベクトル
k:投票する最近傍インスタンスの個数を示す整数
この関数は、テストデータフレームの各行から予想したクラスをまとめた因子ベクトルを返す

書籍の説明では、knnのパラメータ名?が「class」となっているが実際に指定するのは「cl」。

> temp_pred <- knn(train = test_train,test = test_test, class = test_train_labels, k = 21)
Error in knn(train = test_train, test = test_test, class = test_train_labels,  : 
  unused argument (class = test_train_labels)
wbcd_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k = 3)

モデル性能の改善

scale

scale()関数は、デフォルトでZスコア標準化を使って値の範囲の調整ができる。

3
3
1

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
3
3