{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport datetime\nfrom tqdm import tqdm\nimport cv2\n\n\nimport os\nprint(os.listdir(\"../input\"))\n\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7871aeddb67be272d0f9c9aeda3b6bec493fd558"},"cell_type":"markdown","source":"## Load data\n\nIn this section, we try to load the data. "},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"\ndef loadTrainData(train_file):        \n    raw_data = np.loadtxt(train_file, skiprows=1, dtype='int', delimiter=',')   \n    x_train = raw_data[:, 1:]   \n    x_val = raw_data[:, 0]   \n    return x_train,x_val\n\n\ndef loadTestData(test_file):   \n    raw_data = np.loadtxt(test_file, skiprows=1, dtype='int', delimiter=',') \n    x_test = raw_data[:,0:]  \n    return x_test\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"526354f5717c2d160aadf030a01a6f0a354c8160"},"cell_type":"code","source":"def getHOGFeature(dataset):      \n    features = []\n    hog = cv2.HOGDescriptor('../input/Opencv HOG Feature/hog.xml')   \n    for img in dataset:       \n        img = np.reshape(img,(28,28)).astype(np.uint8)       \n        hogFeature = hog.compute(img)       \n        features.append(hogFeature)   \n        features = np.array(features)    \n        features = np.reshape(features,(-1,324))   \n    return features\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"071108ebb2b12eae6444f5f8a18b7fd5da60cea2"},"cell_type":"code","source":"def get_data():\n    print(\"load training file...\")\n    train_img, train_label = loadTrainData(\"../input/train.csv\")\n    print(train_img.shape)\n    print(\"load testing file...\")\n    test_img = loadTestData(\"../input/test.csv\")\n    print(test_img.shape)\n    # reshape,方便后续计算\n    train_img=np.reshape(train_img,(len(train_img),28*28))\n    test_img=np.reshape(test_img,(len(test_img),28*28))\n    train_label=np.reshape(train_label,(len(train_img),1))\n    #test_label=np.reshape(test_label,(len(test_label),1))\n    return getHOGFeature(train_img), train_label, getHOGFeature(test_img)#, test_label\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13c3295dbf3e466dd2eb6cb1e7d2a262e2e1b743"},"cell_type":"code","source":"def kDistance(vectorA,vectorB):\n    #2个矩阵的l1范数\n    dis=np.sum(np.fabs((vectorA-vectorB)),axis=1)\n    return dis\n\ndef kNearestNeighbor(testSample,trainSample,trainLabel,k=3):\n    #knn算法实现\n    sampleMat=np.tile(testSample,(trainSample.shape[0],1))\n    disMat=kDistance(sampleMat,trainSample)\n    #计算距离\n    sortDisMat=np.argsort(disMat)\n    #按照距离排序\n    vote={}\n    for sample in range(k):\n        candidate=trainLabel[sortDisMat[sample],0]\n        vote[candidate] = vote.get(candidate, 0) + 1 \n        candMax=0\n    for key, value in vote.items():\n            #多数投票决定最终结果\n        if value > candMax: \n            candMax = value  \n            candIndex = key\n    return candIndex\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe13de891b65bd9260003df834890c179b2861ea"},"cell_type":"code","source":"start=datetime.datetime.now()\n\nimgTrain,labelTrain,imgTest=get_data()\n\ny_predict=[]\ntemp=datetime.datetime.now()\nfor index in tqdm(range(imgTest.shape[0])):\n    y_predict.append(kNearestNeighbor(imgTest[index:index+1,:],imgTrain,labelTrain,k=3))\n\nend=datetime.datetime.now()\n    #print(accuracy(y_predict,labelTest))\n    \n    \nprint(\"Time Consuming Total(second):\",(end-start).seconds)\nprint(\"Time Consuming KNN(second):\",(end-temp).seconds)\n\nwith open(\"submission.csv\", 'w') as f:\n    f.write('ImageId,Label\\n') \n        for i in range(len(y_predict)): \n        f.write(\"\".join([str(i + 1), ',', str(y_predict[i]), '\\n']))\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}