2016-06-17 4 views
-2

私はこの場所からどこで始めるべきか分かりません。 Rに組み込まれたIRISデータセットを使用した私の割り当ては:Rの近くのクラシファイアを使用してクラスラベルを割り当てます

最近接分類器(NN)[1]を使用して各観測データ(単一次元)に単一のクラスラベルを割り当てるRコードを書く]。 Part Aを参照データベース(観測およびクラスラベル)として使用し、パートBをテストセットとして使用します。パートBからの各観測がパートAからの最も近い観測を見つけ、そのクラスラベルをパートAからの観測に割り当てるために、パートBのクラスラベルを知らないと仮定します。

クラスごとの精度(1クラスあたりの正確に分類された観測数を観測数の合計で割ったもの)を計算して返します。

私は最初の部分のために書かれたコードは非常に簡単です:

newData = iris 

evenRows.A <- newData[seq(2, nrow(newData), 2),] #SELECT EVEN ROWS 

oddRows.B <- newData[seq(1, nrow(newData), 2),] #SELECT ODD ROWS. This is the testing set 

クラスラベル上の任意の助けすでにいただければ幸いです。

はEDIT:フォーマットRコード

答えて

1
newData = iris 
    evenRows.A <- newData[seq(2, nrow(newData), 2),] #SELECT EVEN ROWS 
    oddRows.B <- newData[seq(1, nrow(newData), 2),] #SELECT ODD ROWS. This is the testing set 

normalize <- function(x){return((x-min(x))/(max(x)-min(x)))}  #Define a function to normalize the data 
    evenRows.train<- as.data.frame(normalize(evenRows.A[,c(1,2,3,4)])) #Apply normalization to part A, the reference data 
    oddRows.test<- as.data.frame(normalize(oddRows.B[,c(1,2,3,4)])) #Apply normalization to part B, the test data 
    evenRows.train.target<-evenRows.A[,5] 

    require(class) #load required classes for nearest neighbor modelling 
    sqrt(150) 
    #rule of thumb: pick k= sqrt(observations), rounded to nearest odd integer. In this case, 12.247 --> k = 13 

    model1<-knn(train=evenRows.train, test=oddRows.test, cl=evenRows.train.target, k=13) 
    model1 

    #Display confusion matrix of results, to quantify correct versus incorrect classification 

    table(oddRows.test.target, model1) 

Iがk最近傍分類を適用する試みがあり、端に混同行列として結果を表示します。これが私が投稿した質問に対する賢明なアプローチであるかどうかは、皆に教えていただけますか?

関連する問題