Skip to content

Instantly share code, notes, and snippets.

@hiromu
Last active January 1, 2016 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromu/8085987 to your computer and use it in GitHub Desktop.
Save hiromu/8085987 to your computer and use it in GitHub Desktop.
機械学習コンテスト用サンプルソース
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRAINDATA 50
#define TESTDATA 100
#define PARAMETER 4
#define TYPE 3
#define K 3
float traindata[TRAINDATA][PARAMETER], testdata[PARAMETER], distance[TRAINDATA];
int type[TRAINDATA], rank[TRAINDATA], result[TYPE];
int sort(const void *a, const void *b)
{
if(distance[*(int *)a] < distance[*(int *)b])
return -1;
else if(distance[*(int *)a] > distance[*(int *)b])
return 1;
else
return 0;
}
int main(void)
{
int i, j, k, maximum;
FILE *learn, *test;
// 学習データを読み込んで配列に保存
learn = fopen("train.txt", "r");
for(i = 0; i < TRAINDATA; i++) {
for(j = 0; j < PARAMETER; j++)
fscanf(learn, "%f,", &traindata[i][j]);
fscanf(learn, "%d", &type[i]);
}
fclose(learn);
// テストデータを1行ごとに処理
test = fopen("test.txt", "r");
for(i = 0; i < TESTDATA; i++) {
// テストデータを読み込んで配列に保存
for(j = 0; j < PARAMETER; j++) {
if(j == 0)
fscanf(test, "%f", &testdata[j]);
else
fscanf(test, ",%f", &testdata[j]);
}
// 学習データのそれぞれとのユークリッド距離を求める
for(j = 0; j < TRAINDATA; j++) {
distance[j] = 0;
for(k = 0; k < PARAMETER; k++)
distance[j] += pow(testdata[k] - traindata[j][k], 2);
distance[j] = sqrt(distance[j]);
}
// ユークリッド距離が近い順にソートする
for(j = 0; j < TRAINDATA; j++)
rank[j] = j;
qsort(rank, TRAINDATA, sizeof(int), sort);
// 最も近いK個についてどの種類が多いかカウントする
memset(result, 0, sizeof(result));
maximum = -1;
for(j = 0; j < K; j++) {
result[type[rank[j]]] += 1;
if(result[type[rank[j]]] > maximum)
maximum = result[type[rank[j]]];
}
// 最も多かった種類を出力する
for(j = 0; j < TYPE; j++) {
if(result[j] == maximum) {
printf("%d\n", j);
break;
}
}
}
fclose(test);
return 0;
}
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRAINDATA 200
#define TESTDATA 500
#define PARAMETER 10
#define TYPE 11
#define K 5
float traindata[TRAINDATA][PARAMETER], testdata[PARAMETER], distance[TRAINDATA];
int rating[TRAINDATA], rank[TRAINDATA];
int sort(const void *a, const void *b)
{
if(distance[*(int *)a] < distance[*(int *)b])
return -1;
else if(distance[*(int *)a] > distance[*(int *)b])
return 1;
else
return 0;
}
int main(void)
{
int i, j, k;
float sum;
FILE *learn, *test;
// 学習データを読み込んで配列に保存
learn = fopen("train.txt", "r");
for(i = 0; i < TRAINDATA; i++) {
for(j = 0; j < PARAMETER; j++)
fscanf(learn, "%f,", &traindata[i][j]);
fscanf(learn, "%d", &rating[i]);
}
fclose(learn);
// テストデータを1行ごとに処理
test = fopen("test.txt", "r");
for(i = 0; i < TESTDATA; i++) {
// テストデータを読み込んで配列に保存
for(j = 0; j < PARAMETER; j++) {
if(j == 0)
fscanf(test, "%f", &testdata[j]);
else
fscanf(test, ",%f", &testdata[j]);
}
// 学習データのそれぞれとのユークリッド距離を求める
for(j = 0; j < TRAINDATA; j++) {
distance[j] = 0;
for(k = 0; k < PARAMETER; k++)
distance[j] += pow(testdata[k] - traindata[j][k], 2);
distance[j] = sqrt(distance[j]);
}
// ユークリッド距離が近い順にソートする
for(j = 0; j < TRAINDATA; j++)
rank[j] = j;
qsort(rank, TRAINDATA, sizeof(int), sort);
// 最も近いK個についてレーティングの平均を求める
sum = 0;
for(j = 0; j < K; j++)
sum += rating[rank[j]];
printf("%d\n", (int)round(sum / K));
}
fclose(test);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment