서민관, 박희정 팀 ¶
- 코드를 올려주세요.
def compare(firstData, secondData): diffSum = 0; firstDataList = [int(i) for i in firstData.split(',')]; secondDataList = [int(i) for i in secondData.split(',')]; for i in range(len(firstDataList)): diffSum += abs(firstDataList[i] - secondDataList[i]); return diffSum; trainData = open('DataSet/train_data11293x8165').readlines(); trainClass = open('DataSet/train_class11293x20').readlines(); testData = open('DataSet/test_data7528x8165').readlines(); testClass = list(); print 'load DataSet finished' similiarIndex = 0; diffValue = 0; leastDiffValue = 10000; for i in range(len(testData)): for j in range(len(trainData)): diffValue = compare(testData[i], trainData[j]); # print 'diffValue : ', diffValue; if diffValue < leastDiffValue: leastDiffValue = diffValue; similiarIndex = j; leastDiffValue = 10000; print 'similiar index : ', similiarIndex; testClass.append(trainClass[similiarIndex]); f = open("test_class") for i in testClass: f.write(i);
MachineLearning.cpp
#include <iostream> #include <fstream> #include <vector> #include <sstream> #include <istream> using namespace std; std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { std::stringstream ss(s); std::string item; while (std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; split(s, delim, elems); return elems; } int compare(string firstData, string secondData) { vector<string> firstDataList = split(firstData, ','); vector<string> secondDataList = split(secondData, ','); int diffValue = 0; for ( int i = 0; i < firstDataList.size(); i++ ) { diffValue += abs(atoi(firstDataList[i].c_str()) - atoi(secondDataList[i].c_str())); } return diffValue; } int main(int argc, char *argv[]) { ifstream trainData; trainData.open("train_data11293x8165"); vector<string> trainDataList = vector<string>(); ifstream trainClass; trainClass.open("train_class11293x20"); vector<string> trainClassList = vector<string>(); ifstream testData; testData.open("test_data7528x8165"); vector<string> testDataList = vector<string>(); vector<string> testClass = vector<string>(); int leastDiffValue = 10000; int similiarIndex = 0; cout << "File Open Finished" << endl; string line; int idx = 0; while ( trainData.good() ) { getline(trainData, line); trainDataList.insert(trainDataList.end(), line); cout << "index : " << idx++ << endl; cout << "data[0] : " << line[0] << endl; } while ( trainClass.good() ) { getline(trainClass, line); trainClassList.insert(trainClassList.end(), line); } while ( testData.good() ) { getline(testData, line); testDataList.insert(testDataList.end(), line); } cout << "File load Finished" << endl; for ( int i = 0; i < testDataList.size(); i++ ) { for ( int j = 0; j < trainDataList.size(); j++ ) { int diffValue = compare(testDataList[i], trainDataList[j]); if ( diffValue < leastDiffValue ) { leastDiffValue = diffValue; similiarIndex = j; } } leastDiffValue = 10000; cout << "similiar index : " << similiarIndex << endl; testClass.insert(testClass.end(), trainClassList[similiarIndex]); } ofstream outputFile; outputFile.open("Test_Class"); for ( int i = 0; i < testClass.size(); i++ ) { outputFile << testClass[i] << endl; } outputFile.close(); trainData.close(); trainClass.close(); testData.close(); return 0; }
안혁준, 남근우 팀 ¶
- 코드를 올려주세요.
#include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> using namespace std; struct DArray { int ** data; int col; int row; }; int ** allocArr(int row, int col); void deallocArr(int ** target, int row); void readFile(int ** target, const char * filename, int row, int col); void findClass(DArray train_data, DArray train_class, DArray test_data); int main(){ char buf[1024*1024]; //alloc int ** train_data = allocArr(11293, 8165); int ** train_class = allocArr(11293, 20); int ** test_data = allocArr(7528, 8165); readFile(train_data, "DataSet/train_data11293x8165", 11293, 8165); readFile(train_class, "DataSet/train_class11293x20", 11293, 20); readFile(test_data, "DataSet/test_data7528x8165", 7528, 8165); DArray train_d; DArray train_c; DArray test_d; train_d.data = train_data; train_d.row = 11293; train_d.col = 8163; train_c.data = train_class; train_c.row = 11293; train_c.col = 20; test_d.data = test_data; test_d.row = 7528; test_d.col = 8163; findClass(train_d, train_c, test_d); //dealloc deallocArr(train_data, 11293); deallocArr(train_class, 11293); deallocArr(test_data, 7528); } int ** allocArr(int row, int col){ int ** train_data = (int**)malloc(sizeof(int*) * row); for(int i = 0; i < row; i++){ train_data[i] = (int*)malloc(sizeof(int) * col); } return train_data; } void deallocArr(int ** target, int row){ for(int i = 0; i < row; i++){ free(target[i]); } free(target); } void readFile(int ** target, const char * filename, int row, int col){ FILE * file = fopen(filename, "r"); for(int i = 0; i < row; i++){ for( int j = 0; j < col; j++){ if(j < col) fscanf(file, "%d,", &target[i][j]); else fscanf(file, "%d", &target[i][j]); } } fclose(file); } void findClass(DArray train_data, DArray train_class, DArray test_data){ for(int i = 0; i < test_data.row; i++){ //test_data[i]; int min_index = -1; int min = 1000; for(int j = 0; j < train_data.row; j++){ //train_data[j] int sum = 0; for(int k =0; k < train_data.col; k++){ int v = test_data.data[i][k] - train_data.data[j][k]; v = v >0 ? v : -v; sum += v; } if(sum < min){ min_index = j; min = sum; } } //min이 어떤 클래스 인지. for(int j = 0; j < train_class.col; j++){ if(train_class.data[min_index][j]){ printf("%d\n", j); } } } }