Python 对于大小为 1 的轴 0,索引 1 超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31574859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
index 1 is out of bounds for axis 0 with size 1
提问by Edoardo
N=100
numbers_training_pattern=10
for pattern in range(number_training_pattern):
rate=np.zeros((1,N))
for epoch in range(1,nepochs+1):
if epoch<=1:
clamp=1
else:
clamp=0
activ=np.zeros((1,N))
for neuron in range(N):
PreSynInput = rate.T + (Testing_pattern[neuron, pattern] * clamp)
activ[neuron] = np.dot(PreSynInput.T,autocorrelation_matrix[:,neuron])
error IndexError Traceback (most recent call last) in () 17 18 ---> 19 activ[neuron] = np.dot(PreSynInput.T,autocorrelation_matrix[:,neuron]) 20 21
() 17 18 ---> 19 activ[neuron] = np.dot(PreSynInput.T,autocorrelation_matrix[:,neuron]) 20 21
IndexError: index 1 is out of bounds for axis 0 with size 1
索引错误:索引 1 超出轴 0 的范围,大小为 1
采纳答案by unutbu
activ=np.zeros((1,N))
means that activ
has 1 row and N
columns. activ[0]
refers to the first row. activ[1]
would raise an IndexError because there is no second row.
意味着activ
有 1 行和N
列。activ[0]
指第一行。activ[1]
会引发 IndexError 因为没有第二行。
for i in range(N)
makes i
range from 0 to N-1
. Hence, an error occurs if N
is greater than 1.
for i in range(N)
使i
范围从 0 到N-1
. 因此,如果N
大于 1 ,则会发生错误。
One way to fix the error while changing the least amount of your current code would be to use
在更改最少当前代码的同时修复错误的一种方法是使用
activ[0, neuron] = np.dot(PreSynInput.T,autocorrelation_matrix[:,neuron])
However, assigning values to a NumPy array element-by-element is usually not the ideal way to take advantage of NumPy. You'll get much better performance if you can express the computation as one done on larger arrays and without the Python for-loops.
但是,逐个元素地为 NumPy 数组赋值通常不是利用 NumPy 的理想方式。如果您可以将计算表示为在更大的数组上完成并且没有 Python for 循环,您将获得更好的性能。
For example, If I understand the shapes of the undefined arrays correctly, you could replace
例如,如果我正确理解未定义数组的形状,您可以替换
activ=np.zeros((1,N))
for neuron in range(N):
PreSynInput = rate.T + (Testing_pattern[neuron, pattern] * clamp)
activ[neuron] = np.dot(PreSynInput.T,autocorrelation_matrix[:,neuron])
with
和
PreSynInput = (rate.T + (Testing_pattern[:, pattern] * clamp))
activ = np.einsum('ij,ij->j', PreSynInput, autocorrelation_matrix)
For example,
例如,
import numpy as np
np.random.seed(2015)
N, M, pattern = 10, 5, 0
clamp = 1
autocorrelation_matrix = np.random.randint(10, size=(N, N))
Testing_pattern = np.random.randint(10, size=(N, M))
rate = np.random.randint(10, size=(1,N))
activ=np.zeros((1,N))
for neuron in range(N):
PreSynInput = rate.T + (Testing_pattern[neuron, pattern] * clamp)
activ[:, neuron] = np.dot(PreSynInput.T, autocorrelation_matrix[:,neuron])
PreSynInput = (rate.T + (Testing_pattern[:, pattern] * clamp))
activ2 = np.einsum('ij,ij->j', PreSynInput, autocorrelation_matrix)
print(activ)
# [[ 405. 421. 272. 475. 227. 424. 644. 212. 325. 502.]]
print(activ2)
# [405 421 272 475 227 424 644 212 325 502]
You would get even better performance if you can find a way to eliminate the
for pattern
-loop and for epoch
-loop.
如果您能找到消除for pattern
-loop 和for epoch
-loop的方法,您将获得更好的性能
。
How to eliminate those loops is a problem sufficiently difficult and interesting to justify a separate question. If you do post a new question about it, please include a minimal example with runnable codeso it is absolutely clear what the desired output is for the given input.
如何消除这些循环是一个足够困难和有趣的问题,足以证明一个单独的问题是合理的。如果您确实发布了一个关于它的新问题,请包含一个带有可运行代码的最小示例,以便绝对清楚给定输入所需的输出是什么。