Python

[Q1-10]Pythonの「100 numpy exercises」をひたすらやってみた話[訳+実行結果]

こんにちは,しまさん(@shimasan0x00)です.

最近Pythonを扱う機会に恵まれています(というか自分から積極的に使っています).

私の場合はDeep Learningなんかを勉強したり,扱うことが多いので必然とNumpyを触る機会が増えます.

本でも勉強できますが,Github上で「100 numpy exercises」という100問の問題を通してNumpyについて学ぶことができるものがあるので試してみることにしました.

問題が英語なので訳と実行結果を載せていきたいと思います.

今回はQ1-10です.

環境

macOS Mojave 10.14

pyenv Python 3.6.7

numpy 1.15.4

・Q1以降はNumpyを「np」でインポートしてあるものとする.

・Jupyter Notebook環境で行なっている.

・マーカーが引いてある問題は解答が「100 numpy exercises」とは異なる.

スポンサーリンク

100 numpy exercises

より良い解もしくは問題の問題があった場合はこちら

スポンサーリンク

Q1-Q10

Q1. numpyを「np」でインポートせよ.

import numpy as np

Q2. numpyのバージョンとコンフィグを表示せよ.

np.__version__
np.show_config()
'1.15.4'

blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]

Q3. size10(10次元)のゼロベクトルを作成せよ.

A = np.zeros(10)
A
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

Q4. 任意サイズの配列(行列)のメモリサイズを表示せよ.

A = np.zeros((10,10,10))
print('Memory Size : {} bytes'.format(A.size * A.itemsize))
Memory Size : 8000 bytes

Q5. コマンドライン上でNumpyのadd函数のドキュメントを表示せよ.

# Command Line
python3 -c "import numpy as np ; np.info(np.add)"
# Notebook
! python3 -c "import numpy as np ; np.info(np.add)"
add(x1, x2[, out])

Add arguments element-wise.

Parameters
----------
x1, x2 : array_like
    The arrays to be added.  If ``x1.shape != x2.shape``, they must be
    broadcastable to a common shape (which may be the shape of one or
    the other).

Returns
-------
add : ndarray or scalar
    The sum of `x1` and `x2`, element-wise.  Returns a scalar if
    both  `x1` and `x2` are scalars.

Notes
-----
Equivalent to `x1` + `x2` in terms of array broadcasting.

Examples
--------
>>> np.add(1.0, 4.0)
5.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.add(x1, x2)
array([[  0.,   2.,   4.],
       [  3.,   5.,   7.],
       [  6.,   8.,  10.]])

Q6. size10(10次元)のゼロベクトルを作成せよ.但し5番目の要素は1とする.

(この問題での「4」は全角の4なのでコピーする際は注意してください.)

A = np.zeros(10)
A[] = 1
A
array([ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.])

Q7. 10から49の値を持った配列(行列)を作成せよ.

A = np.arange(10,50)
A
array([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])

Q8. 任意の行列の中身を逆順にして表示せよ.

A = np.arange(10,50)
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])

Q9. 0-8の値を3×3行列の形で作成せよ.

A = np.arange(9).reshape(3,3)
A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

Q10. [1,2,0,0,4,0]の中からゼロでない要素のインデックスを探し,表示せよ.

A = np.array([1,2,0,0,4,0])
B = np.nonzero(A)
B
(array([0, 1, 4]),)

さいごに

今回は「100 numpy exercises」のQ1-Q10を解きました.

まだまだここら辺は簡単だなという印象ですがこれからどんどん難しくなるでしょう.

ゆるゆると更新していきたいと思います.

ABOUT ME
しまさん
高専→大学編入→大学院→? / 計算社会科学,ウェブマイニングなど / グレープフルーツと本が好き / SNS(Twitter,YouTube,Twitch)やVTuberのデータ分析屋 詳しいプロフィールはこちら≫ 投げ銭はコチラ