LoginSignup
4
1

More than 5 years have passed since last update.

pythonのscipy.ioでarffフォーマットのデータセットを読み込み分析

Last updated at Posted at 2017-01-14

はじめに

arffフォーマットのweka用データセットを分析する必要があり、pythonで読みこんで、使えるようになるのに少し苦戦したので、まとめておきます。

読み込み

scipy.io の loadaiff() を使って読み込めます。 (scipy.io リファレンス 参照)
https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/io.html

readarff.py
from scipy.io import arff
import numpy as np
dataset, meta = arff.loadarff("DARPA99Week3-46.arff")

Array 変換

scipy や scikit-learnで分析するには、通常のnumpyのarrayにしたいので、次のようなスクリプトで変換します。 (Stack Overflowの 「Prepare scipy.io loadarff result for scikit-learn」参照)

arff1.py
ds=np.asarray(dataset.tolist(), dtype=np.float32)
target=np.asarray(ds[:,22].tolist(), dtype=np.int8)
train=ds[:, :21]

もしくは

arff2.py
train_data = dataset[meta.names()[:-1]]
train_array = train_data.view(np.float).reshape(data.shape + (-1,))

グラフ表示

いったんnumpyのarrayにできれば、matplotlibなどで、グラフ表示したり分析を行うことができます。

hist.py
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

duration=ds[:,16]
plt.hist(duration, bins=50)
plt.show()

参照

scipy.io リファレンス
https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/io.html

Prepare scipy.io loadarff result for scikit-learn (Stack Overflow)
http://stackoverflow.com/questions/22873434/prepare-scipy-io-loadarff-result-for-scikit-learn

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