2014-03-12 84 views
2

亲爱的同学编码器和科学家伙:)在绘制matplotlib二维函数

我使用Python和numpy的和matplotlib模拟感知,自豪地说,它工作得很好。

我使用python甚至很难我从来没有见过它,因为我听说matplotlib提供了惊人的图形可视化功能。

使用函数下面我得到的2D阵列看起来像这样: [[aplha_1,900],[alpha_2],600,...,[alpha_99,900]

所以我得到这个2D阵列和我想写一个函数,使我能够分析收敛。

我在寻找的东西,可以轻松,直观地(没有时间学习5小时一个全新的库现在)得出这样的函数素描:

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 

这是整个世代的功能。

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

    return res 

这很容易做到吗?

+1

可以简化和使用'NumPy'的矢量运算优化代码 –

回答

2

你可以将你的嵌套列表转换成一个2d numpy数组,然后使用切片来获取alpha和时代数(就像在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() 

取出plt.xkcd()线,如果你不真正想要的情节看起来像一个素描......

+0

像魔术一样工作,正是我所要求的。thx milion –