![](/img/trans.png)
[英]Two different numpy arrays are being assigned the same values when only one array is being referenced
[英]Python numpy zeros array being assigned 1 for every value when only one index is updated
以下是我的代码:
amount_features = X.shape[1]
best_features = np.zeros((amount_features,), dtype=int)
best_accuracy = 0
best_accuracy_index = 0
def find_best_features(best_features, best_accuracy):
for i in range(amount_features):
trial_features = best_features
trial_features[i] = 1
svc = SVC(C = 10, gamma = .1)
svc.fit(X_train[:,trial_features==1],y_train)
y_pred = svc.predict(X_test[:,trial_features==1])
accuracy = metrics.accuracy_score(y_test,y_pred)
if (accuracy > best_accuracy):
best_accuracy = accuracy
best_accuracy_index = i
print(best_accuracy_index)
best_features[best_accuracy_index] = 1
return best_features, best_accuracy
bf, ba = find_best_features(best_features, best_accuracy)
print(bf, ba)
这是我的输出:
25
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 0.865853658537
和我的预期输出:
25
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0] 0.865853658537
我正在尝试使用提供最高准确性的索引来更新zeros数组。 如您所见,它应该是索引25,然后我为数组分配25索引等于1。但是,当我打印该数组时,它显示每个索引都已更新为1。
不知道是什么不幸。 感谢您在地球上花费有限的时间来帮助我。
将trial_features = best_features
更改为trial_features = numpy.copy(best_features)
。 @Michael Butscher已经给出了更改背后的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.