3
11

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 > ファイル読み書き > np.save() / np.load() / np.savetxt() / np.loadtxt() > バイナリ読み書き / csv読み書き

Last updated at Posted at 2016-10-08
動作環境
Ubuntu 14.04 TLS desktop amd64

numpyでのファイル読み書きを調べた。

参考 https://hydrocul.github.io/wiki/numpy/ndarray-io.html

バイナリ読み書き

fileIO.py
import numpy as np

x_data1 = np.random.rand(10,3).astype(np.float32)

np.save('test_x.npy', x_data1)

x_data2 = np.load('test_x.npy')

print(x_data1)
print(x_data2)
結果
$ python fileio.py [[ 0.84425318  0.69987315  0.3876062 ]
 [ 0.31025144  0.45909852  0.48562685]
 [ 0.83929896  0.80323225  0.99521893]
 [ 0.22227153  0.96804965  0.45995703]
 [ 0.74783921  0.48443237  0.56863761]
 [ 0.06758564  0.25367704  0.30701843]
 [ 0.20933156  0.38771868  0.81327569]
 [ 0.64947814  0.36773711  0.32540786]
 [ 0.08585044  0.92082411  0.78450251]
 [ 0.51730436  0.69340336  0.64845133]]
[[ 0.84425318  0.69987315  0.3876062 ]
 [ 0.31025144  0.45909852  0.48562685]
 [ 0.83929896  0.80323225  0.99521893]
 [ 0.22227153  0.96804965  0.45995703]
 [ 0.74783921  0.48443237  0.56863761]
 [ 0.06758564  0.25367704  0.30701843]
 [ 0.20933156  0.38771868  0.81327569]
 [ 0.64947814  0.36773711  0.32540786]
 [ 0.08585044  0.92082411  0.78450251]
 [ 0.51730436  0.69340336  0.64845133]]

作成されたファイルはバイナリ形式でlessでは見れない。

拡張子は.npyにするようだ。.datという拡張子にしたら.dat.npyというファイルが作成された。

csv読み書き

同じリンクにテキスト形式での読み書きが記載されていた。

fileIO_csv.py
import numpy as np

x_data1 = np.random.rand(10,3).astype(np.float32)

np.savetxt('test_x.csv', x_data1, delimiter=',')

x_data2 = np.loadtxt('test_x.csv', delimiter=',')

print(x_data1)
print(x_data2)
結果
$ python fileio_csv.py 
[[ 0.4589383   0.19229905  0.56762594]
 [ 0.78595251  0.62296402  0.69180036]
 [ 0.79244858  0.4788976   0.67316693]
 [ 0.74409723  0.9375959   0.27137175]
 [ 0.92335385  0.46055159  0.99533266]
 [ 0.83741426  0.86077845  0.96400464]
 [ 0.38847181  0.22187459  0.90489089]
 [ 0.36801556  0.47473967  0.79495519]
 [ 0.93096507  0.29290742  0.01023043]
 [ 0.32679453  0.42028335  0.47830898]]
[[ 0.4589383   0.19229905  0.56762594]
 [ 0.78595251  0.62296402  0.69180036]
 [ 0.79244858  0.4788976   0.67316693]
 [ 0.74409723  0.9375959   0.27137175]
 [ 0.92335385  0.46055159  0.99533266]
 [ 0.83741426  0.86077845  0.96400464]
 [ 0.38847181  0.22187459  0.90489089]
 [ 0.36801556  0.47473967  0.79495519]
 [ 0.93096507  0.29290742  0.01023043]
 [ 0.32679453  0.42028335  0.47830898]]
lessの実行
4.589383006095886230e-01,1.922990530729293823e-01,5.676259398460388184e-01
7.859525084495544434e-01,6.229640245437622070e-01,6.918003559112548828e-01
7.924485802650451660e-01,4.788976013660430908e-01,6.731669306755065918e-01
7.440972328186035156e-01,9.375959038734436035e-01,2.713717520236968994e-01
9.233538508415222168e-01,4.605515897274017334e-01,9.953326582908630371e-01
8.374142646789550781e-01,8.607784509658813477e-01,9.640046358108520508e-01
3.884718120098114014e-01,2.218745946884155273e-01,9.048908948898315430e-01
3.680155575275421143e-01,4.747396707534790039e-01,7.949551939964294434e-01
9.309650659561157227e-01,2.929074168205261230e-01,1.023042947053909302e-02
3.267945349216461182e-01,4.202833473682403564e-01,4.783089756965637207e-01
test_x.csv
3
11
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
3
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?