NumPy
sum, meanの使い方
>>> a = np.arange(24).reshape((2,3,4))
>>> a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> a.sum()
276
>>> a.mean()
11.5
>>> a.sum(0)
array([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]])
>>> a.mean(axis=0)
array([[ 6., 7., 8., 9.],
[ 10., 11., 12., 13.],
[ 14., 15., 16., 17.]])
>>> a.sum(1)
array([[12, 15, 18, 21],
[48, 51, 54, 57]])
>>> a.mean(axis=1)
array([[ 4., 5., 6., 7.],
[ 16., 17., 18., 19.]])
>>> a.sum(2)
array([[ 6, 22, 38],
[54, 70, 86]])
>>> a.mean(axis=2)
array([[ 1.5, 5.5, 9.5],
[ 13.5, 17.5, 21.5]])
load, savez, loadtxtの使い方
>>> ary1 = np.arange(10)
>>> ary2 = np.arange(3)
>>> np.savez('data', a = ary1, b = ary2)
>>> npz = np.load('data.npz')
>>> npz['a']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> npz['b']
array([0, 1, 2])
(data.txt
1, 2, 3
10,20,30
が保存している状況で)
>>> ary = np.loadtxt('data.txt', delimiter=',')
>>> ary
array([[ 1., 2., 3.],
[ 10., 20., 30.]])
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。