繁体   English   中英

仅更新一个索引时,Python numpy zeros数组将为每个值分配1

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM