def cross_validate_parameters(dataset_name, train_set, test_set): dims = [i for i in range(1, 7)] sigmas = [0.001, 0.01, 0.1, 1, 2, 5] accuracies = [] best_accu_and_dim = 0, 0 max = 0 for dim in dims: clf_poly = Perceptron.Perceptron(dataset_name, train_set[:, :-1], train_set[:, -1], _kernel_=2, epochs=10, _dim_=dim) accuracy, accuracy_index = tests.best_epoch(clf_poly, test_set, 10) # clf_poly.fit() # predicted = clf_poly.predict_set(test_set[:, :-1], test_set[:, -1]) # accuracy = clf_poly.accuracy(test_set[:, -1], predicted) accuracies.append((accuracy, dim)) for accu, dim in accuracies: if accu > max: max = accu best_accu_and_dim = accu, dim accuracies = [] best_accu_and_sigma = 0, 0 max = 0 for sigma in sigmas: clf_gaussian = Perceptron.Perceptron(dataset_name, train_set[:, :-1], train_set[:, -1], _kernel_=3, epochs=10, _sigma_=sigma) accuracy, accuracy_index = tests.best_epoch(clf_gaussian, test_set, 10) # clf_gaussian.fit() # predicted = clf_gaussian.predict_set(test_set[:, :-1], test_set[:, -1]) # accuracy = clf_gaussian.accuracy(test_set[:, -1], predicted) accuracies.append((accuracy, sigma)) for accu, sigma in accuracies: if accu > max: max = accu best_accu_and_sigma = accu, sigma print("Best accuracy with associated dim: ", best_accu_and_dim, "\n", "Best accuracy with associated sigma :", best_accu_and_sigma) return best_accu_and_dim, best_accu_and_sigma
def test_xor(self): NAND = fortest.Perceptron(w1=-0.5, w2=-0.5, b=0.7) OR = fortest.Perceptron(w1=0.5, w2=0.5, b=-0.2) AND = fortest.Perceptron(0.5, 0.5, -0.7) #Perceptron can not solve the nonlinear problem, but it is possible with multilayer perceptron. #Since the XOR gate is a nonlinear problem, it is implemented with a multilayer perceptron. def XOR(x1, x2): return AND.do(NAND.do(x1, x2), OR.do(x1, x2)) self.assertEqual(0, XOR(0, 0)) self.assertEqual(1, XOR(1, 0)) self.assertEqual(1, XOR(0, 1)) self.assertEqual(0, XOR(1, 1))
def Perceptron_Learning_Test(num, iter): Neuron = pc.Perceptron(func.Step(), 2, 0.1) data = np.zeros((num, 2)) expected = np.zeros((num)) for d in range(num): data[d][0] = random.uniform(-50.0, 50.0) data[d][1] = random.uniform(-120.0, 120.0) if 3 * data[d][0] + 5 < data[d][1]: expected[d] = 1 else: expected[d] = 0 start_params = [Neuron.weights, Neuron.bias] out = np.zeros((num)) for i in range(iter): for j in range(num): out[j] = Neuron.feed(data[j]) Neuron.train(data[j], expected[j]) acc = 0 for a in range(num): if expected[a] == out[a]: acc += 1 print((acc / num) * 100)
def main(): df = pd.read_csv( 'https://archive.ics.uci.edu/ml/' 'machine-learning-databases/iris/iris.data', header=None) y = df.iloc[:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) X = df.iloc[:100, [0, 2]].values ''' plt.figure() plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa') plt.scatter(X[50:, 0], X[50:, 1], color='blue', marker='x', label='versicolor') plt.xlabel('petal length (cm)') plt.ylabel('sepal length (cm)') plt.legend(loc='upper left') plt.show() ''' plt.figure() ppn = Perceptron.Perceptron(eta=0.1, n_iter=10) ppn.fit(X, y) #plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o') plot_decision_regions(X, y, classifier=ppn) plt.xlabel('petal length (cm)') plt.ylabel('sepal length (cm)') plt.legend(loc='upper left') plt.show()
def cangrejos(): print "\n--> Cangrejos" tlu = Perceptron(random.random(), [ random.random(), random.random(), random.random(), random.random(), random.random() ]) print "Con los datos:" print "Pesos: ", tlu.weights print "Umbral: ", tlu.threshold print "Coeficiente", tlu._lambda print "Resultado:" print "\t-->Female" if tlu.execute([17.7, 13.6, 38.7, 44.5, 16 ]) else "\t-->Male" print "\t-->Female" if tlu.execute([15.1, 13.8, 31.7, 36.6, 13 ]) else "\t-->Male" tlu.setDataTraining(crabData) tlu.training() print print "Con los datos:" print "Pesos: ", tlu.weights print "Umbral: ", tlu.threshold print "Coeficiente", tlu._lambda print "Resultado:" print "\t-->Female" if tlu.execute([17.7, 13.6, 38.7, 44.5, 16 ]) else "\t-->Male" # Debe ser Male print "\t-->Female" if tlu.execute([15.1, 13.8, 31.7, 36.6, 13 ]) else "\t-->Male" # Debe ser Female
def main(): #read dataset from file df = pd.read_csv('iris.data', header=None) #select setosa e versicolor y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) #extract sepal length and pental length X = df.iloc[0:100, [0, 2]].values #plot data ''' plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa') plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor') plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc='upper left') plt.show() ''' ppn = pc.Perceptron(eta=0.1, n_iter=10) ppn.fit(X, y) ''' plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o') plt.xlabel('Epochs') plt.ylabel('Number of updates') plt.show() ''' plot_decision_regions(X, y, classifier=ppn) plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc='upper left') plt.show()
def main(ws): n = len(ws) train_l = [] data_l = [] for t in range(TRAIN_N): xs = [] for i in range(n): xs.append(random.randint(0, 200) / 10 - 10) train_l.append((xs, [function(ws, xs)])) for t in range(DATA_N): xs = [] for i in range(n): xs.append(random.randint(0, 200) / 10 - 10) data_l.append((xs, [function(ws, xs)])) perceptron = P.Perceptron(n, 8, 1, 0.1) perceptron.train(train_l) count = 0 for data in data_l: (xs, t) = data result = perceptron.execute(xs) if result == t: count += 1 print(count / DATA_N)
def main(args): training_set = utilis.readExamples(args[1]) training_labels_set = utilis.readLabelSet(args[2]) init_weights = np.zeros((3, training_set.shape[1])) test_set = utilis.readExamples(args[3]) norm_tra_set, norm_tes_set = norm.Min_Max_normalization( training_set, test_set, MAX_RANGE, MIN_RANGE) seed = utilis.generate_Seed(SEED_RANGE, BIAS) perc_obj = Perceptron.Perceptron(PERC_EPOCHS, PERC_RATE, norm_tra_set, training_labels_set, seed) perceptron_weights = perc_obj.training(None, None, init_weights) pa_obj = PA.PA(PA_EPOCHS, norm_tra_set, training_labels_set, seed) pa_weights = pa_obj.training(None, None, init_weights) norm_tra_set, norm_tes_set = norm.Zscore_normalization( training_set, test_set) svm_obj = SVM.SVM(SVM_EPOCHS, SVM_LAMBDA, SVM_RATE, norm_tra_set, training_labels_set, seed) svm_weights = svm_obj.training(None, None, init_weights) utilis.printThePredictions(perceptron_weights, svm_weights, pa_weights, test_set)
def train(X, y): model = Perceptron(eta=0.001, n_iter=400) res = model.fit(X, y) print("w=", res.w_) #print("errors=", res.errors_) #showAfterTrainingErrors(res.errors_) return model
def __init__(self, genome=None): # network = FFNeuralNetwork(INPUT_NODES, NODES_PER_HIDDEN_LAYER, OUTPUT_NODES) network = Perceptron(INPUT_NODES, OUTPUT_NODES) if not genome: genome = network.getWeights() else: network.setWeights(genome) self.genome = genome self.network = network
def sumar(self, input): nand_L1N1 = Perceptron([-2, -2], 3) output_L1N1 = nand_L1N1.output(input) nand_L2N3 = Perceptron([-2, -2], 3) carry_bit = nand_L2N3.output([output_L1N1, output_L1N1]) nand_L2N1 = Perceptron([-2, -2], 3) output_L2N1 = nand_L2N1.output([input[0], output_L1N1]) nand_L2N2 = Perceptron([-2, -2], 3) output_L2N2 = nand_L2N2.output([input[1], output_L1N1]) nand_L3N1 = Perceptron([-2, -2], 3) sum = nand_L3N1.output([output_L2N1, output_L2N2]) return [sum, carry_bit]
def fit_perceptron(self): X = np.array(self.df.iloc[:, :-1]) y = np.array(self.df.iloc[:, -1]) uniform_X = uniform_vector(X, y) max_epochs = self.ui.max_epochs_sb.value() weights, num_epochs = Perceptron(uniform_X, max_epochs) self.ui.epochs_num_te.setText(str(num_epochs)) self.ui.weights_matrix_te.setText(str(weights))
def __init__(self): conv1 = ConvLayer(28, 28, 1, 6, 5, 1, 2) sigmoid1 = Sigmoid() pool1 = Pool(2) conv2 = ConvLayer(14, 14, 6, 16, 5, 1, 0) sigmoid2 = Sigmoid() pool2 = Pool(2) fc = Perceptron([400, 600, 10]) self.layers = [conv1, sigmoid1, pool1, conv2, sigmoid2, pool2, fc]
def test_for_each_kernel(dataset_name, train_set, test_set, max_epochs, test_on_train, dataset_index): best_accuracies_per_kernel = {} for i in range(1, 4): clf = Perceptron.Perceptron(dataset_name, train_set[:, :-1], train_set[:, -1], _kernel_=i, epochs=10, _sigma_=cv.best_parameters[i - 1, dataset_index], _dim_=cv.best_parameters[i - 1, dataset_index]) best_epoch_accuracy, best_epoch_accuracy_index = best_epoch( clf, test_set, max_epochs) if test_on_train: clf_train = Perceptron.Perceptron( dataset_name, train_set[:, :-1], train_set[:, -1], _kernel_=i, epochs=best_epoch_accuracy_index, _sigma_=cv.best_parameters[i - 1, dataset_index], _dim_=cv.best_parameters[i - 1, dataset_index]) clf_train.fit() predicted = clf_train.predict_set(train_set[:, :-1], train_set[:, -1]) best_epoch_accuracy = clf_train.accuracy(train_set[:, -1], predicted) best_accuracies_per_kernel[i] = best_epoch_accuracy ''' if not test_on_train: # adding the results from sklearn perceptron clf = SkPerceptron(tol=1e-3, random_state=0) clf.fit(train_set[:, :-1], train_set[:, -1]) best_accuracies_per_kernel[4] = clf.score(train_set[:, :-1], train_set[:, -1]) * 100 ''' return best_accuracies_per_kernel
def final_format(data_file, lables_file, test_file): # Getting the data and convert it to be useable by the algorthems data = flr.read_from_file(data_file) data = dm.add_bias(data) data = dm.convert_sex_to_number(data, 1, 1, 0) data = np.array(data) # Normalaize the data min_range = np.ones(data.shape[1]) min_range = np.multiply(-1, min_range) max_range = np.ones(data.shape[1]) data = dm.min_max_normalization(data, min_range, max_range) # Getting the lables and convert it to be useable by the algorthems lables = flr.read_from_file(lables_file) lables = np.array(lables) lables = dm.convert_to_float(lables) # Get the test data and convert it to be useable by the algorthems test = flr.read_from_file(test_file) test = dm.add_bias(test) test = dm.convert_sex_to_number(test, 1, 1, 0) test = np.array(test) # Normalaize the test data min_range = np.ones(data.shape[1]) min_range = np.multiply(-1, min_range) max_range = np.ones(data.shape[1]) test = dm.min_max_normalization(test, min_range, max_range) # Set the properties we want the algorithems to use to use ignore = np.ones(len(data[0])) # Perceptron algorithem alg_peceptron = prtn.Perceptron(data, 0.1, 100, ignore, 3) alg_peceptron.train(data, lables, 0.01, 20) # Svm algorithem alg_svm = svm.Svm(data, 0.01, 100, ignore, 0.001, 3) alg_svm.lamda = 0.1 alg_svm.train(data, lables, 0.1, 20) # Pa algorithem alg_pa = pa.Pa(data, 0.1, 100, ignore, 3) alg_pa.train(data, lables, 0.01, 25) # Compare the algoritems on the test for test_data in test: line_to_print = "perceptron: " + str( alg_peceptron.predict(test_data)) + ", " line_to_print += "svm: " + str(alg_svm.predict(test_data)) + ", " line_to_print += "pa: " + str(alg_pa.predict(test_data)) print(line_to_print)
def __init__(self): #part A_check_only self.only_p11 = Perceptron(100, 100) self.only_p12 = Perceptron(-100, 0) self.only_p13 = Perceptron(-100, 0) self.only_p14 = Perceptron(-100, 0) self.only_p21 = Perceptron([20, 20, 20, 20], 80) self.only_p31 = Perceptron([100, 100, 100, 100], 100) #part_B_check_cross self.cross_p11 = Perceptron([20, 20, 20, 20], 60) #partA+B self.rem_p11 = Perceptron([100, 100], 100)
def __init__(self): self.only_p11=Perceptron(100,100) self.only_p12=Perceptron(-100,0) self.only_p13=Perceptron(-100,0) self.only_p14=Perceptron(-100,0) self.only_p21=Perceptron([20,20,20,20],80) self.only_p31=Perceptron([100,100,100,100],100)
def test(): # training data X = np.array([[1, 3, 3], [1, 4, 3], [1, 1, 1]]) Y = np.array([1, 1, -1]) learningRate = 0.1 learningTimes = 100 # training perceptron = Perceptron() perceptron.train(X, Y, learningTimes, learningRate) W = perceptron.getW() trainTimes = perceptron.getTrainTimes() print W print trainTimes # plot the training data X1 = [3, 4] Y1 = [3, 3] X2 = [1] Y2 = [1] k = -W[1] / W[2] d = -W[0] / W[2] x = np.linspace(0, 5) # generate arithmetic sequence # plot plt.figure() plt.plot(x, x*k+d, 'r') plt.plot(X1, Y1, 'bo') plt.plot(X2, Y2, 'yo') plt.show() # predict test = np.array([[1, 2, 3], [1, 6, 2], [1, 3, 3], [1, 7, 5], [1, 5, 7], [1, 9, 2]]) testResult = perceptron.predict(test) print testResult testX1 = [] testY1 = [] testX2 = [] testY2 = [] for i in range(len(testResult)): if testResult[i] >= 0: testX1.append(test[i][1]) testY1.append(test[i][2]) else: testX2.append(test[i][1]) testY2.append(test[i][2]) plt.figure() plt.plot(x, x*k+d, 'r') plt.plot(testX1, testY1, 'bo') plt.plot(testX2, testY2, 'yo') plt.show()
def main(): # kernels: 1: Linear, 2: Poly, 3: Gaussian # datasets_names = ["bank_marketing", "gender_voice", "mushroom"] # analyze_accuracies(datasets_names, test_on_train=False) dataset_name = "gender_voice" # splits and balances the dataset train_set, test_set = dt.load_dataset(dataset_name, 70, standardize=True) # print(dt.verify_linear_separability(train_set, test_set)) clf = Perceptron.Perceptron(dataset_name, train_set[:, :-1], train_set[:, -1], _kernel_=3, epochs=10, _sigma_=5, _dim_=5) clf.fit() predicted = clf.predict_set(test_set[:, :-1], test_set[:, -1]) accuracy = clf.accuracy(test_set[:, -1], predicted) print("Kernel ", clf.kernel, " accuracy: " + str(accuracy)) # print("Kernel ", clf.kernel, " Best Accuracy with 10 maximum epochs: ", best_epoch(clf, test_set, 10)) ''' # full_dataset is used to plot graphs full_dataset = np.vstack((train_set, test_set)) X = full_dataset[:, :-1] Y = full_dataset[:, -1] # plotter functions to visualize the dataset and the hyperplane Plotter.plot_3d(clf, X, Y) Plotter.plot_2d(clf, X, Y) ''' ''' # plots dataset with seaborn sns.set(style="ticks", color_codes=True) tmp_dataset = np.loadtxt("ml_datasets/mushroom_dataset.csv", delimiter=',', dtype=str) tmp_dataset = tmp_dataset[0, :] df = pd.DataFrame(data=full_dataset, columns=tmp_dataset) df.dropna(inplace=True) sns.pairplot(df, vars=["cap-shape", "cap-surface", "cap-color"], hue='mushroom', corner=True) # plt.savefig("Pictures/" + str(dataset_name) + " scatterplot.png") plt.show() ''' '''
def main(): global P make_points(500) # Make 100 points P = per.Perceptron(3, 0.0001) # Loop until we press quit while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # Draw to the canvas draw() # Keep the refresh rate to 60 clock.tick(5)
def display(): data = cPickle.load(gzip.open('mnist.pkl.gz')) steps = [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1] time = [5000 * i for i in range(1, 15)] # defaut = 63 000 res = [] for i in time: network = Perceptron(len(data[0][0][0]), 0.005) res.append( test(data[0][0], data[0][1], data[1][0], data[1][1], i, network)) plt.plot(time, res, '-o') plt.xscale("linear") plt.ylabel("Taux de reussite") plt.xlabel("Pas d'apprentissages") plt.show()
def test(trainingSet, testSet): perceptron = p.Perceptron(trainingSet) perceptron.train(100) # <--------------------------------------------------------- adjust max number of iterations votedPerceptron = vp.VotedPerceptron(trainingSet, 100) # <--------------------------------- adjust number of epochs votedPerceptron.train() perceptronErrors = 0 votedPerceptronErrors = 0 perceptronConfusionMatrix = np.zeros(shape=(2, 2)) votedPerceptronConfusionMatrix = np.zeros(shape=(2, 2)) for i in xrange(testSet.size): perceptronPrediction = perceptron.predict(testSet.x[i]) if perceptronPrediction != testSet.y[i]: perceptronErrors += 1 if perceptronPrediction == 1: perceptronConfusionMatrix[1][0] += 1 # False positive else: perceptronConfusionMatrix[0][1] += 1 # False negative else: if perceptronPrediction == 1: perceptronConfusionMatrix[0][0] += 1 # True positive else: perceptronConfusionMatrix[1][1] += 1 # True negative votedPerceptronPrediction = votedPerceptron.predict(testSet.x[i]) if votedPerceptronPrediction != testSet.y[i]: votedPerceptronErrors += 1 if votedPerceptronPrediction == 1: votedPerceptronConfusionMatrix[1][0] += 1 # False positive else: votedPerceptronConfusionMatrix[0][1] += 1 # False negative else: if votedPerceptronPrediction == 1: votedPerceptronConfusionMatrix[0][0] += 1 # True positive else: votedPerceptronConfusionMatrix[1][1] += 1 # True negative perceptronAccuracy = round((testSet.size - perceptronErrors) / float(testSet.size) * 100, 2) votedPerceptronAccuracy = round((testSet.size - votedPerceptronErrors) / float(testSet.size) * 100, 2) print("Test set size: {}".format(testSet.size)) print("Perceptron errors: {} | Accuracy: {}%.".format(perceptronErrors, perceptronAccuracy)) print("Perceptron confusion matrix:") print(perceptronConfusionMatrix) print("Voted perceptron errors: {} | Accuracy: {}%.".format(votedPerceptronErrors, votedPerceptronAccuracy)) print("Voted perceptron confusion matrix:") print(votedPerceptronConfusionMatrix) print("") return perceptronAccuracy, votedPerceptronAccuracy
def main_bkup(): iris = load_data() train_data = iris.data[:int(len(iris.data) * .8)] test_data = iris.data[int(len(iris.data) * .8):] train_label = iris.target[:int(len(iris.data) * .8)] test_label = iris.target[int(len(iris.data) * .8):] classifier = Perceptron(learning_rate=0.1) classifier.fit(train_data, train_label, 20) print("Computed weights are: ", classifier._w) for i in range(len(test_label)): print(classifier.predict(test_data[i]), test_label[i])
def test(trainingSet, testSet): perceptron=p.Perceptron(trainingSet); perceptron.train(50); # <---- Change the number to change the max iterations of the perceptron votedPerceptron=vp.VotedPerceptron(trainingSet,5); # <---- Change the number to change the number of epochs of the voted Perceptron votedPerceptron.train(); perceptronErrors = 0; votedPerceptronErrors = 0; pconfusionMatrix=np.zeros(shape=(2,2)); # (rows -> real) row 0 = +1 row 1 = -1 | (column -> predicted) column 0 = +1 column 1 = -1 vpconfusionMatrix = np.zeros(shape=(2, 2)); #real test where predict each value in the test and check if correct. Create also the confusions matrix and take note of the errors for i in range(0,testSet.getDimension()): #for the perceptron perceptronPrediction=perceptron.predict(testSet.getx()[i]); if perceptronPrediction!=testSet.gety()[i] : perceptronErrors+=1; if perceptronPrediction==1: pconfusionMatrix[0][1]+=1; else: pconfusionMatrix[1][0] += 1; else: if perceptronPrediction==1: pconfusionMatrix[0][0]+=1; else: pconfusionMatrix[1][1] += 1; #for the votedPerceptron votedPerceptronPrediction=votedPerceptron.predict(testSet.getx()[i]); if votedPerceptronPrediction!=testSet.gety()[i] : votedPerceptronErrors+=1; if votedPerceptronPrediction==1: vpconfusionMatrix[0][1]+=1; else: vpconfusionMatrix[1][0] += 1; else: if votedPerceptronPrediction==1: vpconfusionMatrix[0][0]+=1; else: vpconfusionMatrix[1][1] += 1; perceptronAccuracy = round(((testSet.getDimension()-perceptronErrors)/testSet.getDimension())*100,2); votedPerceptronAccuracy = round(((testSet.getDimension() - votedPerceptronErrors) / testSet.getDimension()) * 100,2); print("Test set dimensions: ",testSet.getDimension()); print("Voted perceptron errors: ",votedPerceptronErrors," | Accuracy: ",votedPerceptronAccuracy,"%"); print(vpconfusionMatrix); print("Perceptron errors: ",perceptronErrors," | Accuracy: ",perceptronAccuracy,"%"); print(pconfusionMatrix);
def calculatePoints(numberOfPoints, trainingSteps): myPerceptron = Perceptron(0.1) steps = [i for i in range(trainingSteps)] trainingEvolution = [] for i in range(trainingSteps): dotX = random.uniform(-5, 5) dotY = random.uniform(-5, 5) inputs = [dotX, dotY] myPerceptron.train(inputs, whereIs(dotX, dotY)) correct = [] realAns = [] answer = [] pointsX = [] pointsY = [] functionY = [] colors = [] for j in range(numberOfPoints): dotX = random.uniform(-5, 5) dotY = random.uniform(-5, 5) inputs = [dotX, dotY] pointsX.append(dotX) pointsY.append(dotY) answer.append(myPerceptron.feed(inputs)) functionY.append(3 * dotX + 2) realAns.append(whereIs(dotX, dotY)) if (answer[j] == 1): colors.append('b') else: colors.append('r') if (realAns[j] == answer): correct.append(True) else: correct.append(False) percentage = 0 for (ans,real) in zip(answer,realAns): if ans == real: percentage += 1 percentage /= numberOfPoints trainingEvolution.append(percentage) plt.plot(pointsX, functionY) plt.scatter(pointsX, pointsY, c=colors) plt.show() plt.plot(steps,trainingEvolution) plt.show()
def Main(): #Load Data Íris iris_df = pd.read_csv("Data/Iris_Data.csv") #Clear collumn 'species' iris_df['species'] = iris_df['species'].apply( lambda x: 1 if 'virginica' in x else 0 ) #Separate the class 'd' from the input vectors class_iris = iris_df.species iris_df.drop(['species'],axis = 1,inplace = True) #Artificial part_1 = np.random.randint(-10,10,size=20) * 0.01 part_2a = np.array([np.random.randint(-10,10,size=10) * 0.01]) part_2b = np.array([np.random.randint(90,99,size=10) * 0.01]) part_3Sa = np.array([np.random.randint(90,99,size=10) * 0.01]) part_3b = np.array([np.random.randint(-10,10,size=10) * 0.01]) part_4 = np.random.randint(90,99,size=20) * 0.01 part_1.shape = (10,2) part_2a.shape = (10,1) part_2b.shape = (10,1) part_3a.shape = (10,1) part_3b.shape = (10,1) part_4.shape = (10,2) class_0 = np.zeros(30) class_1 = np.ones(10) part_2 = np.concatenate((part_2a,part_2b),axis = 1) part_3 = np.concatenate((part_3a,part_3b),axis = 1) artificial = np.concatenate((part_1,part_2,part_3,part_4)) artificial = artificial.reshape(40,2) class_artificial = pd.Series(np.concatenate((class_0,class_1))) artificial_df = pd.DataFrame(artificial, columns = ['x1','x2']) #Split train from the test train_x,test_x, train_d,test_d = trainTest(iris_df,class_iris) #Create the Perceptron perceptron = Perceptron() perceptron.trainingModel(train_x,train_d)
def outputModels(day1, day2, day3, test): for i in range(0, 3): trainingUnit = Per.Perceptron('Linear', i) day = str(i + 1) for j in range(1, 4): ite = day + '.' + str(j) if j == 1: train = day1 elif j == 2: train = day2 else: train = day3 trainingModel = trainingUnit.train(train) createScatterGraph2(trainingModel, ite, 'Training') calculateError(trainingModel, test, ite) createLineGraph(trainingModel, i + 1) calculateError(trainingModel, test, 'Day ' + day) makeComparisonGraph(test, trainingModel, 'Architecture' + day)
def main(): """ mainfunction, creating perceptron, train, print result """ #create new perceptron with 3 inputs [x,y,1] ptron = pt.Perceptron(3, learning_constant) #generate trainigdata generateData() # print out initial weights print(ptron.getWeights()) #train with generated data for item in trainingdata: ptron.train(item['data'], item['output']) # print out endweights w = ptron.getWeights() print(w) # what is the seperating line print(30 * '=') print('y =', -w[0] / w[1], 'x+', -w[2] / w[1])
def TrainAndTestBinPerceptron(): if len(digits) > 2: print "digits should be 2 digits in the case of Binary Perceptron" print "Running the Binary Perceptron" print "the digits it shall descriminate between: " + str( digits[0]) + "," + str(digits[1]) print "the label 1 will be given to the digit " + str(digits[0]) img, lbl = rn.getData(digits, "training") binlbl = rn.BinaryLabels(lbl, digit) perc = pr.Perceptron(img, binlbl, int(alpha * len(binlbl))) print "Error on the Training data: " + str( pr.TestClassifier(img, binlbl, perc) * 100.0) + "%" print "(out of " + str(len(binlbl)) + " samples)" print "Extracting the Testing Data" img, lbl = rn.getData(digits, "testing") binlbl = rn.BinaryLabels(lbl, digit) print "Error on the Testing data: " + str( pr.TestClassifier(img, binlbl, perc) * 100.0) + "%" print "(out of " + str(len(binlbl)) + " samples)"
def main(): print('main()...........') print(' NumInputs = {}'.format(NumInputs)) print(' NumHiddenLayersNeurons = {}'.format( NumHiddenLayersNeurons)) print(' NumOuputs = {}'.format(NumOuputs)) print(' NumSamples = {}'.format(NumSamples)) print(' InputsMatrix = {}'.format(InputsMatrix)) print(' OutputsMatrix = {}'.format(OutputsMatrix)) # create perceptron perceptron = Perceptron.Perceptron(NumInputs, NumHiddenLayersNeurons, NumOuputs, NumSamples) print('PERCEPTRON') print(' hidden layer neurons') print(' perceptron.hiddenLayerWeightsMatrix = {}'.format( perceptron.hiddenLayerWeightsMatrix)) print(' perceptron.hiddenLayerBiasMatrix = {}'.format( perceptron.hiddenLayerBiasMatrix)) print(' outputs') print(' perceptron.outputLayerWeightsMatrix = {}'.format( perceptron.outputLayerWeightsMatrix)) print(' perceptron.outputLayerBiasMatrix = {}'.format( perceptron.outputLayerBiasMatrix)) print('LOOP TRAINING...................................') for i in range(NumTraining): # create forward propagate forwardPropagator = ForwardPropagator.ForwardPropagator(InputsMatrix) # do forward propagate forwardPropagator.propagate(perceptron) print('FORWARD PROPAGATE') print(' forwardPropagate.inputsMatrix = {}'.format( forwardPropagator.inputsMatrix)) print( ' forwardPropagate.outputsMatrixHiddenLayerNeurons = {}' .format(forwardPropagator.outputsMatrixHiddenLayerNeurons)) print(' forwardPropagate.outputsMatrixOutputsLayer = {}'. format(forwardPropagator.outputsMatrixOutputsLayer))