1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

numpy > savetxt() > 1次元のデータ書き出しは、各要素に改行が入る > 2次元にする

Last updated at Posted at 2017-04-28
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)

numpyでsavetxt()を使っていてはまった。

test_python_170429a.py
import numpy as np

wrk = [ 3.1415, 2.718, 6.022, 1.023 ]
np.savetxt('testout.csv', wrk, fmt="%.4f")
testout.csv
3.1415
2.7180
6.0220
1.0230

要素ごとに改行が入る。

numpy.savetxt saving every element on new line?

If you give np.savetxt a 1-d array, it will treat said array as a column and write each entry on a new line.

v0.2

二次元にすればいいようだ。

test_python_170429a.py
import numpy as np

wrk = [[ 3.1415, 2.718, 6.022, 1.023 ]]
np.savetxt('testout.csv', wrk, fmt="%.4f")
testout.csv
3.1415 2.7180 6.0220 1.0230

v0.3

reshapeでも可能。

test_python_170429a.py
import numpy as np

wrk = [ 3.1415, 2.718, 6.022, 1.023 ]

wrk = np.array(wrk).reshape(1,4)

np.savetxt('testout.csv', wrk, fmt="%.4f")

v0.2ではこちらのコードでうまく実装できなかった。
reshapeなら楽だ。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?