Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与for循环的Python列表交互问题_Python_List_Append - Fatal编程技术网

与for循环的Python列表交互问题

与for循环的Python列表交互问题,python,list,append,Python,List,Append,我尝试构建的遗传特征优化算法有问题。其想法是,将测试特定的特征组合,如果使用这些特征的模型精度高于先前的最大值,则特征组合将替换先前的最大组合。通过以这种方式运行剩余的潜在特征,最终的组合应该是给定特征向量大小的最佳特征组合。目前,实现这一目标的代码如下所示: def mutate_features(features, feature): new_features = features index = random.randint(0,len(features)-1) n

我尝试构建的遗传特征优化算法有问题。其想法是,将测试特定的特征组合,如果使用这些特征的模型精度高于先前的最大值,则特征组合将替换先前的最大组合。通过以这种方式运行剩余的潜在特征,最终的组合应该是给定特征向量大小的最佳特征组合。目前,实现这一目标的代码如下所示:

def mutate_features(features, feature):
    new_features = features
    index = random.randint(0,len(features)-1)
    new_features[index] = feature
    return new_features

def run_series(n, f_list, df):
    features_list = []
    results_list = []
    max_results_list = [[0,0,0,0,0]]
    max_feature_list = []
    features = [0,0,0,0,1]
    for i in range(0,5):  # 5 has just been chosen as the range for testing purposes
        results = run_algorithm(df, f_list, features)
        features_list.append(features)
        results_list.append(results)
        if (check_result_vector(max_results_list, results)):
            max_results_list.append(results)
            max_feature_list.append(features)
        else:
            print("Revert to previous :" +str(max_feature_list[-1]))
            features = max_feature_list[-1]
        features = mutate_features(features, f_list[i])
        print("Feature List = " +str(features_list))
        print("Results List = " +str(results_list))
        print("Max Results List = " +str(max_results_list))
        print("Max Feature List = "+str(max_feature_list))
该代码的输出已包含在下面

单击可缩放或放大照片

我不理解的部分是
max\u feature\u list
feature\u list
的输出


如果通过使用for循环中的
.append()
将任何内容添加到
max\u feature\u list
feature\u list
中,则似乎会将已经是列表成员的所有项目更改为与列表中最新添加的项目相同。我可能不完全理解这一点的语法/逻辑,并且非常希望得到有关程序为什么这样做的任何反馈。

发生这种情况是因为您在
mutate\u features
函数中更改了
features
的值,然后,由于append to
max\u feature\u列表是通过引用的,
max_feature_list
中填充的值也在更改,因为它们的基础值已更改

防止此类行为的一种方法是在
中对
功能进行深入复制,然后根据需要对复制的功能进行变异,然后返回

例如:

from copy import deepcopy

def mutate_features(features, feature):
    new_features = deepcopy(features)
    index = random.randint(0,len(features)-1)
    new_features[index] = feature
    return new_features


features = [1, 2, 3]
res = []
res.append(features)
features = mutate_features(features, feature)
res.append(features)
print(res)

之所以会发生这种情况,是因为您在
mutate_features
函数中更改了
features
的值,然后,由于append to
max_feature_list
是通过引用的,因此
max_feature_list
中填充的值也在更改,因为它们的基本值已更改

防止此类行为的一种方法是在
中对
功能进行深入复制,然后根据需要对复制的功能进行变异,然后返回

例如:

from copy import deepcopy

def mutate_features(features, feature):
    new_features = deepcopy(features)
    index = random.randint(0,len(features)-1)
    new_features[index] = feature
    return new_features


features = [1, 2, 3]
res = []
res.append(features)
features = mutate_features(features, feature)
res.append(features)
print(res)

你确定没有在你(未显示)的
mutate\u features\u函数中修改
max\u features\u列表
吗?我不相信它被修改了,我将编辑原始帖子以显示mutate features函数。最简单的调试方法是包含一个临时
打印(max\u features\u列表)
紧跟在
max\u features\u list之后的语句。追加(features)
。您确定没有在(未显示的)
mutate\u features
函数中修改
max\u features\u list
吗?我不相信它被修改了,我将编辑原始帖子以显示mutate features函数。最简单的调试方法是在
max\u features\u列表之后添加一个临时
print(max\u features\u列表)
语句。append(features)
features
不会在
mutate\u features
中更改;这也是我的第一个想法,但在OP发布代码后,情况显然不是这样。
new_features
features
是对相同值的两个引用(将
features
分配给
new_features
是通过ref而不是通过值),因此在这种情况下,更改
新功能
与更改
功能
是一样的。这个简单的更改解决了这个问题!我不知道将append()添加到max_features_列表是通过引用实现的,谢谢您的帮助。@AdamWhitrow很乐意提供帮助:)
功能在
mutate_功能中未更改;这也是我的第一个想法,但在OP发布代码后,情况显然不是这样。
new_features
features
是对相同值的两个引用(将
features
分配给
new_features
是通过ref而不是通过值),因此在这种情况下,更改
新功能
与更改
功能
是一样的。这个简单的更改解决了这个问题!我不知道max_feature_列表的append()是通过引用的,谢谢您的帮助。@AdamWhitrow很乐意帮助:)