标签:
step1: KNN
实验室的电脑太慢了,用Python简直慢的一比,用C写的,跑了一个多小时,识别率93%,很低,继续……
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 int ans[15000]; 7 int label[42010]; 8 int matrix[42010][800]; 9 int test[26000][800]; 10 int m, n; 11 int MAXN = 784; 12 int vis[10]; 13 14 typedef struct node 15 { 16 int val, cla; 17 }node; 18 node nd[42010]; 19 20 int cmp(const void* a, const void* b) 21 { 22 return ((node*)a)->val - ((node*)b)->val; 23 } 24 25 int knn(int k, int idx) 26 { 27 printf("Dealing %d\n", idx); 28 int cnt = 0; 29 for (int i = 0; i < m; ++i) 30 { 31 cnt = 0; 32 for (int j = 0; j < MAXN; ++j) 33 { 34 if (matrix[i][j] != test[idx][j]) cnt++; 35 } 36 nd[i].val = cnt, nd[i].cla = label[i]; 37 } 38 qsort(nd, m, sizeof(node), cmp); 39 for (int i = 0; i < k; ++i) 40 vis[nd[i].cla]++; 41 int mx = 0, ret = 0; 42 for (int i = 0; i < 10; ++i) 43 { 44 if (vis[i] > mx) mx = vis[i], ret = i; 45 vis[i] = 0; 46 } 47 return ret; 48 } 49 50 int main() 51 { 52 /* 53 int i = 0; 54 FILE *fp; 55 fp = freopen("E:\\Kaggle\\Digit Recognizer\\train.csv", "r", stdin); 56 while (~scanf("%d", &label[i])) 57 { 58 for (int j = 0; j < 784; ++j) 59 { 60 scanf(",%d", &matrix[i][j]); 61 if (matrix[i][j] != 0) matrix[i][j] = 1; 62 } 63 i++; 64 } 65 printf("Hello\n"); 66 m = i; 67 fclose(fp); 68 FILE* fp1; 69 fp1 = freopen("E:\\Kaggle\\Digit Recognizer\\test1.csv", "r", stdin); 70 i = 0; 71 while (~scanf("%d", &test[i][0])) 72 { 73 for (int j = 1; j < 784; ++j) 74 { 75 scanf(",%d", &test[i][j]); 76 if (test[i][j] != 0) test[i][j] = 1; 77 } 78 i++; 79 } 80 printf("Hello\n"); 81 fclose(fp1); 82 n = i; 83 FILE *out1; 84 out1 = fopen("E:\\Kaggle\\Digit Recognizer\\ans1.txt", "w+"); 85 for (int j = 0; j < n; ++j) 86 fprintf(out1, "%d\n", knn(100, j)); 87 fclose(out1); 88 */ 89 FILE *out1; 90 out1 = freopen("E:\\Kaggle\\Digit Recognizer\\ans1.txt", "r", stdin); 91 FILE *ot; 92 ot = fopen("E:\\Kaggle\\Digit Recognizer\\ans2.txt", "w+"); 93 int i = 1; 94 int val; 95 while (~scanf("%d", &val)) 96 { 97 fprintf(ot, "%d,%d\n", i, val); 98 i++; 99 } 100 fclose(ot); 101 fclose(out1); 102 103 return 0; 104 }
标签:
原文地址:http://www.cnblogs.com/JustForCS/p/5122646.html