示例#1
0
def population_fitness(train_features, train_labels, test_features,
                       test_labels, population):
    # 计算种群每个个体的自适应度值
    population_fit = np.zeros(len(population))
    for i in range(len(population)):
        index = population[i]
        train_feature_exp = train_features[:, index == 1]
        test_features_exp = test_features[:, index == 1]
        # predictY, accuracy = myKNN(k, trainX, trainY, testX, testY)
        predictY, accuracy = myKNN(1, train_feature_exp, train_labels,
                                   test_features_exp, test_labels)
        population_fit[i] = accuracy
    return population_fit
示例#2
0
def kCrossValidation(allData,allLabel,trueLabel,countArr):
    kf = KFold(n_splits=5, shuffle=True)
    # m = len(countArr)
    # print 'm is ', m
    # temp = np.zeros((m, 1))
    # for i in range(m):
    #     temp[i] = i
    accuracy = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
    trueAccuracy = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
    for trainIndex, testIndex in kf.split(trueLabel):
        trainData, trainLabel, trainCountArr, trainTrueLabel, testData, testLabel, testCountArr, testTrueLabel = getTrainTestData(allData, allLabel, trueLabel, countArr, trainIndex, testIndex)
        a, b = knn.myKNN(trainData, trainLabel, testData, testLabel, testTrueLabel, testCountArr)
        accuracy += a
        trueAccuracy += b
    accuracy = np.array(accuracy)/5
    trueAccuracy = np.array(trueAccuracy)/5
    print 'average Accuracy is ', accuracy
    print 'average TrueAccuracy is', trueAccuracy
    print 'allAverageTrueAccuracy is ', np.sum(trueAccuracy)/9
    plt.plot(trueAccuracy)
    plt.show()
import numpy as np
from myKNN import myKNN

trn_data = np.loadtxt('optdigits_train.txt', delimiter = ',')
tst_data = np.loadtxt('optdigits_test.txt', delimiter = ',')

training_feature_matrix = trn_data[:,:-1]
training_response = trn_data[:,-1]
test_feature_matrix = tst_data[:,:-1]
test_response =  tst_data[:,-1]

k_values = [1,3,5,7]

error_rates_dict = {}
for x in k_values :
    print("KNN for k value :", x)
    predictions = myKNN(trn_data, tst_data, x)
    error = 0
    for j in range(len(predictions)) :
        if(test_response[j] != predictions[j]) :
            error += 1

    print("Number of errors :", error)
    error_rate = error / len(predictions)
    print("Error rate for k :",x, " is = ", error_rate)
    error_rates_dict[x] = error_rate

    print("===============================================================================")

print(error_rates_dict)
示例#4
0
    print("Projection into dimension of size :", l)
    print()
    matrix_w, eigen_values = myLDA(trn_data, l)
    train_lda = training_feature_matrix.dot(matrix_w)
    test_lda = test_feature_matrix.dot(matrix_w)

    train_lda_concat = np.hstack(
        (train_lda, training_response.reshape(1500, 1)))
    test_lda_concat = np.hstack((test_lda, test_response.reshape(297, 1)))

    k_values = [1, 3, 5]

    error_rates_dict = {}
    for x in k_values:
        print("KNN for k value :", x)
        predictions = myKNN(train_lda_concat, test_lda_concat, x)
        error = 0
        for j in range(len(predictions)):
            if (test_response[j] != predictions[j]):
                error += 1

        print("Number of errors :", error)
        error_rate = error / len(predictions)
        print("Error rate for k :", x, " is = ", error_rate)
        error_rates_dict[x] = error_rate

        print(
            "==============================================================================="
        )

    print(error_rates_dict)
      eigen_vec_count)

matrix_w, eigen_vals_pc = myPCA(trn_data, eigen_vec_count)

train_pca = training_feature_matrix.dot(matrix_w)
test_pca = test_feature_matrix.dot(matrix_w)

train_pca_concat = np.hstack((train_pca, training_response.reshape(500, 1)))
test_pca_concat = np.hstack((test_pca, test_response.reshape(124, 1)))

k_values = [1, 3, 5, 7]

error_rates_dict = {}
for x in k_values:
    print("KNN for k value :", x)
    predictions = myKNN(train_pca_concat, test_pca_concat, x)
    error = 0
    for j in range(len(predictions)):
        if (test_response[j] != predictions[j]):
            error += 1

    print("Number of errors :", error)
    error_rate = error / len(predictions)
    print("Error rate for k :", x, " is = ", error_rate)
    error_rates_dict[x] = error_rate

    print(
        "==============================================================================="
    )

print(error_rates_dict)