今回は第3回の冒頭で紹介した,
Numpyの導入
Numpyはオープンソースの拡張モジュールで行列や多次元配列と,
ここではインストールの仕方とNumpyの簡単な実行例を確認しておきましょう。
インストール
WindowsとMacOSXのPCにNumpyをインストールする場合は,
しかし,
ソースコードリポジトリから最新版のインストールを行う場合は,
$ svn co http://svn.scipy.org/svn/numpy/trunk numpy $ cd numpy $ su $ sudo python setup.py install
また,
$ mkdir ~/tmp $ cd ~/tmp $ git clone git://github.com/numpy/numpy.git numpy $ cd numpy $ sudo python setup.py install
インストールを終えたら,
$ sudo easy_install nose
実際に単体テストを実行してみると,
>>> import numpy >>> numpy.test() Running unit tests for numpy NumPy version 2.0.0.dev8716 NumPy is installed in /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/numpy Python version 2.6.6 (r266:84292, Oct 8 2010, 22:12:21) [GCC 4.2.1 (Apple Inc. build 5646)] nose version 0.11.3 (中略 ---------------------------------------------------------------------- Ran 3029 tests in 17.020s OK (KNOWNFAIL=4, SKIP=1) <nose.result.TextTestResult run=3029 errors=0 failures=0>
numpy.
Numpy簡単な使い方
それでは,
$ python Python 2.6.6 (r266:84292, Oct 8 2010, 22:12:21) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> >>> import numpy as np
以下の例では,
Numpyのバージョンを確認する
これで現在利用しているNumpyのバージョンを確認できます。
>>> np.version.version '2.0.0.dev8716'
キーワードからNumpyの機能を探す
Numpyでは関数lookfor()を利用することで,
>>> np.lookfor('array')
すると,
Search results for 'array' -------------------------- numpy.array Create an array. numpy.asarray Convert the input to an array. numpy.ndarray ndarray(shape, dtype=float, buffer=None, offset=0, numpy.asfarray Return an array converted to a float type. numpy.recarray Construct an ndarray that allows field access using attributes. numpy.all Test whether all array elements along a given axis evaluate to True. numpy.array_str Return a string representation of the data in an array. numpy.any Test whether any array element along a given axis evaluates to True. numpy.eye Return a 2-D array with ones on the diagonal and zeros elsewhere. (略...
検索結果の画面から,