##################################################### # # # test part for testing correctness of # # Perceptron class # # # ##################################################### if __name__ == '__main__': # small example for testing perceptron class import matplotlib.pyplot as plt from read_iris import read_iris_dataset # read train and test from iris dataset trainX, validX, testX, trainY, validY, testY = read_iris_dataset( rand_state=0, train_ratio=0.60, valid_ratio=0.20, test_ratio=0.20) #print(trainX.shape[0], validX.shape[0], testX.shape[0], trainY.shape[0], validY.shape[0], testY.shape[0]) # for classifying Iris setosa #trainY = np.where(trainY == 'Iris-setosa', 1, 0) #testY = np.where(testY == 'Iris-setosa', 1, 0) # for classifying Iris-versicolor trainY = np.where(trainY == 'Iris-versicolor', 1, 0) validY = np.where(validY == 'Iris-versicolor', 1, 0) testY = np.where(testY == 'Iris-versicolor', 1, 0) # for classifying Iris-versicolor #trainY = np.where(trainY == 'Iris-virginica', 1, 0) #testY = np.where(testY == 'Iris-virginica', 1, 0)
# write in file file = open('find_good_adalines.txt', 'w') ratio_of_train_set = 0.7 ratio_of_valid_set = 0.15 ratio_of_test_set = 0.15 min_test_error = 10000.0 best_learning = np.inf best_random_state = np.inf for random_state in range(0, 8): # read train and test from iris dataset trainX, validX, testX, trainY, validY, testY = read_iris_dataset(rand_state=random_state, train_ratio=ratio_of_train_set, valid_ratio=ratio_of_valid_set, test_ratio=1 - (ratio_of_train_set + ratio_of_valid_set), order=order) # print(trainX, testX, trainY, testY) # standard scaler scaler = preprocessing.StandardScaler().fit(trainX) trainX = scaler.transform(trainX) validX = scaler.transform(validX) testX = scaler.transform(testX) # map labels name map = {'Iris-setosa': 1, 'Iris-versicolor': 2, 'Iris-virginica': 3} temp = np.zeros(trainY.shape, dtype=int) for idx, label in enumerate(trainY): temp[idx] = map[trainY[idx][0]]
# test part for testing correctness of # # k_fold function # # # ##################################################### if __name__ == '__main__': # small example for testing k-fold function import numpy as np import matplotlib.pyplot as plt from read_iris import read_iris_dataset from perceptron_one_vs_all import perceptron_one_vs_all # read train and test from iris dataset trainX, testX, trainY, testY = read_iris_dataset(rand_state=0, train_ratio=0.7, test_ratio=0.15) #print(trainX, testX, trainY, testY) #map labels name map = {'Iris-setosa': 1, 'Iris-versicolor': 2, 'Iris-virginica': 3} temp = np.zeros(trainY.shape, dtype=int) for idx, label in enumerate(trainY): temp[idx] = map[trainY[idx][0]] trainY = np.copy(temp) temp = np.zeros(testY.shape, dtype=int) for idx, label in enumerate(testY): temp[idx] = map[testY[idx][0]] testY = np.copy(temp)