我有一个程序输出这样的条件(这是实际输出,它是伪代码):
if ( first occurance of 'AB' <= -0.5 ) {
return [ 0.]
} else {
if ( number of products viewed <= 1.5 ) {
if ( similarity to 'AB' <= 0.899999976158 ) {
return [ 1.]
} else {
return [ 0.]
}
} else {
if ( average time between actions <= 57.2111129761 ) {
return [ 0.39145907]
} else {
return [ 0.10410805]
}
}
}
如何获得更具人性化(?)/解构的解决方案?即:
( first occurance of 'AB' > -0.5 ) * (( number of products viewed <= 1.5 ) * ( similarity to 'AB' <= 0.899999976158 ))+((( number of products viewed > 1.5 ) * ( average time between actions <= 57.2111129761 ))
(我已经有了可以更改的代码&#34;&lt; =&#34;到&#34;&gt;&#34;但除此之外我似乎无法隔离每个条件按照正确的顺序。
编辑:用于获取当前输出的代码(不是最小的例子,我害怕)
def get_code(tree, feature_names, tabdepth=0):
left = tree.tree_.children_left
right = tree.tree_.children_right
threshold = tree.tree_.threshold
features = [feature_names[i] for i in tree.tree_.feature]
value = tree.tree_.value
f=[]
def recurse(left, right, threshold, features, node, f, tabdepth=0):
if (threshold[node] != -2):
print('\t' * tabdepth + 'if ( ' + features[node] + ' <= ' + str(threshold[node]) + ' ) {')
f.append('%' * tabdepth+'if ( ' + features[node] + ' <= ' + str(threshold[node]) + ' ) {')
if left[node] != -1:
recurse (left, right, threshold, features,left[node], f, tabdepth+1)
print('\t' * tabdepth+'} else {')
f.append('%' * tabdepth+'} else {')
if right[node] != -1:
recurse (left, right, threshold, features,right[node], f, tabdepth+1)
print('\t' * tabdepth + '}')
else:
print('\t' * tabdepth + 'return ' + str(value[node][0]))
f.append('%' * tabdepth + 'return ' + str(value[node][0]))
recurse(left, right, threshold, features, 0, f)
答案 0 :(得分:1)
听起来你需要编写代码:
对于转换,主要想法是:
convert( if C then T else E )
==>
C * convert(T) + !C * convert(E)
如果您需要更具体的帮助,您应该提出更具体的问题。