将numpy数组列提取到字典中

时间:2018-10-24 03:47:12

标签: python pandas numpy

我正在手动创建功能和numpy数组的字典。

import tensorflow as tf

def suppress_range(x, a, b):
    """Sets all the elements in the range [a, b) to 0"""
    assert (a < b), "a must be less than b"
    significant = tf.logical_or(x <= a, x > b)
    zero = tf.zeros_like(x)
    return tf.where(significant, x, zero)

def main():
    logits = tf.placeholder(tf.float32)
    output = tf.sign(suppress_range(logits, -0.5, 0.5))

    with tf.Session() as sess:
        x = [[0.6, 0.4, -0.6, -0.4], [0.5, 0.1, -0.7, -0.2]]
        print(sess.run(output, feed_dict={logits: x}))

if __name__ == '__main__':
    main()

columns = ['a', 'b', 'c', 'd'] features = {'a': df_train.values[:, 0], 'b': df_train.values[:, 1], 'c': df_train.values[:, 2], 'd': df_train.values[:, 3]} 是源自df_train

的Pandas数据框

我正在使用以下代码对其进行简化:

pandas.read_csv()

还有更多Python方式吗? (例如,带zip的字典?)

1 个答案:

答案 0 :(得分:2)

您可以使用移调:

dict(zip(columns, df_train.values.T))

{k: v for k, v in zip(columns, df_train.values.T)}