小编use*_*669的帖子

Pandas - 使用每行元素的点积创建新的 DataFrame 列

我正在尝试采用现有的 DataFrame 并附加一个新列。

假设我有这个 DataFrame(只是一些随机数):

    a           b           c            d          e
0   2.847674    0.890958    -1.785646   -0.648289   1.178657
1   -0.865278   0.696976    1.522485    -0.248514   1.004034
2   -2.229555   -0.037372   -1.380972   -0.880361   -0.532428
3   -0.057895   -2.193053   -0.691445   -0.588935   -0.883624
Run Code Online (Sandbox Code Playgroud)

我想创建一个新列“f”,将每一行乘以“成本”向量,例如 [1,0,0,0,0]。因此,对于第 0 行,f 列中的输出应为 2.847674。

这是我目前使用的功能:

def addEstimate (df, costs): 
   row_iterator = df.iterrows()

   for i, row in row_iterator:
      df.ix[i, 'f'] = np.dot(costs, df.ix[i])
Run Code Online (Sandbox Code Playgroud)

我正在用一个 15 个元素的向量来做这个,超过约 20k 行,我发现这超级慢(半小时)。我怀疑使用iterrowsix效率低下,但我不确定如何纠正这个问题。

有没有一种方法可以一次将它应用于整个 DataFrame,而不是遍历行?或者您有其他建议可以加快速度吗?

python vectorization dataframe dot-product pandas

2
推荐指数
1
解决办法
3306
查看次数

tf.train.Features TypeError:不允许位置参数

我可能在这里做了一些愚蠢的事情,但我不确定为什么我会收到这个错误。

此代码有效:

example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      'image/fn_0': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[0]))),
      'image/encoded_0': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[0])),
      'image/fn_1': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[1]))),
      'image/encoded_1': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[1])),
      'image/fn_2': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[2]))),
      'image/encoded_2': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[2]))}))
return example
Run Code Online (Sandbox Code Playgroud)

但是这段代码不起作用(在帖子标题中抛出 TypeError ):

feature_dict={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      }

  for idx, image in sorted(ex_image_buffers.iteritems()):
    img_key = 'image/encoded_' + str(idx)
    fn_key = 'image/fn_' + str(idx)
    feature_dict[img_key] = _bytes_feature(tf.compat.as_bytes(image))
    feature_dict[fn_key] = _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[idx])))

  example = tf.train.Example(features=tf.train.Features(feature_dict))
  return example
Run Code Online (Sandbox Code Playgroud)

ex_image_buffers 是一个列表。 …

python tensorflow

1
推荐指数
1
解决办法
5446
查看次数