numpy 100 Exercises 1-10
numpyそんなに詳しくなかったので、100本ノックやることにした。
ノーヒントクリア厳しいので、ヒントを見てその関数を調べる形式で。
(言語処理のやつも止まってるけどorz)
上記のレポジトリをクローンして、ローカルでJupyter notebookを開いてやっていくのが良さそう。
あとは指示に従って演習を進める。
分からなかったら、ヒントで出た関数をドキュメントで調べる。
目次
- 1. Import the numpy package under the name np (★☆☆)
- 2. Print the numpy version and the configuration (★☆☆)
- 3. Create a null vector of size 10 (★☆☆)
- 4. How to find the memory size of any array (★☆☆)
- 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
- 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
- 7. Create a vector with values ranging from 10 to 49 (★☆☆)
- 8. Reverse a vector (first element becomes last) (★☆☆)
- 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
- 10. Find indices of non-zero elements from 1,2,0,0,4,0(★☆☆)
1. Import the numpy package under the name np (★☆☆)
npという名前でnumpyをインポートする。import XXXX as XX
の書き方でOK
(そもそも、インポートしないと使えない)
import numpy as np
2. Print the numpy version and the configuration (★☆☆)
numpyのバージョンと設定を出力するには、np.__version__
でできる。
print(np.__version__) # 1.17.4
3. Create a null vector of size 10 (★☆☆)
全部0のベクトルを作成する時、np.zeros(size)
を使う
a = np.zeros(10) print(a) # [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4. How to find the memory size of any array (★☆☆)
numpyで作った配列のメモリサイズを求める。少し工夫が必要。
前の問題で作った配列のメモリサイズを求めることにした。
配列aの要素数を求める時は、配列.size
を使い、
配列の各要素のバイト単位でのサイズは、配列.itemsiee
で分かる。
これらの結果をかけ合わせれば、配列全体のメモリサイズが分かる
a.size # 10 配列aの要素数 a.itemsize # 8 配列aの各要素のバイト単位でのサイズ # かけ合わせれば配列全体のメモリサイズが分かる print(a.size * a.itemsize) # 80
5. How to get the documentation of the numpy add function from the command line? (★☆☆)
linuxコマンドで言うところのman
コマンド。
np.info(知りたい関数名)
で使い方を知ることができる。
# np.info(参照したい関数)で使い方がわかる np.info(size) # np.sizeの使い方が出てくる。出力例は略。
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
長さ10の0埋めベクトル、ただし5番目の値が1であるものを作れという問題。
面倒なので最初に0埋めベクトルを作り、インデックスで要素を決め打ちして変更する。
Pythonのリスト操作と同じ感覚(そりゃそうだ)
a = np.zeros(10) a[4] = 1 print(a) # [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
7. Create a vector with values ranging from 10 to 49 (★☆☆)
10から49までの範囲のベクトルを作成する。
範囲指定したarrayの作成は、np.arange(start, stop)
で実現できる。
(今後良く出てくる)
# 範囲指定でベクトル作成。np.arange(start, stop, step) stopの考え方はスライスと同じ a = np.arange(10, 50) a # [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 # 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
8. Reverse a vector (first element becomes last) (★☆☆)
さっきのベクトルを逆順にする。リストの逆順スライスと全く同じで、a[::-1]
でできる
a[::-1] # array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, # 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, # 15, 14, 13, 12, 11, 10])
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
3x3行列を0から8までの数値で作る。範囲指定はarange
で行けるが、
nxm行列で作りたい時は、np.reshape(n, m)
で行と列、次元などを指定する。
範囲指定して再整形するイメージ
# n行m列の行列として生成する時はreshape(n, m) a = np.arange(0, 9).reshape(3,3) a # array([[0, 1, 2], # [3, 4, 5], # [6, 7, 8]])
10. Find indices of non-zero elements from 1,2,0,0,4,0(★☆☆)
1,2,0,0,4,0というarrayの中で、要素が0でないもののインデックスを出すには、
np.nonzero(array)
で実現可能。
np.nonzero([1, 2, 0, 0, 4, 0]) # (array([0, 1, 4]),)