Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Python嵌套for循环-内部循环迭代器类型更改_Python_Loops_Types_Nested_Iterator - Fatal编程技术网

Python嵌套for循环-内部循环迭代器类型更改

Python嵌套for循环-内部循环迭代器类型更改,python,loops,types,nested,iterator,Python,Loops,Types,Nested,Iterator,在此函数中,exs被假定为浮动列表的列表。它表示我所有训练示例的列表,每个示例都是一个浮点数列表(lengthnum_vars),表示感知器输入target被假定为浮点数列表(lengthnum_vars),表示目标函数的系数 def gradDesc(exs, target, num_vars, n=0.5, its=256): import random weights = [] # Create and initialize delWeights to 0. Mak

在此函数中,
exs
被假定为浮动列表的列表。它表示我所有训练示例的列表,每个示例都是一个浮点数列表(length
num_vars
),表示感知器输入
target
被假定为浮点数列表(length
num_vars
),表示目标函数的系数

def gradDesc(exs, target, num_vars, n=0.5, its=256):
    import random
    weights = []
    # Create and initialize delWeights to 0. Make its size num_vars.
    delWeights = [0.0]*num_vars


    # Initializes the weights to a real number in [-1,1]. Also makes weights
    # contain num_vars entries.
    for i in range(num_vars):
        weights.append(random.uniform(-1,1))

    # To make the printouts look nicer
    print("Iteration\tError")
    print("---------\t-----")

    for i in range(its):

        # Reset delWeights to 0
        for j in range(num_vars):
            delWeights[j] = 0

        for e in exs:

            # Plug e into the current hypothesis and get the output.
            output = test_hypo(weights, e, num_vars)

            print("delWeights: ", delWeights)
            for dw in delWeights:
                print("type(dw): ", type(dw))
                delWeights[dw] = delWeights[dw] + n*(test_hypo(target, e, num_vars) - output)*e[dw]

        for w in weights:
            weights[w] = weights[w] + delWeights[dw]

        # Print out the error every tenth iteration
        if i % 10 == 0:
            print(i + "\t" + train_err(exs, target, weights, num_vars))

    # Print out the final hypothesis
    print(i + "\t" + train_err(exs, target, weights, num_vars))

    return weights
问题是,在给定(有限)测试输入的情况下,当我尝试运行此程序时

trainers = 
[[1, 2.7902232015508766, -4.624194135789617], 
[1, -7.964359679418456, 2.1940274082288624], 
[1, 8.445941538761794, -8.86567924774781], 
... other sub-lists following this same format ...]

我得到了这个奇怪的输出:

gradDesc(trainers, target, num_vars)
Iteration       Error
---------       -----
delWeights:  [0, 0, 0]
type(dw):  <class 'int'>
type(dw):  <class 'int'>
type(dw):  <class 'int'>
delWeights:  [0.0, 0, 0]
type(dw):  <class 'float'>
Traceback (most recent call last):

  File "<ipython-input-19-97298b385113>", line 1, in <module>
    gradDesc(trainers, target, num_vars)

  File "C:/Users/Me/.spyder-py3/Machine Learning/gradDesc.py", line 107, in gradDesc
    delWeights[dw] = delWeights[dw] + n*(test_hypo(target, e, num_vars) - output)*e[dw]

TypeError: list indices must be integers or slices, not float
gradDesc(训练师、目标、数值变量)
迭代误差
---------       -----
增量权重:[0,0,0]
类型(dw):
类型(dw):
类型(dw):
增量权重:[0.0,0,0]
类型(dw):
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
gradDesc(训练师、目标、数值变量)
文件“C:/Users/Me/.spyder-py3/Machine Learning/gradDesc.py”,第107行,gradDesc中
delWeights[dw]=delWeights[dw]+n*(测试hypo(目标,e,数值)-输出)*e[dw]
TypeError:列表索引必须是整数或片,而不是浮点

所以我的问题是:
dw
的类型为什么在第二次迭代中通过exs中e的
循环从int变为float?

你的意思是使用
范围内i(len(delWeights))的
而不是delWeights中dw的
循环通过
delWeight
的索引吗

for dw in delWeights
循环遍历
delWeights
中的所有值,因此循环的第一次迭代可能会为
delWeights[dw]+n*(test_hypo(target,e,num_vars)-output)*e[dw
中的一个
delWeights
索引分配一个浮点值

delWeights[dw] = delWeights[dw] + n*(test_hypo(target, e, num_vars) - output)*e[dw]
delWeights[dw]
设置为浮点,因为
e[dw]
是浮点。因此,下次在delWeights:
循环中对dw执行
时,
dw
是一个浮点

使用
delWeights
的元素作为索引是没有意义的。如果要在列表上迭代并获取索引,则应使用
enumerate()


为什么在delweights:
循环中的dw的
之外使用
dw
变量?为什么首先将
delweights
初始化为
0.0
的列表,然后在
for i in range(its):
循环中用
0
替换它?为什么不
delWeights=[0]*num_vars
delWeights[dw] = delWeights[dw] + n*(test_hypo(target, e, num_vars) - output)*e[dw]
for i, dw in enumerate(delWeights):
    delWeights[i] = dw + n*(test_hypo(target, e, num_vars) - output)*e[i]