<jupyter_start><jupyter_text>Training a perceptron neural net from scratch using the sepal length & width, petal length & width as data inputs.<jupyter_code>from numpy import exp, array, random, dot, genfromtxt
import numpy as np
class NeuronLayer():
    def __init__(self, number_of_neurons, number_of_inputs_per_neuron):
        self.synaptic_weights = 2 * random.random((number_of_inputs_per_neuron, number_of_neurons)) - 1
class NeuralNetwork():
    def __init__(self, layer1):
        # Seed the random number generator, so it generates the same numbers
        # every time the program runs.
        self.layer1 = layer1
        
    # The Sigmoid function, which describes an S shaped curve.
    # We pass the weighted sum of the inputs through this function to
    # normalise them between 0 and 1.
    def __sigmoid(self, x):
        return 1 / (1 + exp(-x))

    # The derivative of the Sigmoid function.
    # This is the gradient of the Sigmoid curve.
    # It indicates how confident we are about the existing weight.
    def __sigmoid_derivative(self, x):
        return x * (1 - x)

    # We train the neural network through a process of trial and error.
    # Adjusting the synaptic weights each time.
    def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
        for iteration in xrange(number_of_training_iterations):
            # Pass the training set through our neural network (a single neuron).
            output = self.think(training_set_inputs)

            # Calculate the error (The difference between the desired output
            # and the predicted output).
            error = training_set_outputs - output

            # Multiply the error by the input and again by the gradient of the Sigmoid curve.
            # This means less confident weights are adjusted more.
            # This means inputs, which are zero, do not cause changes to the weights.
            adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))

            # Adjust the weights.
            self.layer1.synaptic_weights += adjustment*0.01

    # The neural network thinks.
    def think(self, inputs):
        # Pass inputs through our neural network (our single neuron).
        return self.__sigmoid(dot(inputs, self.layer1.synaptic_weights))

if __name__ == "__main__":

    random.seed(1)

    # 1 neurons, 4 inputs    
    layer1 = NeuronLayer(1, 4)

    neural_network = NeuralNetwork(layer1)

    dataset = genfromtxt('IRIS.csv', delimiter=',')
    
    training_set_inputs = array(dataset[1:, :-1])
    training_set_outputs = np.reshape(array(dataset[1:, -1]), (-1, 1))
    
    neural_network.train(training_set_inputs, training_set_outputs, 100000)


    for i in range(51):
        output4 = neural_network.think(array(dataset[i,:-1]))    
        print(output4)
        
    print("Other")
    for i in range(50):
        output4 = neural_network.think(array(dataset[i+51,:-1]))    
        print(output4)<jupyter_output>[ nan]
[ 0.00038267]
[ 0.00185387]
[ 0.00082315]
[ 0.00292791]
[ 0.00031901]
[ 0.00056492]
[ 0.00101472]
[ 0.00087028]
[ 0.00393867]
[ 0.00176556]
[ 0.0002583]
[ 0.00164935]
[ 0.00163712]
[ 0.00068746]
[  1.99333562e-05]
[  4.31415223e-05]
[  9.38397958e-05]
[ 0.00047687]
[ 0.00044919]
[ 0.00032487]
[ 0.00145619]
[ 0.00053432]
[  7.76590504e-05]
[ 0.00493949]
[ 0.00631148]
[ 0.00412545]
[ 0.00211494]
[ 0.00054473]
[ 0.00045903]
[ 0.00315731]
[ 0.00378523]
[ 0.0009221]
[  8.26701347e-05]
[  3.74023308e-05]
[ 0.00176556]
[ 0.00039459]
[ 0.00016668]
[ 0.00176556]
[ 0.00190859]
[ 0.00079099]
[ 0.00033499]
[ 0.01489737]
[ 0.00109629]
[ 0.0024877]
[ 0.00243338]
[ 0.0025405]
[ 0.00040833]
[ 0.00141806]
[ 0.00028421]
[ 0.00073339]
Other
[ 0.99981662]
[ 0.99979653]
[ 0.99995871]
[ 0.99989632]
[ 0.99995292]
[ 0.99994668]
[ 0.99992018]
[ 0.99655491]
[ 0.99989379]
[ 0.99970305]
[ 0.99948971]
[ 0.99972166]
[ 0.99975479]
[ 0.99996627]
[ 0.9963782]
[ 0.99959915]
[ 0.99994564]
[ 0.99948207]
[ 0.999989[...]
{'fraction_non_alphanumeric': 0.11523145843703335, 'fraction_numerical': 0.1672473867595819, 'mean_word_length': 3.035140562248996, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '543297', 'license': 'MIT', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1429, 'n_words': 361, 'path': 'Lab1/IRIS Perceptron.ipynb', 'repo_name': 'IT-Department-Labs/Soft-Computing-Lab', 'size': '4018'}