def loopPcaKnn(loop=1): accuracy = 0 for i in range(loop): iris_data = IrisPCA('iris_data_set/iris.data') iris_data.randomSplit(35) # print (np.array(iris_data.irisdata)) # print (np.array(iris_data.test_data)) # get means and Standard deviation for training data iris_data.calTrainMeanSd(iris_data.train_data) # print ("Mean: \n", iris_data.train_mean) # print ("Standard deviation: \n", iris_data.train_standard_deviation) # apply Z score normalize for training data # print (np.array(iris_data.train_data)) iris_data.zScoreNormalize(iris_data.train_data) # np.set_printoptions(precision=3) # print (np.array(iris_data.train_data)) # get Projection Matrix W iris_data.calProjectionMatrixW(number_of_conponent=2) # print ("\nProjection Matrix W: \n", np.array(iris_data.projectionMatrixW)) # apply Z score normalize for testing data # print (np.array(iris_data.test_data)) iris_data.zScoreNormalize(iris_data.test_data) # print (np.array(iris_data.test_data)) new_train_data = iris_data.getProjectedData(iris_data.train_data) new_test_data = iris_data.getProjectedData(iris_data.test_data) # print (np.array(new_train_data)) # print (np.array(new_test_data)) knn = Knn() # print ("Round ",i+1, " 3-NN accuracy: ", format(knn.kNearestNeighbors(new_train_data, new_test_data), ".3f")) accuracy += knn.kNearestNeighbors(new_train_data, new_test_data) return accuracy / loop
def loopLdaKnn(loop=1): accuracy = 0 for i in range(loop): iris_data = IrisLDA('iris_data_set/iris.data') iris_data.randomSplit(35) iris_data.getMeansForEachClass(iris_data.train_data) iris_data.getScatterMatrices() iris_data.getTranformMatrixW() new_train_data = iris_data.getProjectedData(iris_data.train_data) new_test_data = iris_data.getProjectedData(iris_data.test_data) # print (np.array(iris_data.train_data)) # print (np.array(new_train_data)) knn = Knn() print( "Round ", i + 1, " 3-NN accuracy: ", format(knn.kNearestNeighbors(new_train_data, new_test_data), ".3f")) accuracy += knn.kNearestNeighbors(new_train_data, new_test_data) return accuracy / loop
def icaKnnTest(): iris_data = IrisICA('iris_data_set/iris.data') iris_data.plotIrisData('iris data before ica') iris_data.applyIcaFromFullIris(number_components=4) energy_of_components = iris_data.getSortedComponentEnergy() train_data, test_data = iris_data.getTrainTestSet(energy_of_components[:2], train_size=0.7) iris_data.plotIrisData('iris data after ica') knn = Knn() print(knn.kNearestNeighbors(train_data, test_data)) plt.show()
def icaKnnLoop(loop=10): accuracy = 0 for i in range(loop): iris_data = IrisICA('iris_data_set/iris.data') iris_data.applyIcaFromFullIris(number_components=4) energy_of_components = iris_data.getSortedComponentEnergy() train_data, test_data = iris_data.getTrainTestSet( energy_of_components[:2], train_size=0.7) knn = Knn() current_accuracy = knn.kNearestNeighbors(train_data, test_data) accuracy += current_accuracy print('round ', i + 1, ' accuracy: ', current_accuracy) return accuracy / loop
def loopFaKnn(loop=1): accuracy = 0 for i in range(loop): iris_data = IrisFA('iris_data_set/iris.data') iris_data.randomSplit(35) iris_data.getProjectionMatrixW(k=2) new_train_data = iris_data.getProjectedData(iris_data.train_data) new_test_data = iris_data.getProjectedData(iris_data.test_data) # print (np.array(iris_data.train_data)) # print (np.array(new_train_data)) knn = Knn() # print ("Round ",i+1, " 3-NN accuracy: ", format(knn.kNearestNeighbors(new_train_data, new_test_data), ".3f")) accuracy += knn.kNearestNeighbors(new_train_data, new_test_data) return accuracy / loop