在下面的代码=,我试图计算的频率和一组向量总和(numpy的载体)+与numpy.array对象修改原始对象
def calculate_means_on(the_labels, the_data):
freq = dict();
sums = dict();
means = dict();
total = 0;
for index, a_label in enumerate(the_labels):
this_data = the_data[index];
if a_label not in freq:
freq[a_label] = 1;
sums[a_label] = this_data;
else:
freq[a_label] += 1;
sums[a_label] += this_data;
假设the_data
(一个numpy的'矩阵')最初是:
[[ 1. 2. 4.]
[ 1. 2. 4.]
[ 2. 1. 1.]
[ 2. 1. 1.]
[ 1. 1. 1.]]
运行上述代码之后,the_data
变为:
[[ 3. 6. 12.]
[ 1. 2. 4.]
[ 7. 4. 4.]
[ 2. 1. 1.]
[ 1. 1. 1.]]
这是为什么?我已经推断它到行sums[a_label] += this_data;
,因为当我将其更改为sums[a_label] = sums[a_label] + this_data;
它表现如预期;即,the_data
未被修改。
看到[这里](http://stackoverflow.com/questions/12905338/python-difference-between-x-x1-and-x-1) –