1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # -*- coding: utf-8 -*- """SpineDataset.ipynb Automatically generated by Colaboratory. Original file is located at """ from google.colab import drive drive.mount('/gdrive') # /gdrive/My Drive/Colab Notebooks/resources/ <== My resource path import numpy as np import matplotlib.pyplot as plt import time labels = [ b'NO', b'DH', b'SL' ] data = np.loadtxt("/gdrive/My Drive/Colab Notebooks/resources/SPINE/column_3C.dat", converters={6: lambda s: labels.index(s)} ) data.shape x = data[: , 0:6] y = data[: , 6] training_indices = list(range(0 , 20)) + list(range( 4,188)) + list(range(230, 310)) test_indices = list(range(20, 40)) + list(range(188,230)) trainx = x[training_indices,:] trainy = y[training_indices ] testx = x[test_indices,: ] testy = y[test_indices ] """##Functions""" def NN_L2(trainx, trainy, testx): predicY = np.array([ trainy[ find_nn(testI) ] for testI in testx ]) return predicY def find_nn(x): distances = [ l2_dist(x, trainx[i, ]) for i in range(len(trainx)) ] return np.argmin(distances) def l2_dist(v1, v2): return np.sum( np.square(v1-v2) ) def getLabel(idx): return trainy[idx] def NN_L1(trainx, trainy, testx): predicY = np.array([ trainy[ find_nnL1(testI) ] for testI in testx ]) return predicY def find_nnL1(x): distances = [ l1_dist(x, trainx[i, ]) for i in range(len(trainx)) ] return np.argmin(distances) def l1_dist(v1, v2): return np.sum( np.abs(v1-v2) ) def confusion(predicLabel, testLabel): confMat = np.zeros((3,3) , dtype = np.int32) for i in range(len(testLabel)): confMat[int(predicLabel[i]), int(testLabel[i])] += 1 return confMat """##Main""" testy_L2 = NN_L2(trainx, trainy, testx) print( type( testy_L2) ) print( len(testy_L2) ) print( testy_L2[40:50] ) err_positions = np.not_equal(testy_L2, testy) error = float(np.sum(err_positions)) / len(testx) error testy_L1 = NN_L1(trainx, trainy, testx) print( type( testy_L1) ) print( len(testy_L1) ) print( testy_L1[40:50] ) print( all(testy_L1 == testy_L2) ) err_positions = np.not_equal(testy_L1, testy) error = float(np.sum(err_positions)) / len(testx) error resultL2 = confusion(testy_L2, testy) resultL1 = confusion(testy_L1, testy) print(resultL2) print(resultL1) | cs |
출처 및 참고자료 : edx - Machine Learning Fundamentals_week_1 Programming Assignment.2
'Python Library > Machine Learning' 카테고리의 다른 글
Day 05_Multivariate Gaussian_Winery_Classifier_MNIST (0) | 2019.07.13 |
---|---|
Day 05_Multivariate Gaussian_Winery_Classifier (0) | 2019.07.13 |
Day 03.Probability and statistics Review (0) | 2019.07.08 |
Day 02. Implementation Of K-Nearest Neighbor (0) | 2019.07.07 |
Day 01. K-Nearest Neighbor ( KNN ) (0) | 2019.07.07 |