有一个优先级列表,如:
> df <- data.frame(id = c(1,2,3), Google = c(1,1,0), Yahoo = c(1,1,1), Microsoft = c(0,1,1))
> df
id Google Yahoo Microsoft
1 1 1 1 0
2 2 1 1 1
3 3 0 1 1
来自二进制数据框,如下所示:
> df <- data.frame(id = c(1,2,3), Google = c(1,1,0), Yahoo = c(0,0,1), Microsoft = c(0,0,0))
> df
id Google Yahoo Microsoft
1 1 1 0 0
2 2 1 0 0
3 3 0 1 0
如何生成一个新的数据帧,其中列是相同的,但根据优先级,只有优先级最高的列保留1,另一行在每行中取0?
预期结果的例子:
import tensorflow as tf
def load_graph(frozen_graph_filename):
# We load the protobuf file from the disk and parse it to retrieve the
# unserialized graph_def
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Then, we import the graph_def into a new Graph and returns it
with tf.Graph().as_default() as graph:
# The name var will prefix every op/nodes in your graph
# Since we load everything in a new graph, this is not needed
tf.import_graph_def(graph_def, name="prefix")
return graph
graph = load_graph('tensorflow_inception_graph.pb')
inception_graph = tf.get_default_graph()
x = graph.get_tensor_by_name('prefix/input:0')
y = graph.get_tensor_by_name('prefix/avgpool0/reshape:0')
output_feature = tf.stop_gradient(y) # It's an identity function
# Saving
sess = tf.Session()
builder = tf.saved_model.builder.SavedModelBuilder('./test5')
builder.add_meta_graph_and_variables(sess, ["tag"], signature_def_map= {
"model": tf.saved_model.signature_def_utils.predict_signature_def(
inputs= {"x": x},
outputs= {"finalnode": y})
})
builder.save()
答案 0 :(得分:2)
使用:
mc <- max.col(df[-1], ties.method = 'first')
df[-1] <- 0
df[cbind(1:nrow(df), mc + 1)] <- 1
给出:
> df id Google Yahoo Microsoft 1 1 1 0 0 2 2 1 0 0 3 3 0 1 0
如果公司列不在优先级顺序中,您可以使用以下命令进行更改:
priority <- c('Google',"Yahoo",'Microsoft')
df <- df[, c(1, match(priority, names(df)))]
答案 1 :(得分:1)
我们也可以使用apply
函数:
df[-1]= t(apply(df[-1], 1, function(x)`[<-`(x,-which.max(x),0)))
df
id Google Yahoo Microsoft
1 1 1 0 0
2 2 1 0 0
3 3 0 1 0