我正在尝试从使用Python的书中学习apriori机器学习算法,作为该学习的一部分,我目前仍然遇到以下问题:
以下代码构造似乎工作正常:
Ck = [[1], [2], [3], [4], [5]]
for tranid in range(10):
for candidate in Ck:
print("Printing candidate value: ", candidate)
Run Code Online (Sandbox Code Playgroud)
但是,以下不起作用:
Ck = [[1], [2], [3], [4], [5]]
Ck2 = map(frozenset, Ck)
for tranid in range(10):
for candidate in Ck2:
print("Printing candidate value: ", candidate)
Run Code Online (Sandbox Code Playgroud)
当我将原始迭代的每个元素映射到冻结集时,我注意到内部循环("对于Ck2中的候选者")仅执行一次.之后它永远不会执行.上面没有冻结集的代码正确地循环内循环10次.但是,使用frozenset映射,我可以让内循环只执行一次.
请帮我解决这个问题.本书已将可迭代值映射到frozenset,因为它们不希望它对于算法的目的是可变的.我只是想按原样跟随它.
我在Anaconda(Spyder)上使用Python 3.5.1.
请帮助,因为我不熟悉Python和机器学习.
谢谢和问候,Mahesh.
为什么 TypeScript 不对对象的成员应用类型缩小到对象类型本身,以便它可以传递给另一个需要缩小类型的函数?如何在不失去类型安全性的情况下修复/规避这种情况?
最小的例子:
type A = { key: string | null};
type B = {key: string};
function func(a: B) {};
const a: A = {key:'abcd'};
if(typeof(a.key)==='string') {
a.key // has (narrowed) type 'string'
func(a); // still does not work
}
Run Code Online (Sandbox Code Playgroud)
错误信息是: Types of property 'key' are incompatible. Type 'string | null' is not assignable to type 'string'.