首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当只更新一个索引时,为每个值分配一个Python numpy零数组。

当只更新一个索引时,为每个值分配一个Python numpy零数组。
EN

Stack Overflow用户
提问于 2017-10-29 22:36:36
回答 1查看 77关注 0票数 0

以下是我的代码:

代码语言:javascript
复制
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)

这是我的输出:

代码语言:javascript
复制
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

我的预期产出:

代码语言:javascript
复制
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

我试图用提供最高精度的索引更新零数组。正如您所看到的,它应该是索引25,然后为我的数组指定25索引等于1。但是,当我打印数组时,它显示每个索引都被更新为1。

不知道是怎么回事。感谢你在地球上有限的时间来帮助我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-29 22:46:30

trial_features = best_features更改为trial_features = numpy.copy(best_features)。“迈克尔·布奇”已经给出了改变背后的理由。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47005969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档