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
被假定为浮动列表的列表。它表示我所有训练示例的列表,每个示例都是一个浮点数列表(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. 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]