1
0

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 3 years have passed since last update.

h5pyとnumpyの使い方

Last updated at Posted at 2021-07-22

はじめに

ここでは、h5pyの3.6.0で確認を行った。また、google colabで動かして確認した。

元データの作成

import h5py
import numpy as np

expression_matrix =  np.random.rand(20000, 8)
sample_name = ['sample_' + str(x + 1) for x in range(8)]

書き込みの例

out_file = './h5pysample.h5'

outfh = h5py.File(out_file, 'w')
outfh.create_dataset('expmat', data = expression_matrix)
outfh.create_dataset('sample_name', data = sample_name)
outfh.flush()
outfh.close()

読み出しの例

フォーマットとして出てくる<f8は、little-endian floating point 8byte(64bit)の意味である。Numpyのデータフォーマットに従っている。

in_file = './h5pysample.h5'

infh = h5py.File(in_file, 'r')
print(infh.keys())
## <KeysViewHDF5 ['expmat', 'sample_name']

print(infh['expmat'])
## <HDF5 dataset "expmat": shape (20000, 8), type "<f8">

print(infh['expmat'][::])

expression_matrix_2 = np.array(infh['expmat'])
sample_name_2 = list(infh['sample_name'])

infh.close()

検索

HDF5データの検索で、テーブルリストに該当名称があるかないかは、grepで検索できる。上でいうところのexpmat等で存在するテーブルを確認することが出来る。

参考資料

ソースコード

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?