numpy基礎シリーズ第二弾として、numpyによるアレイのサブセットを使った計算を学習してみようと思う。
スポンサーリンク
Computations on subsets of arrays¶
%download https://raw.githubusercontent.com/jrjohansson/scientific-python-lectures/master/stockholm_td_adj.dat
データの中身を確認する(先頭から3行)。
!head -n 3 data
次に、今回の学習に必要なモジュールをインポートする。
%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
mask_feb = data[:,1] == 2 #2月をマスクする。
# the temperature data is in column 3
mean(data[mask_feb,3]) #2月のmean value(平均値)
この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
n, m = A.shape
B = A.reshape((1,n*m))
B
B[0,0:5] = 5 # modify the array
B
A # and the original variable is also changed. B is only a different view of the same data
higher-dimensional arrayをベクトルにするのに、flatten関数も同様に使うことができる。しかし、この関数はデータのコピーを作ってしまう。
B = A.flatten()
B
B[0:5] = 10
B
A # now A has not changed, because B's data is a copy of A's, not refering to the same data
スポンサーリンク
スポンサーリンク