Esempio n. 1
0
def Test_weightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('pc1.arff')

    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "Weightened KNN with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)
        for x in range(k_fold):
            predictions = []
            for i in range(len(testSet[x])):      
                result = calculateNeighborsWeightened(trainingSet[x], testSet[x][i], k)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print 
Esempio n. 2
0
def Test_vdm_weightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('chess.csv')
    
    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "Weightened KNN-VDM with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)
        for x in range(k_fold):
            predictions = []
            lookup_ = misc.buildLookUp(trainingSet[x])
            classes = misc.getClasses(trainingSet[x])
            for i in range(len(testSet[x])):      
                result = calculateNeighborsWeightenedVDM(trainingSet[x],testSet[x][i],k,lookup_,classes)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print 
Esempio n. 3
0
def Test_hvdm_unweightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('crx.data')
    
    for i in range(len(dataset)):
        if dataset[i][13] != '?':
            dataset[i][13] = float(dataset[i][13])

    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "UnWeightened KNN-HVDM with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)

        for x in range(k_fold):
            predictions = []
            maxmin = misc.maxMin(trainingSet[x])
            lookup_ = misc.buildLookUp(trainingSet[x])
            classes = misc.getClasses(trainingSet[x])
            for i in range(len(testSet[x])):      
                result = calculateNeighborsUnWeightenedHVDM(trainingSet[x],testSet[x][i],k,lookup_,classes,maxmin)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print