嵌套for循环vs numpy

2024-06-06 10:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个嵌套循环中嵌套了一个嵌套循环,但这真的很慢。有没有办法做这种事?在

这类似于用于视觉识别的cs231n神经网络。在

N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D)) # data matrix (each row = single example)
y = np.zeros(N*K, dtype='uint8') # class labels
for j in xrange(K):
  ix = range(N*j,N*(j+1))
  r = np.linspace(0.0,1,N) # radius
  t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta
  X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
  y[ix] = 

Tags: ofnumbernpzeros神经网络视觉pointsclass