これはAWK言語を使用しています。主にステップ2の問題があります。サンプルデータセットを示しましたが、元のデータセットは100フィールドと2000レコードで構成されています。awkを使用する最近隣の1人
アルゴリズム
1)R0のための最近傍を検索するには、各レコードrため
Find the closest other record, o, in the dataset using distance formula
を精度= 0
2)を初期化し、IはR9にR1とR0を比較するために必要と次のように計算します。square(abs(r0.c1 - r1.c1))+ square ) とそれらの距離を格納します。
3)最小距離を1つ、そのc6値を比較します。 c6の値が等しい場合は、精度を1ずつ増やします。
すべてのレコードについて処理を繰り返した後。
4)最終的に、1nn精度のパーセンテージを (精度/合計記録)* 100で取得します。
サンプルデータセット
c1 c2 c3 c4 c5 c6 --> Columns
r0 0.19 0.33 0.02 0.90 0.12 0.17 --> row1 & row7 nearest neighbour in c1
r1 0.34 0.47 0.29 0.32 0.20 1.00 and same values in c6(0.3) so ++accuracy
r2 0.37 0.72 0.34 0.60 0.29 0.15
r3 0.43 0.39 0.40 0.39 0.32 0.27
r4 0.27 0.41 0.08 0.19 0.10 0.18
r5 0.48 0.27 0.68 0.23 0.41 0.25
r6 0.52 0.68 0.40 0.75 0.75 0.35
r7 0.55 0.59 0.61 0.56 0.74 0.76
r8 0.04 0.14 0.03 0.24 0.27 0.37
r9 0.39 0.07 0.07 0.08 0.08 0.89
コード
BEGIN {
#initialize accuracy and total_records
accuracy = 0;
total_records = 10;
}
NR==FNR { # Loop through each record and store it in an array
for (i=1; i<=NF; i++)
{
records[i]=$i;
}
next
}
{ # Re-Loop through the file and compare each record from the array with each record in a file
for(i=1; i <= length(records); i++)
{
for (j=1; j<=NF; j++)
{ # here I need to get the difference of each field of the record[i] with each all the records, square them and sum it up.
distance[j] = (records[i] - $j)^2;
}
#Once I have all the distance, I can simply compare the values of field_6 for the record with least distance.
if(min(distance[j]))
{
if(records[$6] == $6)
{
++accuracy;
}
}
}
END{
percentage = 100 * (accuracy/total_records);
print percentage;
}
あなたはfields [i] = print $ iを意味します。すべてのフィールドを配列に格納しますか?フィールドは独立していますが、一番近い行が見つかると、フィールド[6]にあるclass_valueを見つける必要があります。私が各フィールドを別々にソートすると、データが混乱してしまいました。あなたのアイデアをどうやって伝えるかについてもう少し説明できますか? –
説明にコメントを組み込んだので、今実際の質問は何ですか?私。あなたはステップ2でどんな難しさを持っていますか? – Soren
2つのレコードの各フィールドに式を適用する方法(二乗と和を含む)はわかりません。このビットは間違っています - >> distance [j] =(records [i] - $ j); –