NumPyの練習問題100
以前のノートで「NumPy公式チュートリアルを効率的に学習する方法」としてPythonのユニットテストフレームワークを利用したNumPyのテストコードを書く方法を紹介しました。
NumPyの練習問題100 (numpy-unittest-100)
今回はこれを応用してNumPyの練習問題100を作成しました。内容としてはNumPy公式チュートリアルのトピック毎にテストケースがあって、実行結果をAssert文を書いて当てる演習問題です。
サンプル(Indexing に関する演習問題)
(問1)xの部分のコードを書いて正しいAssert文にして下さい。
import unittest
import numpy as np
from numpy.testing import assert_array_equal
# ファンシーなインデックス付けとトリック
class TestArrayIndexing(unittest.TestCase):
# インデックス配列によるインデックス付け
def test_fancy_indexing(self):
vector = np.array([7, 6, 5])
assert_array_equal(vector[[2, 0, 1]], np.array([x, x, x]))
# ブール型配列によるインデックス付け
def test_indexing_with_boolean_arrays(self):
metrix = np.arange(9).reshape(3, 3)
assert_array_equal(metrix > 4, np.array([[x, x, x],
[x, x, x],
[x, x, x]]))
# ブール型配列によるインデックス付け(エレメントの選択)
def test_indexing_with_selected_elements(self):
metrix = np.arange(9).reshape(3, 3)
assert_array_equal(metrix[metrix > 4], np.array([x, x, x, x]))
# ブール型配列によるインデックス付け(値の代入)
def test_indexing_with_assignments(self):
metrix = np.arange(9).reshape(3, 3)
metrix[metrix > 4] = 0
assert_array_equal(metrix, np.array([[x, x, x],
[x, x, x],
[x, x, x]]))
if __name__ == '__main__':
unittest.main()
正解のコード
import unittest
import numpy as np
from numpy.testing import assert_array_equal
# ファンシーなインデックス付けとトリック
class TestArrayIndexing(unittest.TestCase):
# インデックス配列によるインデックス付け
def test_fancy_indexing(self):
vector = np.array([7, 6, 5])
assert_array_equal(vector[[2, 0, 1]], np.array([5, 7, 6]))
# ブール型配列によるインデックス付け
def test_indexing_with_boolean_arrays(self):
metrix = np.arange(9).reshape(3, 3)
assert_array_equal(metrix > 4, np.array([[False, False, False],
[False, False, True],
[True, True, True]]))
# ブール型配列によるインデックス付け(エレメントの選択)
def test_indexing_with_selected_elements(self):
metrix = np.arange(9).reshape(3, 3)
assert_array_equal(metrix[metrix > 4], np.array([5, 6, 7, 8]))
# ブール型配列によるインデックス付け(値の代入)
def test_indexing_with_assignments(self):
metrix = np.arange(9).reshape(3, 3)
metrix[metrix > 4] = 0
assert_array_equal(metrix, np.array([[0, 1, 2],
[3, 4, 0],
[0, 0, 0]]))
if __name__ == '__main__':
unittest.main()
PyCharm(推奨)で実行すると以下のようなテスト結果表示になります。
対応テストケース
・test_array_basic.py(NumPyの基礎)
・test_array_broadcast.py(ブロードキャスト)
・test_array_creation.py(配列の生成)
・test_array_eye.py(単位行列)
・test_array_indexing.py(インデックス参照)
・test_array_operation.py(基本的な操作)
・test_array_reshape.py(形状の操作)
・test_array_select.py(配列の選択)
・test_array_slicing.py(スライス)
・test_array_stack.py(配列のスタック)
・test_array_stats.py(統計関数)
・test_array_ufunc.py(ユニバーサル関数)
numpy-unittest-100
この練習問題は以下のGitHubで公開していますのでローカルにクローンしてPyCharm(推奨)で試してみてはいかがでしょうか。NumPy力のアップの役に立てれば幸いです。
最後まで読んで頂きありがとうございます。もしこのノートを気に入ってくれたなら下記のボタンよりフォロー&ブックマーク宜しくお願いします。
【参考文献】
NumPy - Quickstart tutorial https://docs.scipy.org/doc/numpy/user/quickstart.html.
@naoya_t - 私訳「暫定的 NumPy チュートリアル」(日本語訳)http://naoyat.hatenablog.jp/entry/2011/12/29/021414.
Python - 26.4. unittest — ユニットテストフレームワーク https://docs.python.jp/3/library/unittest.html.
Rougier - 100 numpy exercises https://github.com/rougier/numpy-100.
東京大学 松尾研究室 - Numpy Test System.
この記事が気に入ったらサポートをしてみませんか?