Question

Dear fellow coders and science guys :)

I am using python with numpy and matplotlib to simulate a perceptron, proud to say it works pretty well.

I used python even tough I've never seen it before, cause I heard matplotlib offered amazing graph visualisation capabilities.

Using functions below I get a 2d array that looks like this: [[aplha_1, 900], [alpha_2], 600, .., [alpha_99, 900]

So I get this 2D array and would love to write a function that would enable me to analyze the convergence.

I am looking for something that will easily and intuitively (don't have time to study a whole new library for 5 hours now) draw a function like this sketch:

enter image description here

def get_convergence_for_alpha(self, _alpha):
    epochs = []
    for i in range(0, 5):
        epochs.append(self.perceptron_algorithm())
        self.weights = self.generate_weights()

    avg = sum(epochs, 0) / len(epochs)
    res = [_alpha, avg]
    return res

And this is the whole generation function.

def alpha_convergence_function(self):
    res = []
    for i in range(1, 100):
        res.append(self.get_convergence_for_alpha(i / 100))

    return res

Is this easily doable?

Was it helpful?

Solution

You can convert your nested list to a 2d numpy array and then use slicing to get the alphas and epoch counts (just like in matlab).

import numpy as np
import matplotlib.pyplot as plt

# code to simulate the perceptron goes here...

res = your_object.alpha_convergence_function()
res = np.asarray(res)

print('array size:', res.shape)

plt.xkcd()   # so you get the sketchy look :)

# first column -> x-axis, second column -> y-axis
plt.plot(res[:,0], res[:,1])
plt.show()

Remove the plt.xkcd() line if you don't actually want the plot to look like a sketch...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top