在下面的代碼=,我試圖計算的頻率和一組向量總和(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) –