numpyを使った簡単なデータ加工

その買うを、もっとハッピーに。|ハピタス

numpy基礎シリーズ第二弾として、numpyによるアレイのサブセットを使った計算を学習してみようと思う。

スポンサーリンク

Computations on subsets of arrays

先ず、今回の学習に必要なデータをダウンロードする。

%download https://raw.githubusercontent.com/jrjohansson/scientific-python-lectures/master/stockholm_td_adj.dat
Downloaded 'stockholm_td_adj.dat'.

データの中身を確認する(先頭から3行)。

!head -n 3 data
1800  1  1    -6.1    -6.1    -6.1 1
1800  1  2   -15.4   -15.4   -15.4 1
1800  1  3   -15.0   -15.0   -15.0 1

次に、今回の学習に必要なモジュールをインポートする。

%matplotlib inline
import matplotlib.pyplot as plt
from numpy import *

dataformatは、year, month, day, daily average temperature, low, high, locationで、特定月の平均気温だけが知りたい場合、index maskを作成してその月のデータだけを使うことができる。

data = genfromtxt('stockholm_td_adj.dat')
unique(data[:,1]) # the month column takes values from 1 to 12
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.])
mask_feb = data[:,1] == 2 #2月をマスクする。
# the temperature data is in column 3
mean(data[mask_feb,3]) #2月のmean value(平均値)
-3.212109570736596

このnumpy toolは非常にパワフルで、下記の例のように、簡単に全ての月の平均気温をグラフ化できてしまう。

import matplotlib.pylab as pylab
pylab.rcParams['figure.figsize'] = 10, 7
pylab.rcParams["font.size"] = "19"
months = arange(1,13)
monthly_mean = [mean(data[data[:,1] == month, 3]) for month in months]

fig, ax = plt.subplots()
ax.bar(months, monthly_mean)
ax.set_xlabel("Month")
ax.set_ylabel("Monthly avg. temp.");
スポンサーリンク

Reshaping, resizing and stacking arrays

numpy array shapeは、元データをコピーすることなく変えられるので巨大なアレイに対しても高速な処理を可能にしてくれる。

A = array([[n+m*10 for n in range(5)] for m in range(5)])
A
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [20, 21, 22, 23, 24],
       [30, 31, 32, 33, 34],
       [40, 41, 42, 43, 44]])
n, m = A.shape
B = A.reshape((1,n*m))
B
array([[ 0,  1,  2,  3,  4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30,
        31, 32, 33, 34, 40, 41, 42, 43, 44]])
B[0,0:5] = 5 # modify the array
B
array([[ 5,  5,  5,  5,  5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30,
        31, 32, 33, 34, 40, 41, 42, 43, 44]])
A # and the original variable is also changed. B is only a different view of the same data
array([[ 5,  5,  5,  5,  5],
       [10, 11, 12, 13, 14],
       [20, 21, 22, 23, 24],
       [30, 31, 32, 33, 34],
       [40, 41, 42, 43, 44]])

higher-dimensional arrayをベクトルにするのに、flatten関数も同様に使うことができる。しかし、この関数はデータのコピーを作ってしまう。

B = A.flatten()
B
array([ 5,  5,  5,  5,  5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,
       32, 33, 34, 40, 41, 42, 43, 44])
B[0:5] = 10
B
array([10, 10, 10, 10, 10, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,
       32, 33, 34, 40, 41, 42, 43, 44])
A # now A has not changed, because B's data is a copy of A's, not refering to the same data
array([[ 5,  5,  5,  5,  5],
       [10, 11, 12, 13, 14],
       [20, 21, 22, 23, 24],
       [30, 31, 32, 33, 34],
       [40, 41, 42, 43, 44]])
参考サイトhttps://github.com/

スポンサーリンク
スポンサーリンク