【问题标题】:Write multidimensional numpy array to csv将多维numpy数组写入csv
【发布时间】:2015-08-07 19:39:30
【问题描述】:

我有一个包含函数值的多维 numpy 数组,我想将它写入一个长 csv。我怎样才能干净地做到这一点?我找不到 numpy 函数,但也许我在谷歌上搜索了错误的术语。一个例子:

#!/usr/bin/python

import csv
import numpy as np

x = np.array([1, 2, 3, 4])
y = np.array([50, 51])
z = np.array([99, 100, 101])
f = np.arange(24).reshape((4, 2, 3))  # Contains f(x, y, z)
assert f.shape == (x.size, y.size, z.size)

## I'd like to create a csv file whose columns are x, y, z, f
## How can I do that?

## np.savetxt("test.csv", a, delimiter=",")
## TypeError: float argument required, not numpy.ndarray

## Works, but does numpy already have a function that does this?
with open("test.csv", "wb") as csvfile:
    writer = csv.writer(csvfile, delimiter=",", quotechar="'", quoting=csv.QUOTE_MINIMAL)
    writer.writerow(["x", "y", "z", "f"])
    for x_index in range(x.size):
        for y_index in range(y.size):
            for z_index in range(z.size):
                writer.writerow([x[x_index], y[y_index], z[z_index],
                                 f[x_index, y_index, z_index]])

我有三个向量 x、y、z 和一个 X×Y×Z 数组,其中包含函数值 f(x,y,z)。换言之,f[i, j, k] 包含对应于 x[i], y[j] 和 z[k] 的函数值 f。有没有更简洁的方法来编写包含 x、y、z、f 列的长 csv?

这是头部 test.csv:

x,y,z,f
1,50,99,0
1,50,100,1
1,50,101,2
1,51,99,3
1,51,100,4
1,51,101,5
2,50,99,6
2,50,100,7
2,50,101,8

编辑:这似乎也有效:

x_y_z = np.array([x for x in itertools.product(x, y, z)])
assert x_y_z.shape[0] == f.size
output_array = np.hstack((x_y_z, f.flatten().reshape((f.size, 1)))
np.savetxt("test2.csv", output_array, comments="", delimiter=",", fmt="%i",
           header="x,y,z,f")

我是在重新发明轮子吗?

【问题讨论】:

  • 使用np.savetxt
  • @EdChum 看这个例子——我不认为 np.savetxt 做我想要的。我应该传递什么参数给它?
  • 将所有数组连接在一起并使用savetxt一次性写出来会更容易,我会亲自这样做
  • @EdChum 看到编辑,这是你的想法吗?
  • 我认为这可能是最好的方法

标签: python csv numpy


【解决方案1】:

事实上,是的,它比它应该的要复杂一些。

给定 3 个列表 x、y 和 z

import numpy as np

x = [1,2,3]
y = [4,5]
z = [6,7,8]

您需要修改此列表以获得所有可能的组合,请以这种方式使用numpy.repeat

new_x = np.array(x).repeat(len(y)*len(z))
print new_x

>> [1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3]

new_y = np.array([y]).repeat(len(z),axis=0).repeat(len(x),axis=1)

print new_y

>> [4 4 4 5 5 5 4 4 4 5 5 5 4 4 4 5 5 5]

new_z = np.array([z]).repeat(len(x)*len(y),axis=0)
print new_z

>> [6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8]

# reshape y and z just like new_x

new_y = new_y.reshape(new_x.shape)
new_z = new_z.reshape(new_x.shape)

只需连接它们!

# suppose that your vector f
f = np.array(range(len(x)*len(y)*len(z)))

matrix = np.array([new_x,new_y,new_z,f]).T
# or matrix = np.concatenate((np.concatenate((new_x,new_y),axis=1),np.concatenate((new_z,f),axis=1)),axis=1).T
print matrix

>>
[[ 1  4  6  0]
 [ 1  4  7  1]
 [ 1  4  8  2]
 [ 1  5  6  3]
 [ 1  5  7  4]
 [ 1  5  8  5]
 [ 2  4  6  6]
 [ 2  4  7  7]
 [ 2  4  8  8]
 [ 2  5  6  9]
 [ 2  5  7 10]
 [ 2  5  8 11]
 [ 3  4  6 12]
 [ 3  4  7 13]
 [ 3  4  8 14]
 [ 3  5  6 15]
 [ 3  5  7 16]
 [ 3  5  8 17]]

最后,将数组保存为 csv

np.savetxt('file_name.csv',matrix)

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 2019-01-29
    • 2018-07-09
    • 2022-11-17
    • 2019-04-09
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多