LoginSignup
3
0

More than 3 years have passed since last update.

R言語によるSVM(機械学習)

Posted at

はじめに

 R言語を用いたサポートベクターマシンにより、テキスト分類を行ってみました。
 テキストのベクトル化などについて、詳しくは、
リンク(GitHub) を参照ください。

データ

 テキストは、文書内の語の出現頻度の指標である「tf-idf」を用いてベクトル化し、次のような形状のcsvファイルになっています。

num,。,が,の,...,おめでとう,くん,笑,group
0,0.000576,0.018227,0.018290,...,0.036156,0.019288,0.012082,ctrl
1,0.012769,0.026343,0.016902,...,0.007307,0.020576,0.013051,dep
2,0.000437,0.000360,0.000364,...,0.002279,0.001640,0.002097,ctrl
...

最終列(group列)の、"ctrl" or "dep" が、ラベルです。

R言語によるサポートベクターマシンの利用

kernlabライブラリの、ksvm関数を用いました。

>library(kernlab)

>setwd("//")

>train=read.csv("trainデータの一覧.csv")

>test=read.csv("testデータの一覧.csv")

>svm<-ksvm(group ~., data=train)

>predict<-predict(svm, test)

>predict

>table(predict,test$group)

ksvm関数のパラメータをきちんと調整すると、さらに良いかもしれません。

詳しくは、
リンク(GitHub) を参照ください。

環境

R version 3.6.1
macOS Catalina 10.15.3

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