Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active April 14, 2016 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cartman0/9fa48b89664dc08ef82a55877767b5a0 to your computer and use it in GitHub Desktop.
Save Cartman0/9fa48b89664dc08ef82a55877767b5a0 to your computer and use it in GitHub Desktop.
NumPy Tutorial メモ1 (Basic)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"toc": "true"
},
"cell_type": "markdown",
"source": "# Table of Contents\n <p><div class=\"lev1\"><a href=\"#NumPyTutorial-1-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>NumPyTutorial 1</a></div><div class=\"lev2\"><a href=\"#Prerequisites(前知識)-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Prerequisites(前知識)</a></div><div class=\"lev3\"><a href=\"#NumPyのインストール-1.1.1\"><span class=\"toc-item-num\">1.1.1&nbsp;&nbsp;</span>NumPyのインストール</a></div><div class=\"lev2\"><a href=\"#The-Basics(基礎)-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>The Basics(基礎)</a></div><div class=\"lev3\"><a href=\"#ndarrayオブジェクトが持つより重要な属性:-1.2.1\"><span class=\"toc-item-num\">1.2.1&nbsp;&nbsp;</span>ndarrayオブジェクトが持つより重要な属性:</a></div><div class=\"lev4\"><a href=\"#ndarray.ndim-1.2.1.1\"><span class=\"toc-item-num\">1.2.1.1&nbsp;&nbsp;</span>ndarray.ndim</a></div><div class=\"lev4\"><a href=\"#ndarray.shape-1.2.1.2\"><span class=\"toc-item-num\">1.2.1.2&nbsp;&nbsp;</span>ndarray.shape</a></div><div class=\"lev4\"><a href=\"#ndarray.size-1.2.1.3\"><span class=\"toc-item-num\">1.2.1.3&nbsp;&nbsp;</span>ndarray.size</a></div><div class=\"lev4\"><a href=\"#ndarray.dtype-1.2.1.4\"><span class=\"toc-item-num\">1.2.1.4&nbsp;&nbsp;</span>ndarray.dtype</a></div><div class=\"lev4\"><a href=\"#ndarray.itemsize-1.2.1.5\"><span class=\"toc-item-num\">1.2.1.5&nbsp;&nbsp;</span>ndarray.itemsize</a></div><div class=\"lev4\"><a href=\"#ndarray.data-1.2.1.6\"><span class=\"toc-item-num\">1.2.1.6&nbsp;&nbsp;</span>ndarray.data</a></div><div class=\"lev3\"><a href=\"#example-1.2.2\"><span class=\"toc-item-num\">1.2.2&nbsp;&nbsp;</span>example</a></div><div class=\"lev3\"><a href=\"#Array-Creation(配列の作成)-1.2.3\"><span class=\"toc-item-num\">1.2.3&nbsp;&nbsp;</span>Array Creation(配列の作成)</a></div><div class=\"lev4\"><a href=\"#np.array()関数による生成-1.2.3.1\"><span class=\"toc-item-num\">1.2.3.1&nbsp;&nbsp;</span><code>np.array()</code>関数による生成</a></div><div class=\"lev5\"><a href=\"#dtypeの指定-1.2.3.1.1\"><span class=\"toc-item-num\">1.2.3.1.1&nbsp;&nbsp;</span>dtypeの指定</a></div><div class=\"lev4\"><a href=\"#その他の関数による生成-1.2.3.2\"><span class=\"toc-item-num\">1.2.3.2&nbsp;&nbsp;</span>その他の関数による生成</a></div><div class=\"lev5\"><a href=\"#numpy.zeros()-1.2.3.2.1\"><span class=\"toc-item-num\">1.2.3.2.1&nbsp;&nbsp;</span>numpy.zeros()</a></div><div class=\"lev5\"><a href=\"#numpy.ones()-1.2.3.2.2\"><span class=\"toc-item-num\">1.2.3.2.2&nbsp;&nbsp;</span>numpy.ones()</a></div><div class=\"lev5\"><a href=\"#numpy.empty-1.2.3.2.3\"><span class=\"toc-item-num\">1.2.3.2.3&nbsp;&nbsp;</span>numpy.empty</a></div><div class=\"lev5\"><a href=\"#numpy.zeros_like-1.2.3.2.4\"><span class=\"toc-item-num\">1.2.3.2.4&nbsp;&nbsp;</span>numpy.zeros_like</a></div><div class=\"lev5\"><a href=\"#numpy.ones_like-1.2.3.2.5\"><span class=\"toc-item-num\">1.2.3.2.5&nbsp;&nbsp;</span>numpy.ones_like</a></div><div class=\"lev5\"><a href=\"#numpy.empty_like-1.2.3.2.6\"><span class=\"toc-item-num\">1.2.3.2.6&nbsp;&nbsp;</span>numpy.empty_like</a></div><div class=\"lev5\"><a href=\"#numpy.identity-1.2.3.2.7\"><span class=\"toc-item-num\">1.2.3.2.7&nbsp;&nbsp;</span>numpy.identity</a></div><div class=\"lev3\"><a href=\"#Printing-Arrays(配列の表示)-1.2.4\"><span class=\"toc-item-num\">1.2.4&nbsp;&nbsp;</span>Printing Arrays(配列の表示)</a></div><div class=\"lev3\"><a href=\"#Basic-Operations(基本演算)-1.2.5\"><span class=\"toc-item-num\">1.2.5&nbsp;&nbsp;</span>Basic Operations(基本演算)</a></div><div class=\"lev3\"><a href=\"#Universal-Functions(ユニバーサル関数)-1.2.6\"><span class=\"toc-item-num\">1.2.6&nbsp;&nbsp;</span>Universal Functions(ユニバーサル関数)</a></div><div class=\"lev3\"><a href=\"#Indexing,-Slicing-and-Iterating(インデックス、スライス、イテレート)-1.2.7\"><span class=\"toc-item-num\">1.2.7&nbsp;&nbsp;</span>Indexing, Slicing and Iterating(インデックス、スライス、イテレート)</a></div><div class=\"lev4\"><a href=\"#One-dimensional-1.2.7.1\"><span class=\"toc-item-num\">1.2.7.1&nbsp;&nbsp;</span>One-dimensional</a></div><div class=\"lev4\"><a href=\"#Multidimensional-1.2.7.2\"><span class=\"toc-item-num\">1.2.7.2&nbsp;&nbsp;</span>Multidimensional</a></div><div class=\"lev4\"><a href=\"#dots-1.2.7.3\"><span class=\"toc-item-num\">1.2.7.3&nbsp;&nbsp;</span>dots</a></div><div class=\"lev4\"><a href=\"#Iterating-1.2.7.4\"><span class=\"toc-item-num\">1.2.7.4&nbsp;&nbsp;</span>Iterating</a></div><div class=\"lev3\"><a href=\"#横(列)ベクトルと縦(行)ベクトル-1.2.8\"><span class=\"toc-item-num\">1.2.8&nbsp;&nbsp;</span>横(列)ベクトルと縦(行)ベクトル</a></div><div class=\"lev2\"><a href=\"#randomクラスで使っているアルゴリズム-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>randomクラスで使っているアルゴリズム</a></div><div class=\"lev2\"><a href=\"#参考リンク-1.4\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>参考リンク</a></div>"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# NumPyTutorial 1"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [QuickStartTutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)\n- [私訳「暫定的 NumPy チュートリアル」](http://naoyat.hatenablog.jp/entry/2011/12/29/021414)\n- [機械学習の Python との出会い](http://www.kamishima.net/mlmpyja/)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Prerequisites(前知識)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "take a look at the [Python tutorial](http://docs.python.jp/3/tutorial/)."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### NumPyのインストール"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "http://scipy.org/install.html"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nconda install numpy\n```"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## The Basics(基礎)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "NumPy’s main object is the homogeneous multidimensional array(NumPyのメインオブジェクトは、多次元(均質な(同じ型の))配列になる). \n\nIt is a table of elements (usually numbers), all of the same type, \nindexed by a tuple of positive integers. (\nNumPyのオブジェクトは、すべて同じ型で、\n正の整数のタプルで添字付けされた(たいてい数字)の要素のみのテーブル。)\n\nIn Numpy dimensions(次元) are called *axes(軸)*. The number of axes(軸の数) is *rank(ランク)*.\n\n\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`numpy.ndarray` は、数学の概念で言えば、\n- 1次元: ベクトル\n- 2次元: 行列\n- 3次元以上: テンソル\n\n[参考](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "For example, the coordinates of a point in 3D space `[1, 2, 1]`\nis an array of rank 1, because it has one axis.([1,2,1])\n\nThat axis has a length of 3. (その1軸は長さ3をもつ)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "In example pictured below, the array has rank 2 (it is 2-dimensional). \n\nThe **first dimension (axis) has a length of 2**,([1.,...],[0.,...])\n\nthe **second dimension has a length of 3**.([1.,0.,0.])\n\n```\n[[ 1., 0., 0.],\n [ 0., 1., 2.]]\n```"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Numpy’s array class is called **ndarray**. \n\nIt is also known by the alias array. \nNote that numpy.array is not the same as the Standard Python Library class array.array, \n(標準Pythonライブラリのarray.arrayクラスとは別物。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### ndarrayオブジェクトが持つより重要な属性:"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "import numpy as np\na = np.arange(15).reshape(3, 5)\na",
"execution_count": 1,
"outputs": [
{
"execution_count": 1,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0, 1, 2, 3, 4],\n [ 5, 6, 7, 8, 9],\n [10, 11, 12, 13, 14]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.ndim"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the number of axes (dimensions) of the array. \n配列の軸(次元)数。\n\nIn the Python world, the number of dimensions is referred to as *rank*."
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.ndim",
"execution_count": 2,
"outputs": [
{
"execution_count": 2,
"output_type": "execute_result",
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.shape"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the dimensions of the array. \n配列の寸法。\n\nThis is a tuple of integers indicating the size of the array in each dimension. \nFor a matrix with *n* rows and *m* columns, shape will be **(n,m)**. \n(これは各次元の配列のサイズを表す整数のタプル。\nn行m列の行列の場合、shapeは **(n,m)** となる。)\n\nThe length of the shape tuple is therefore the rank, or number of dimensions, ndim.(shapeタプルの長さはランク、または次元数 ndim になる)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.shape",
"execution_count": 3,
"outputs": [
{
"execution_count": 3,
"output_type": "execute_result",
"data": {
"text/plain": "(3, 5)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.size"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the total number of elements of the array. \n(配列の要素のトータル数。)\n\nThis is equal to the product of the elements of shape."
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.size",
"execution_count": 4,
"outputs": [
{
"execution_count": 4,
"output_type": "execute_result",
"data": {
"text/plain": "15"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.dtype"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "an object describing the type of the elements in the array. \n(配列中の要素のtype(型)を記述するオブジェクト)\n\nOne can create or specify dtype’s using standard Python types. \n(標準のPythonのtype(bool, int, float, complex)を用いて作成もしくは指定することができ、\n\nAdditionally NumPy provides types of its own. \nNumPy も、独自のtypeを提供している。)\n`numpy.int32`, `numpy.int16`, and `numpy.float64` are some examples."
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.dtype",
"execution_count": 5,
"outputs": [
{
"execution_count": 5,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('int32')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "文字列型については,ビルトイン型の str とは,少し違う。\n`numpy.ndarray` では、要素の大きさが同じである必要があるため、\n文字列は固定長になる。\n\nPython の文字列型に対応する NumPy での文字列型は,NumPy の型を返す関数 `np.dtype()` を用いて, `np.dtype('S<文字列長>') [4]` のように指定する。\n\n例えば,最大長が16である文字列を扱う場合は `np.dtype(\"S16\")` のように指定する。\nUnicode文字列の場合は,この `S` を `U` に置き換えます."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.itemsize"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the size in bytes of each element of the array. \n(配列の各要素のバイト数。)\n\nFor example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). \nIt is equivalent to `ndarray.dtype.itemsize`."
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.itemsize",
"execution_count": 6,
"outputs": [
{
"execution_count": 6,
"output_type": "execute_result",
"data": {
"text/plain": "4"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.dtype.itemsize",
"execution_count": 7,
"outputs": [
{
"execution_count": 7,
"output_type": "execute_result",
"data": {
"text/plain": "4"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### ndarray.data"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the buffer containing the actual elements of the array. \n(配列の実際の要素を収容しているバッファ。)\n\nNormally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.\n(配列の要素には添字機能を用いてアクセスできるので、普通はこの属性を用いる必要はないだろう。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.data",
"execution_count": 8,
"outputs": [
{
"execution_count": 8,
"output_type": "execute_result",
"data": {
"text/plain": "<memory at 0x000001737C7B51F8>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### example"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "type(a)",
"execution_count": 9,
"outputs": [
{
"execution_count": 9,
"output_type": "execute_result",
"data": {
"text/plain": "numpy.ndarray"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "b = np.array([6, 7, 8])\nb",
"execution_count": 10,
"outputs": [
{
"execution_count": 10,
"output_type": "execute_result",
"data": {
"text/plain": "array([6, 7, 8])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "type(b)",
"execution_count": 11,
"outputs": [
{
"execution_count": 11,
"output_type": "execute_result",
"data": {
"text/plain": "numpy.ndarray"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = np.array((9, 10, 11))\nc",
"execution_count": 12,
"outputs": [
{
"execution_count": 12,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 9, 10, 11])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "type(c)",
"execution_count": 13,
"outputs": [
{
"execution_count": 13,
"output_type": "execute_result",
"data": {
"text/plain": "numpy.ndarray"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Array Creation(配列の作成)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "There are several ways to create arrays.\n(配列を作成する方法は何通りかある。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "For example, you can create an array from a regular Python list or tuple using the array function. \n普通のPythonのリストやタプルからarray関数で作成する方法。\n\nThe type of the resulting array is deduced from the type of the elements in the sequences.\n(出来上がる配列のtypeは、シーケンスの要素のtype から推論される。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### `np.array()`関数による生成"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)\n```\nCreate an array.\n\n最初の引数 `object` には,配列の内容を,`array_like` というtype で与える。\nこの `array_like` というtype は,配列を `np.ndarray` の他,(多重)リストや(多重)タプルで表現したもの。 \n- リストの場合は,ネストしていない直線状のリストでベクトルを表す。\n- 行列は,直線状リストで表した行を要素とするリスト,すなわち2重にネストしたリストで表します. \n\nもう一つの引数 dtype は,配列の要素の型を指定しする。\n\n- [numpy.array](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array.html)\n- [numpy.arrray 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.array)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "要素が 1, 2, 3 である長さ 3 のベクトルの例:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\na = np.array([1, 2, 3])\na",
"execution_count": 14,
"outputs": [
{
"execution_count": 14,
"output_type": "execute_result",
"data": {
"text/plain": "array([1, 2, 3])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "タプルを使った表現も可能:"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a = np.array((10, 20, 30))\na",
"execution_count": 15,
"outputs": [
{
"execution_count": 15,
"output_type": "execute_result",
"data": {
"text/plain": "array([10, 20, 30])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "リストの要素に `numpy.ndarray` やタプルを含むことも可能:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1.0, 2.0, 3.0])\nb = np.array([a, (10, 20, 30)])\nb",
"execution_count": 16,
"outputs": [
{
"execution_count": 16,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1., 2., 3.],\n [ 10., 20., 30.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": false
},
"cell_type": "code",
"source": "import numpy as np\na = np.array([2,3,4])\na",
"execution_count": 17,
"outputs": [
{
"execution_count": 17,
"output_type": "execute_result",
"data": {
"text/plain": "array([2, 3, 4])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": " a.dtype",
"execution_count": 18,
"outputs": [
{
"execution_count": 18,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('int32')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.array([1.2, 3.5, 5.1])\nb",
"execution_count": 19,
"outputs": [
{
"execution_count": 19,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1.2, 3.5, 5.1])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b.dtype",
"execution_count": 20,
"outputs": [
{
"execution_count": 20,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('float64')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "A frequent error consists in calling **array** with multiple numeric arguments, \nrather than providing a single list of numbers as an argument.\n(array関数の引数に数値のリストを渡すところを複数の数値をそのまま引数に渡して、エラーになるというのはよくある。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array(1,2,3,4) # error",
"execution_count": 21,
"outputs": [
{
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-21-979c362d66e0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# error\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mValueError\u001b[0m: only 2 non-keyword arguments accepted"
],
"ename": "ValueError",
"output_type": "error",
"evalue": "only 2 non-keyword arguments accepted"
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3,4]) # Right",
"execution_count": 22,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**array** transforms sequences of sequences into two-dimensional arrays, \nsequences of sequences of sequences into three-dimensional arrays, and so on.\n(**array** は、シークエンスのシークエンスを2次元配列へ、\nシークエンスのシークエンスのシークエンスを3次元配列へ、といったような変換をしてくれる。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.array([(1.5, 2, 3), (4, 5, 6)])\nb",
"execution_count": 23,
"outputs": [
{
"execution_count": 23,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1.5, 2. , 3. ],\n [ 4. , 5. , 6. ]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = np.array([\n [[1, 2, 3], \n [4, 5, 6]],\n \n [[7, 8, 9], \n [10, 11, 12]]\n ])\nc",
"execution_count": 24,
"outputs": [
{
"execution_count": 24,
"output_type": "execute_result",
"data": {
"text/plain": "array([[[ 1, 2, 3],\n [ 4, 5, 6]],\n\n [[ 7, 8, 9],\n [10, 11, 12]]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c.shape",
"execution_count": 25,
"outputs": [
{
"execution_count": 25,
"output_type": "execute_result",
"data": {
"text/plain": "(2, 2, 3)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The type of the array can also be explicitly specified at creation time(配列作成時に、typeを明示的に指定することも可能):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = np.array([ \n [1 + 1j, 2], \n [3, 4] \n ], dtype=complex)\nc",
"execution_count": 26,
"outputs": [
{
"execution_count": 26,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1.+1.j, 2.+0.j],\n [ 3.+0.j, 4.+0.j]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Often, the elements of an array are originally unknown, \nbut its size is known. \n(配列の要素が最初は不明だが、サイズはわかっているということもある。)\n\nHence, NumPy offers several functions to create arrays with \ninitial placeholder content. \n(そのために、NumPy は、初めはplaceholder の内容を入れて配列を作成する関数をいくつか用意している。)\n\nThese minimize the necessity of growing arrays, an expensive operation.\n配列を拡大する(これはコストのかかる操作)必要性を最小限にしてくれる。"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### dtypeの指定"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "配列の `dtype` 属性を指定するには,\n- `numpy.array()` などの配列生成関数の `dtype` 引数で指定する方法と, \n- `numpy.ndarray` の `numpy.ndarray.astype()` メソッドを使う方法とがある."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`dtype` 引数を指定する方法:\n \n`np.array()` では、要素が全て整数の場合は、要素の型は整数になるが、\nそれを浮動小数点にするには、次のように指定する:"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a = np.array([1, 2, 3])\na.dtype",
"execution_count": 27,
"outputs": [
{
"execution_count": 27,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('int32')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a = np.array([1, 2, 3], dtype=np.float)\na.dtype",
"execution_count": 28,
"outputs": [
{
"execution_count": 28,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('float64')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "浮動小数点型の配列を複素数型で作り直す場合は,次のようになる:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1.0, 1.5, 2.0])\na.dtype",
"execution_count": 29,
"outputs": [
{
"execution_count": 29,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('float64')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a = np.array(a, dtype=np.complex)\na.dtype",
"execution_count": 30,
"outputs": [
{
"execution_count": 30,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('complex128')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a",
"execution_count": 31,
"outputs": [
{
"execution_count": 31,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1.0+0.j, 1.5+0.j, 2.0+0.j])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`numpy.ndarray.astype()` メソッドを使う方法:\n\nメソッドを使う方針でも,メソッド `np.ndarray.astype()` が同様に利用できる。"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1, 2, 3])\na.dtype",
"execution_count": 32,
"outputs": [
{
"execution_count": 32,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('int32')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = a.astype(np.float)\na.dtype",
"execution_count": 33,
"outputs": [
{
"execution_count": 33,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('float64')"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "a",
"execution_count": 34,
"outputs": [
{
"execution_count": 34,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1., 2., 3.])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### その他の関数による生成"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.zeros()"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.zeros(shape, dtype=float, order='C')\n```\nReturn a new array of given shape and type, filled with zeros.\n\n- [numpy.zeros](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.zeros.html)\n- [numpy.zeros 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.zeros)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "長さが 3 の0ベクトルの例:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.zeros(3)",
"execution_count": 35,
"outputs": [
{
"execution_count": 35,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0., 0., 0.])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The function `zeros` creates an array full of zeros, \n(関数 `zeros` は 0 で埋まった配列を)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.zeros((3, 4))",
"execution_count": 36,
"outputs": [
{
"execution_count": 36,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 0., 0., 0.],\n [ 0., 0., 0., 0.],\n [ 0., 0., 0., 0.]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.ones()"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.ones(shape, dtype=None, order='C')\n```\n\nReturn a new array of given shape and type, filled with ones.\n\n- [numpy.ones()](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.ones.html)\n- [numpy.ones 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.ones)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "$3 \\times 4$ の1行列の例。引数をタプルにする:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.ones((3, 4))",
"execution_count": 37,
"outputs": [
{
"execution_count": 37,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "the function `ones` creates an array full of ones, \n(関数 ones は 1 で埋まった配列を、)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.ones((2, 3, 4), dtype=np.int16)",
"execution_count": 38,
"outputs": [
{
"execution_count": 38,
"output_type": "execute_result",
"data": {
"text/plain": "array([[[1, 1, 1, 1],\n [1, 1, 1, 1],\n [1, 1, 1, 1]],\n\n [[1, 1, 1, 1],\n [1, 1, 1, 1],\n [1, 1, 1, 1]]], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.ones((2, 3, 4, 5))",
"execution_count": 39,
"outputs": [
{
"execution_count": 39,
"output_type": "execute_result",
"data": {
"text/plain": "array([[[[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]]],\n\n\n [[[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]],\n\n [[ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.],\n [ 1., 1., 1., 1., 1.]]]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.empty"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "配列を生成した後,その内容をすぐ後で書き換える場合には,\n配列の要素を全て 0 や 1 にするのは無駄な処理になる。 \nそこで,要素の値が不定の状態のままで、指定した大きさの配列を生成する関数 `np.empty()` がある。"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.empty(shape, dtype=float, order='C')\n```\n\nReturn a new array of given shape and type, without initializing entries.\n\n- [numpy.empty](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.empty.html)\n- [numpy.empty 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.empty)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "and the function `empty` creates an array whose initial content is random and depends on the state of the memory. \n(関数 `empty` はメモリの状態に依存したランダムな内容の配列を作成する。)\n\nBy default, the `dtype` of the created array is `float64`.\n(デフォルトで、作成された配列の `dtype` は `float64` である。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "emp = np.empty((2, 3))\nemp",
"execution_count": 40,
"outputs": [
{
"execution_count": 40,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 0., 0.],\n [ 0., 0., 0.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "emp.dtype",
"execution_count": 41,
"outputs": [
{
"execution_count": 41,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('float64')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "To create sequences of numbers, \nNumPy provides a function analogous to `range` that returns arrays instead of lists\n(数値のシークエンスを作成するために、\nNumPy は、リストの代わりに配列を返す、rangeに似た関数を用意している。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.arange(10, 30, 5)",
"execution_count": 42,
"outputs": [
{
"execution_count": 42,
"output_type": "execute_result",
"data": {
"text/plain": "array([10, 15, 20, 25])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.arange(0, 2, 0.3)",
"execution_count": 43,
"outputs": [
{
"execution_count": 43,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "When `arange` is used with floating point arguments, \nit is generally not possible to predict the number of elements obtained, \ndue to the finite floating point precision. \n(arangeが、浮動小数点数を引数として用いられる場合、\n浮動小数点の精度が有限であるため、一般に、得られる要素数を予測することはできない。)\n\nFor this reason, \nit is usually better to use the function `linspace` that receives as an argument the number of elements that we want, instead of the step\n(このため、ステップの代わりに必要な要素数を引数として渡せる関数`linspace` を使ったほうが大抵良い):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.linspace(0, 2, 9) # 9 numbers from 0 to 2 ",
"execution_count": 44,
"outputs": [
{
"execution_count": 44,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "x = np.linspace(0, 2 * np.pi, 100) # 100 numbers from 0 to 2pi [rad]\nprint(x)",
"execution_count": 45,
"outputs": [
{
"text": "[ 0. 0.06346652 0.12693304 0.19039955 0.25386607 0.31733259\n 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317\n 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081\n 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634 1.45972992\n 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903\n 1.90399555 1.96746207 2.03092858 2.0943951 2.15786162 2.22132814\n 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725\n 2.66559377 2.72906028 2.7925268 2.85599332 2.91945984 2.98292636\n 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547\n 3.42719199 3.4906585 3.55412502 3.61759154 3.68105806 3.74452458\n 3.8079911 3.87145761 3.93492413 3.99839065 4.06185717 4.12532369\n 4.1887902 4.25225672 4.31572324 4.37918976 4.44265628 4.5061228\n 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191\n 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545 5.26772102\n 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012\n 5.71198664 5.77545316 5.83891968 5.9023862 5.96585272 6.02931923\n 6.09278575 6.15625227 6.21971879 6.28318531]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "f = np.sin(x)\nprint(f)",
"execution_count": 46,
"outputs": [
{
"text": "[ 0.00000000e+00 6.34239197e-02 1.26592454e-01 1.89251244e-01\n 2.51147987e-01 3.12033446e-01 3.71662456e-01 4.29794912e-01\n 4.86196736e-01 5.40640817e-01 5.92907929e-01 6.42787610e-01\n 6.90079011e-01 7.34591709e-01 7.76146464e-01 8.14575952e-01\n 8.49725430e-01 8.81453363e-01 9.09631995e-01 9.34147860e-01\n 9.54902241e-01 9.71811568e-01 9.84807753e-01 9.93838464e-01\n 9.98867339e-01 9.99874128e-01 9.96854776e-01 9.89821442e-01\n 9.78802446e-01 9.63842159e-01 9.45000819e-01 9.22354294e-01\n 8.95993774e-01 8.66025404e-01 8.32569855e-01 7.95761841e-01\n 7.55749574e-01 7.12694171e-01 6.66769001e-01 6.18158986e-01\n 5.67059864e-01 5.13677392e-01 4.58226522e-01 4.00930535e-01\n 3.42020143e-01 2.81732557e-01 2.20310533e-01 1.58001396e-01\n 9.50560433e-02 3.17279335e-02 -3.17279335e-02 -9.50560433e-02\n -1.58001396e-01 -2.20310533e-01 -2.81732557e-01 -3.42020143e-01\n -4.00930535e-01 -4.58226522e-01 -5.13677392e-01 -5.67059864e-01\n -6.18158986e-01 -6.66769001e-01 -7.12694171e-01 -7.55749574e-01\n -7.95761841e-01 -8.32569855e-01 -8.66025404e-01 -8.95993774e-01\n -9.22354294e-01 -9.45000819e-01 -9.63842159e-01 -9.78802446e-01\n -9.89821442e-01 -9.96854776e-01 -9.99874128e-01 -9.98867339e-01\n -9.93838464e-01 -9.84807753e-01 -9.71811568e-01 -9.54902241e-01\n -9.34147860e-01 -9.09631995e-01 -8.81453363e-01 -8.49725430e-01\n -8.14575952e-01 -7.76146464e-01 -7.34591709e-01 -6.90079011e-01\n -6.42787610e-01 -5.92907929e-01 -5.40640817e-01 -4.86196736e-01\n -4.29794912e-01 -3.71662456e-01 -3.12033446e-01 -2.51147987e-01\n -1.89251244e-01 -1.26592454e-01 -6.34239197e-02 -2.44929360e-16]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "# math.pi とnumpy.piは同じなのか\nimport math\nmath.pi == np.pi",
"execution_count": 47,
"outputs": [
{
"execution_count": 47,
"output_type": "execute_result",
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.pi is math.pi",
"execution_count": 48,
"outputs": [
{
"execution_count": 48,
"output_type": "execute_result",
"data": {
"text/plain": "False"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "See also\n\n- [array](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.array.html#numpy.array) \n- [zeros](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.zeros.html#numpy.zeros) \n- [ones](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ones.html#numpy.ones) \n- [empty](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.empty.html#numpy.empty) \n- [arange](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.arange.html#numpy.arange) \n- [linspace](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.linspace.html#numpy.linspace) \n- [numpy.random.rand](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.rand.html#numpy.random.rand) \n- [numpy.random.randn (正規分布N(0,1)から乱数を返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.randn.html#numpy.random.randn) \n- [fromfunction](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.fromfunction.html#numpy.fromfunction) \n- [fromfile](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.fromfile.html#numpy.fromfile)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.zeros_like"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnp.zeros_like(a, dtype=None)\n```\n\nReturn an array of zeros with the same shape and type as a given array.\n\n- [numpy.zeros_like](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.zeros_like.html#numpy.zeros_like)\n- [numpy.zeros_like 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.zeros_like)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "この例では, $2 \\times 3$ の行列 a と同じ大きさの 0行列を生成する:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([[1, 2, 3], [2, 3, 4]])\nnp.zeros_like(a)",
"execution_count": 49,
"outputs": [
{
"execution_count": 49,
"output_type": "execute_result",
"data": {
"text/plain": "array([[0, 0, 0],\n [0, 0, 0]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.zeros_like(c)",
"execution_count": 50,
"outputs": [
{
"execution_count": 50,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0.+0.j, 0.+0.j],\n [ 0.+0.j, 0.+0.j]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.ones_like"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.ones_like(a, dtype=None, order='K', subok=True)\n```\nReturn an array of ones with the same shape and type as a given array.\n\n- [numpy.ones_like](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.ones_like.html)\n- [numpy.ones_like 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.ones_like)"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "np.ones_like(c)",
"execution_count": 51,
"outputs": [
{
"execution_count": 51,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1.+0.j, 1.+0.j],\n [ 1.+0.j, 1.+0.j]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.empty_like"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.empty_like(a, dtype=None, order='K', subok=True)\n```\n\nReturn a new array with the same shape and type as a given array.\n\n- [numpy.empty_like](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.empty_like.html)\n- [numpy.empty_like 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.empty_like)"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "np.empty_like(c)",
"execution_count": 52,
"outputs": [
{
"execution_count": 52,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1.+0.j, 1.+0.j],\n [ 1.+0.j, 1.+0.j]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### numpy.identity"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nnumpy.identity(n, dtype=None)\n```\n\nReturn the identity array.\n\n- [numpy.identity]()\n- [numpy.identity 日本語訳](http://www.kamishima.net/mlmpyja/nbayes1/ndarray.html#np.identity)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`n` は、行列の大きさを表す。\n例えば,4 と指定すると,単位行列は正方行列なので、\n大きさ $4 \\times 4$ の行列を指定したことになる:"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.identity(4)",
"execution_count": 53,
"outputs": [
{
"execution_count": 53,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 1., 0., 0., 0.],\n [ 0., 1., 0., 0.],\n [ 0., 0., 1., 0.],\n [ 0., 0., 0., 1.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.rand()",
"execution_count": 54,
"outputs": [
{
"execution_count": 54,
"output_type": "execute_result",
"data": {
"text/plain": "0.21997621052048966"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.rand(1)",
"execution_count": 55,
"outputs": [
{
"execution_count": 55,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0.86422861])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.rand(2)",
"execution_count": 56,
"outputs": [
{
"execution_count": 56,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0.41223578, 0.29693131])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.rand(2, 3)",
"execution_count": 57,
"outputs": [
{
"execution_count": 57,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0.98882755, 0.13003944, 0.28143098],\n [ 0.51028739, 0.27926848, 0.32342224]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.rand(2, 2, 3)",
"execution_count": 58,
"outputs": [
{
"execution_count": 58,
"output_type": "execute_result",
"data": {
"text/plain": "array([[[ 0.63080113, 0.50483757, 0.84039243],\n [ 0.79466475, 0.89700236, 0.11939009]],\n\n [[ 0.40637348, 0.4668876 , 0.27641308],\n [ 0.60587571, 0.75736269, 0.78786795]]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.randn()",
"execution_count": 59,
"outputs": [
{
"execution_count": 59,
"output_type": "execute_result",
"data": {
"text/plain": "-0.22012588467163624"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.random.randn(2)",
"execution_count": 60,
"outputs": [
{
"execution_count": 60,
"output_type": "execute_result",
"data": {
"text/plain": "array([-1.78002013, 0.57691838])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "N = {'mu': 3, 'var': 6.25}\nN['mu'] + np.sqrt(N['var']) * np.random.randn(2, 3) # 正規分布N の乱数の(2,3)行列",
"execution_count": 61,
"outputs": [
{
"execution_count": 61,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 2.21352905, 7.75438726, 7.75521993],\n [ 0.51149575, 5.99145576, 4.81005954]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "def ij(i, j):\n return i + j\n\nnp.fromfunction(ij, (3, 3))",
"execution_count": 62,
"outputs": [
{
"execution_count": 62,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 1., 2.],\n [ 1., 2., 3.],\n [ 2., 3., 4.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": true
},
"cell_type": "markdown",
"source": "### Printing Arrays(配列の表示)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:\n配列を表示する場合、\nNumPyはネストされたリストと似たような表示をするが、\n次のようなレイアウトになる。\n\n- the last axis is printed from left to right,\n(最後の軸は左から右へ表示される)、\n- the second-to-last is printed from top to bottom,\n(最後から二番目の軸は上から下へ表示される)、\n- the rest are also printed from top to bottom, with each slice separated from the next by an empty line.\n(残りは上から下へ、空行で区切られて表示される)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "One-dimensional arrays are then printed as rows, (1次元配列は、行(rows)のように表示され、)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(6) # 1次元配列\nprint(a)",
"execution_count": 63,
"outputs": [
{
"text": "[0 1 2 3 4 5]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "bidimensionals as matrices (2次元配列は、行列(matrix)のように表示され、)"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": false
},
"cell_type": "code",
"source": "b = np.arange(12).reshape(4, 3) # 2次元配列\nprint(b)",
"execution_count": 64,
"outputs": [
{
"text": "[[ 0 1 2]\n [ 3 4 5]\n [ 6 7 8]\n [ 9 10 11]]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "and tridimensionals as lists of matrices\n(3次元配列は、行列のリストのように表示される。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = np.arange(24).reshape(2, 3 , 4) # 3次元配列\nprint(c)",
"execution_count": 65,
"outputs": [
{
"text": "[[[ 0 1 2 3]\n [ 4 5 6 7]\n [ 8 9 10 11]]\n\n [[12 13 14 15]\n [16 17 18 19]\n [20 21 22 23]]]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "If an array is too large to be printed, \nNumPy automatically skips the central part of the array and only prints the corners(もし、配列が表示するのに大きすぎるなら、\nNumPy は自動で、配列中央部分をスキップして、端部分のみを表示する):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "print(np.arange(10000))",
"execution_count": 66,
"outputs": [
{
"text": "[ 0 1 2 ..., 9997 9998 9999]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "print(np.arange(10000).reshape(100, 100))",
"execution_count": 67,
"outputs": [
{
"text": "[[ 0 1 2 ..., 97 98 99]\n [ 100 101 102 ..., 197 198 199]\n [ 200 201 202 ..., 297 298 299]\n ..., \n [9700 9701 9702 ..., 9797 9798 9799]\n [9800 9801 9802 ..., 9897 9898 9899]\n [9900 9901 9902 ..., 9997 9998 9999]]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "To disable this behaviour and force NumPy to print the entire array, \nyou can change the printing options using `set_printoptions`.\n(この振舞いをNumPyに配列全体を表示させるために無効にするには、`set_printoptions` を用いて印字オプションを変更することができる。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=100)\nprint(np.arange(101))",
"execution_count": 68,
"outputs": [
{
"text": "[ 0 1 2 ..., 98 99 100]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=1000)\nprint(np.arange(101))",
"execution_count": 69,
"outputs": [
{
"text": "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53\n 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89\n 90 91 92 93 94 95 96 97 98 99 100]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Basic Operations(基本演算)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Arithmetic operators on arrays apply *elementwise*. \n配列に対する算術演算子は、*要素毎*に適用され、\n\nA new array is created and filled with the result.\n演算結果が入った新たな配列が作成される。"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([20, 30, 40, 50])\na",
"execution_count": 70,
"outputs": [
{
"execution_count": 70,
"output_type": "execute_result",
"data": {
"text/plain": "array([20, 30, 40, 50])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.arange(4)\nb",
"execution_count": 71,
"outputs": [
{
"execution_count": 71,
"output_type": "execute_result",
"data": {
"text/plain": "array([0, 1, 2, 3])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = a - b # 引き算\nc",
"execution_count": 72,
"outputs": [
{
"execution_count": 72,
"output_type": "execute_result",
"data": {
"text/plain": "array([20, 29, 38, 47])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b ** 2 # 二乗",
"execution_count": 73,
"outputs": [
{
"execution_count": 73,
"output_type": "execute_result",
"data": {
"text/plain": "array([0, 1, 4, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "10 * np.sin(a)",
"execution_count": 74,
"outputs": [
{
"execution_count": 74,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a < 35 # boolを返す",
"execution_count": 75,
"outputs": [
{
"execution_count": 75,
"output_type": "execute_result",
"data": {
"text/plain": "array([ True, True, False, False], dtype=bool)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Unlike in many matrix languages, \nthe product operator _*_ operates elementwise in NumPy arrays. \n(多くの行列言語とは異なり、\nNumPy の配列においては乗算演算子 _*_ は要素単位の演算を行う。)\n\nThe matrix product can be performed using the dot function or method\n(行列の積は dot 関数か method を用いて計算できる。):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "A = np.array([\n [1, 1],\n [0, 1]\n ])\nA",
"execution_count": 76,
"outputs": [
{
"execution_count": 76,
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 1],\n [0, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "B = np.array([\n [2, 0],\n [3, 4]\n ])\nB",
"execution_count": 77,
"outputs": [
{
"execution_count": 77,
"output_type": "execute_result",
"data": {
"text/plain": "array([[2, 0],\n [3, 4]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "A * B # 要素毎(elementwise)の積",
"execution_count": 78,
"outputs": [
{
"execution_count": 78,
"output_type": "execute_result",
"data": {
"text/plain": "array([[2, 0],\n [0, 4]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "A.dot(B) # 行列の積(matrix product)(method版)",
"execution_count": 79,
"outputs": [
{
"execution_count": 79,
"output_type": "execute_result",
"data": {
"text/plain": "array([[5, 4],\n [3, 4]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.dot(A, B) # another matrix product(function版)",
"execution_count": 80,
"outputs": [
{
"execution_count": 80,
"output_type": "execute_result",
"data": {
"text/plain": "array([[5, 4],\n [3, 4]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Some operations, such as `+=` and `*=`, \nact in place to modify an existing array rather than create a new one.\n(`+=`, `*=`のような演算では、新しい配列を作るというより、すでに存在する配列の内容を書き換える。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.ones((2,3), dtype=int)\na",
"execution_count": 81,
"outputs": [
{
"execution_count": 81,
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 1, 1],\n [1, 1, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a *= 3\na",
"execution_count": 82,
"outputs": [
{
"execution_count": 82,
"output_type": "execute_result",
"data": {
"text/plain": "array([[3, 3, 3],\n [3, 3, 3]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.random.random((2,3))\nb",
"execution_count": 83,
"outputs": [
{
"execution_count": 83,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0.6050557 , 0.63689238, 0.42338643],\n [ 0.51202953, 0.77875615, 0.45499617]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b += a\nb",
"execution_count": 84,
"outputs": [
{
"execution_count": 84,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 3.6050557 , 3.63689238, 3.42338643],\n [ 3.51202953, 3.77875615, 3.45499617]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a += b # b is not automatically converted to integer type(bは自動的にint型に変換されない)",
"execution_count": 85,
"outputs": [
{
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-85-1738f4ac7f17>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mb\u001b[0m \u001b[1;31m# b is not automatically converted to integer type(bは自動的にint型に変換されない)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'"
],
"ename": "TypeError",
"output_type": "error",
"evalue": "Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "When operating with arrays of different types, (異なるtypeの配列を演算する場合、\n)\n\nthe type of the resulting array corresponds to the more general or precise one (a behavior known as **upcasting**).\n(配列の演算結果のtype は、より一般的もしくはより精度の高い側に対応する。(**アップキャスティング**として知られる振舞い))"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.ones(3, dtype=np.int32)\na.dtype.name",
"execution_count": 86,
"outputs": [
{
"execution_count": 86,
"output_type": "execute_result",
"data": {
"text/plain": "'int32'"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.linspace(0, np.pi, 3)\nb",
"execution_count": 87,
"outputs": [
{
"execution_count": 87,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0. , 1.57079633, 3.14159265])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b.dtype.name",
"execution_count": 88,
"outputs": [
{
"execution_count": 88,
"output_type": "execute_result",
"data": {
"text/plain": "'float64'"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = a + b\nc",
"execution_count": 89,
"outputs": [
{
"execution_count": 89,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1. , 2.57079633, 4.14159265])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c.dtype.name",
"execution_count": 90,
"outputs": [
{
"execution_count": 90,
"output_type": "execute_result",
"data": {
"text/plain": "'float64'"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c * 1j",
"execution_count": 91,
"outputs": [
{
"execution_count": 91,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0.+1.j , 0.+2.57079633j, 0.+4.14159265j])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "d = np.exp(1 + c * 1j)\nd",
"execution_count": 92,
"outputs": [
{
"execution_count": 92,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1.46869394+2.28735529j, -2.28735529+1.46869394j,\n -1.46869394-2.28735529j])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "d.dtype",
"execution_count": 93,
"outputs": [
{
"execution_count": 93,
"output_type": "execute_result",
"data": {
"text/plain": "dtype('complex128')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the `ndarray` class.\n(配列に含まれる全要素の総和の計算など、多くの単項演算子(unary operations)は、\n`ndarray`クラスのメソッドとして実装されている。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.random((2,3))\na",
"execution_count": 94,
"outputs": [
{
"execution_count": 94,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0.11597674, 0.63910212, 0.17833459],\n [ 0.35632003, 0.69038429, 0.59595888]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.sum() # 合計",
"execution_count": 95,
"outputs": [
{
"execution_count": 95,
"output_type": "execute_result",
"data": {
"text/plain": "2.5760766509773951"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.min() # 最小値",
"execution_count": 96,
"outputs": [
{
"execution_count": 96,
"output_type": "execute_result",
"data": {
"text/plain": "0.11597673870747105"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a.max() # 最大値",
"execution_count": 97,
"outputs": [
{
"execution_count": 97,
"output_type": "execute_result",
"data": {
"text/plain": "0.69038429053268313"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. \nデフォルトで、これらの演算は、配列に対し、配列のshape に関わらず、\n数値のリストであるかのように適用される。\n\nHowever, by specifying the axis parameter you can apply an operation along the specified axis of an array(しかし、軸パラメータを指定することで、配列の特定の軸に沿って演算を適用することできる):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.arange(12).reshape(3,4)\nb",
"execution_count": 98,
"outputs": [
{
"execution_count": 98,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0, 1, 2, 3],\n [ 4, 5, 6, 7],\n [ 8, 9, 10, 11]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "b.sum(axis=0) # sum of each column(各列の合計)",
"execution_count": 99,
"outputs": [
{
"execution_count": 99,
"output_type": "execute_result",
"data": {
"text/plain": "array([12, 15, 18, 21])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "b.sum(axis=1) # min of each row(各行の最小値)",
"execution_count": 100,
"outputs": [
{
"execution_count": 100,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 6, 22, 38])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "b.min(axis=1) # (各業に沿った累積和)",
"execution_count": 101,
"outputs": [
{
"execution_count": 101,
"output_type": "execute_result",
"data": {
"text/plain": "array([0, 4, 8])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "b.cumsum(axis=1) # cumulative sum along each row",
"execution_count": 102,
"outputs": [
{
"execution_count": 102,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0, 1, 3, 6],\n [ 4, 9, 15, 22],\n [ 8, 17, 27, 38]], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Universal Functions(ユニバーサル関数)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "NumPy provides familiar mathematical functions such as `sin`, `cos`, and `exp`. \n(NumPyはsin, cos, exp のようなおなじみの数学関数も用意している。)\n\nIn NumPy, these are called **“universal functions”(ufunc)**.\n(NumPyでは、これらは「ユニバーサル関数(ufunc)」と呼ばれる。)\n\nWithin NumPy, these functions operate elementwise on an array, producing an array as output.\n(NumPyの中では、これらの関数は配列の要素毎に演算を行い、\n出力として新しい配列を生成する。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "B = np.arange(3)\nB",
"execution_count": 103,
"outputs": [
{
"execution_count": 103,
"output_type": "execute_result",
"data": {
"text/plain": "array([0, 1, 2])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.exp(B)",
"execution_count": 104,
"outputs": [
{
"execution_count": 104,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1. , 2.71828183, 7.3890561 ])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "np.sqrt(B)",
"execution_count": 105,
"outputs": [
{
"execution_count": 105,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0. , 1. , 1.41421356])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "C = np.array([2., -1., 4.])\nnp.add(B, C)",
"execution_count": 106,
"outputs": [
{
"execution_count": 106,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 2., 0., 6.])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "See also\n\n- [all (配列の要素が全て真のとき真を返す.)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.all.html#numpy.all)\n- [any (配列の要素が一つでも真のとき真を返す.)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.any.html#numpy.any)\n- [apply_along_axis(ある軸に対して関数を適用する)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.apply_along_axis.html#numpy.apply_along_axis)\n- [argmax(最大値となるindexを返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.argmax.html#numpy.argmax)\n- [argmin (最小値となるindexを返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.argmin.html#numpy.argmin)\n- [argsort(ソート後の配列のindexを返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.argsort.html#numpy.argsort)\n- [average (重みづけ平均)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.average.html#numpy.average)\n- [bincount(ヒストグラムに使うようなbinをカウント(頻度))](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.bincount.html#numpy.bincount)\n- [ceil (切り上げ)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ceil.html#numpy.ceil)\n- [clip](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.clip.html#numpy.clip)\n- [conj](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.conj.html#numpy.conj)\n- [corrcoef](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.corrcoef.html#numpy.corrcoef)\n- [cov](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.cov.html#numpy.cov)\n- [cross](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.cross.html#numpy.cross)\n- [cumprod](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.cumprod.html#numpy.cumprod)\n- [cumsum](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.cumsum.html#numpy.cumsum)\n- [diff](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.diff.html#numpy.diff)\n- [dot](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.dot.html#numpy.dot)\n- [floor(切り捨て)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.floor.html#numpy.floor)\n- [inner](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.inner.html#numpy.inner)\n- [lexsort(value とkey値を考慮したソート)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.lexsort.html#numpy.lexsort)\n- [max](http://docs.python.org/dev/library/functions.html#max)\n- [maximum](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.maximum.html#numpy.maximum)\n- [mean](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.mean.html#numpy.mean)\n- [median](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.median.html#numpy.median)\n- [min](http://docs.python.org/dev/library/functions.html#min)\n- [minimum (配列要素から要素の最小値(リスト)を返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.minimum.html#numpy.minimum)\n- [nonzero (zeroでない要素のindex配列を返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.nonzero.html#numpy.nonzero)\n- [outer](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.outer.html#numpy.outer)\n- [prod](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.prod.html#numpy.prod)\n- [re](http://docs.python.org/dev/library/re.html#module-re)\n- [round](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.round_.html)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [sort (クイック・マージ・ヒープの中から選べる)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.sort.html#numpy.sort)\n\n```\nnp.sort(kind='quicksort')\n```"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [std](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.std.html#numpy.std)\n- [sum](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.sum.html#numpy.sum)\n- [trace(行列のトレース(対角成分の和))](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.trace.html#numpy.trace)\n- [transpose (転置行列)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.transpose.html#numpy.transpose)\n- [var (分散を計算)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.var.html#numpy.var)\n- [vdot](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vdot.html#numpy.vdot)\n- [vectorize](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vectorize.html#numpy.vectorize)\n- [where](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.where.html#numpy.where)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Indexing, Slicing and Iterating(インデックス、スライス、イテレート)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### One-dimensional"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**One-dimensional** arrays can be indexed, \nsliced and iterated over, \nmuch like lists and other Python sequences.\n(** 1次元配列 ** は、リストや他のPythonシーケンスとほぼ同様、indexでアクセスしたり、スライスしたり、イテレートすることができる。)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(10) ** 3\na",
"execution_count": 107,
"outputs": [
{
"execution_count": 107,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a[2] # index",
"execution_count": 108,
"outputs": [
{
"execution_count": 108,
"output_type": "execute_result",
"data": {
"text/plain": "8"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a[2:5] # slice",
"execution_count": 109,
"outputs": [
{
"execution_count": 109,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 8, 27, 64], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000\na",
"execution_count": 110,
"outputs": [
{
"execution_count": 110,
"output_type": "execute_result",
"data": {
"text/plain": "array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a[::-1] # reversed a(iterate)",
"execution_count": 111,
"outputs": [
{
"execution_count": 111,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "for v in a: # iterate\n o = (v ** (1 / 3.))\n print(o)\n print(type(o))",
"execution_count": 112,
"outputs": [
{
"text": "nan\n<class 'numpy.float64'>\n1.0\n<class 'numpy.float64'>\nnan\n<class 'numpy.float64'>\n3.0\n<class 'numpy.float64'>\nnan\n<class 'numpy.float64'>\n5.0\n<class 'numpy.float64'>\n6.0\n<class 'numpy.float64'>\n7.0\n<class 'numpy.float64'>\n8.0\n<class 'numpy.float64'>\n9.0\n<class 'numpy.float64'>\n",
"output_type": "stream",
"name": "stdout"
},
{
"text": "C:\\Miniconda3\\lib\\site-packages\\ipykernel\\__main__.py:2: RuntimeWarning: invalid value encountered in power\n from ipykernel import kernelapp as app\n",
"output_type": "stream",
"name": "stderr"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Multidimensional"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**Multidimensional** arrays can have one index per axis. \n多次元配列は軸ごとにindex を1つ持つ。\n\nThese indices are given in a tuple separated by commas(これらのindexは、カンマ区切りのタプルで与えられる):"
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "def f(i, j):\n return 10 * i + j",
"execution_count": 113,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b = np.fromfunction(f, (5, 4), dtype=int)\nb",
"execution_count": 114,
"outputs": [
{
"execution_count": 114,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0, 1, 2, 3],\n [10, 11, 12, 13],\n [20, 21, 22, 23],\n [30, 31, 32, 33],\n [40, 41, 42, 43]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b[2, 3] # index",
"execution_count": 115,
"outputs": [
{
"execution_count": 115,
"output_type": "execute_result",
"data": {
"text/plain": "23"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b[0:5, 1] # slice, each row in the second column of b",
"execution_count": 116,
"outputs": [
{
"execution_count": 116,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1, 11, 21, 31, 41])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b[ : , 1] # equivalent to the previous example 前の行と同じ",
"execution_count": 117,
"outputs": [
{
"execution_count": 117,
"output_type": "execute_result",
"data": {
"text/plain": "array([ 1, 11, 21, 31, 41])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b[1:3, : ] # each column in the second and third row of b",
"execution_count": 118,
"outputs": [
{
"execution_count": 118,
"output_type": "execute_result",
"data": {
"text/plain": "array([[10, 11, 12, 13],\n [20, 21, 22, 23]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "When fewer indices are provided than the number of axes, \nthe missing indices are considered complete slices:\n(与えられたindex の数が軸数より少ない場合、足りないindexは、完全なスライスと見なされる)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "b[-1] # == b[-1, : ], the last row.",
"execution_count": 119,
"outputs": [
{
"execution_count": 119,
"output_type": "execute_result",
"data": {
"text/plain": "array([40, 41, 42, 43])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The expression within brackets in `b[i]` is treated as an i followed by as many instances of `:` as needed to represent the remaining axes. \n(`b[i]` のブラケット(大かっこ)の中の式は、i の後に残りの軸を表現するのに必要な分の `:` が続いているものとして扱われる。)\n\nNumPy also allows you to write this using dots as `b[i,...]`.\n(NumPyではドットを用いてこれを `b[i,...]` と書いてもいい。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### dots "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The **dots (`...`)** represent as many colons as needed to produce a complete indexing tuple. \n(ドット (...) は完全な添字タプルを生成するために必要な分のコロンを表す。)\n\nFor example, if `x` is a rank 5 array (i.e., it has 5 axes), then\n(例えば、もし、`x` がrank 5の配列(即ち、軸が5つ)であるとするなら)、\n\n- `x[1, 2, ...]` is equivalent to `x[1, 2, :, :, :]`,\n\n- `x[..., 3]` to `x[:, :, :, :, 3]` and\n\n- `x[4, ..., 5, :]` to `x[4, :, :, 5, :]`."
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c = np.array( [[[ 0, 1, 2], # a 3D array (two stacked 2D arrays) shape(2,2,3)\n [ 10, 12, 13]],\n [[100,101,102],\n [110,112,113]]])\nc.shape",
"execution_count": 120,
"outputs": [
{
"execution_count": 120,
"output_type": "execute_result",
"data": {
"text/plain": "(2, 2, 3)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c[1, ...] # same as c[1,:,:] or c[1]",
"execution_count": 121,
"outputs": [
{
"execution_count": 121,
"output_type": "execute_result",
"data": {
"text/plain": "array([[100, 101, 102],\n [110, 112, 113]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "c[..., 2] # same as c[:,:,2]",
"execution_count": 122,
"outputs": [
{
"execution_count": 122,
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 2, 13],\n [102, 113]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Iterating "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Iterating over multidimensional arrays is done with respect to the first axis\n(多次元配列のイテレートは最初の軸に関して行われる):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "for row in b: # iterate\n print(row)",
"execution_count": 123,
"outputs": [
{
"text": "[0 1 2 3]\n[10 11 12 13]\n[20 21 22 23]\n[30 31 32 33]\n[40 41 42 43]\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "However, if one wants to perform an operation on each element in the array, \none can use the `flat` attribute which is an iterator over all the elements of the array\n(しかし、配列の各要素についてある演算を行いたい場合には、\n配列の全要素を渡り歩くイテレータであるflat属性が使える):"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "for element in b.flat:\n print(element)",
"execution_count": 124,
"outputs": [
{
"text": "0\n1\n2\n3\n10\n11\n12\n13\n20\n21\n22\n23\n30\n31\n32\n33\n40\n41\n42\n43\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### 横(列)ベクトルと縦(行)ベクトル"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "`np.ndarray` の1次元と2次元の配列と,数学の概念であるベクトルと行列との関係についての補足。\n線形代数では,縦(行)ベクトルや横(列)ベクトルという区別があるが、\n1次元の `np.ndarray` 配列にはそのような区別はない。 \n\nそのため,**1次元配列を転置することができず、数学でいうところのベクトルとは厳密には異なる**。"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "col = np.array([1, 2, 3, 4, 5])\ncol",
"execution_count": 125,
"outputs": [
{
"execution_count": 125,
"output_type": "execute_result",
"data": {
"text/plain": "array([1, 2, 3, 4, 5])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "col.T # 転置されない",
"execution_count": 126,
"outputs": [
{
"execution_count": 126,
"output_type": "execute_result",
"data": {
"text/plain": "array([1, 2, 3, 4, 5])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "行ベクトルや列ベクトルを区別して表現するには、\n行数が1である2次元配列(リストが2重)を用いる。\n縦ベクトルは次のようになり:"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "col = np.array([[1, 2, 3, 4, 5]]) # 列ベクトル\ncol",
"execution_count": 127,
"outputs": [
{
"execution_count": 127,
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2, 3, 4, 5]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "col.T # 転置すると行ベクトルになる",
"execution_count": 128,
"outputs": [
{
"execution_count": 128,
"output_type": "execute_result",
"data": {
"text/plain": "array([[1],\n [2],\n [3],\n [4],\n [5]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "See also:\n\n- [Indexing](https://docs.scipy.org/doc/numpy-dev/user/basics.indexing.html#basics-indexing)\n- [Inedexing(arrays)](https://docs.scipy.org/doc/numpy-dev/reference/arrays.indexing.html#arrays-indexing)\n- [newaxis](https://docs.scipy.org/doc/numpy-dev/reference/arrays.indexing.html#numpy.newaxis)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [ndenumerate(多次元indexのイテレータを返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ndenumerate.html#numpy.ndenumerate)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([\n [1, 2], \n [3, 4]\n ])\n\nfor index, v in np.ndenumerate(a):\n print(index, v)",
"execution_count": 129,
"outputs": [
{
"text": "(0, 0) 1\n(0, 1) 2\n(1, 0) 3\n(1, 1) 4\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [indices (指定した次元分(grid)のindexを表す配列を返す)](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.indices.html#numpy.indices)"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "grid = np.indices((2, 3))\ngrid.shape",
"execution_count": 130,
"outputs": [
{
"execution_count": 130,
"output_type": "execute_result",
"data": {
"text/plain": "(2, 2, 3)"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "grid",
"execution_count": 131,
"outputs": [
{
"execution_count": 131,
"output_type": "execute_result",
"data": {
"text/plain": "array([[[0, 0, 0],\n [1, 1, 1]],\n\n [[0, 1, 2],\n [0, 1, 2]]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "grid[0] # row indices",
"execution_count": 132,
"outputs": [
{
"execution_count": 132,
"output_type": "execute_result",
"data": {
"text/plain": "array([[0, 0, 0],\n [1, 1, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "grid[1] # column indices",
"execution_count": 133,
"outputs": [
{
"execution_count": 133,
"output_type": "execute_result",
"data": {
"text/plain": "array([[0, 1, 2],\n [0, 1, 2]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## randomクラスで使っているアルゴリズム"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.random.set_state.html"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "print(np.random.get_state())",
"execution_count": 134,
"outputs": [
{
"text": "('MT19937', array([ 142035591, 2992053589, 1436206245, 2075584926, 1153473185,\n 2023702678, 164400302, 1487550030, 2500935688, 398450423,\n 2241243447, 3053850279, 992651537, 703626941, 184754581,\n 896494976, 1383230003, 2643832555, 3134902701, 1111739025,\n 2920121130, 623667065, 2369835178, 1833405942, 2809490491,\n 3070134211, 1141359642, 2096071536, 2435951546, 3145324780,\n 3116839452, 958673918, 3956765171, 1987654081, 2421401012,\n 3770752022, 3728506922, 647936218, 1368833425, 1170918621,\n 256711212, 565537722, 47683647, 902979579, 4236163335,\n 2979348143, 586206835, 2089696176, 896873745, 791857444,\n 2015898658, 2804859506, 3046071113, 1269391123, 31875726,\n 286279086, 651550258, 3547868609, 3735096567, 2444283478,\n 2711698963, 3033889294, 3454923815, 2983951799, 2195132295,\n 1178883285, 4024891227, 3152655913, 1958647164, 4257009190,\n 4025369694, 3828126802, 1873509363, 1720662462, 2212066546,\n 455762541, 3793163378, 644909654, 559814422, 2981159863,\n 2661623634, 3535510456, 844281027, 490535964, 1081261424,\n 3359141009, 938517146, 3356810585, 130787175, 599435927,\n 3092043225, 4241934958, 1016131312, 2927980932, 2010138871,\n 2518721969, 4094107020, 610754650, 3954153848, 159930682,\n 2987042834, 3721426071, 2645092560, 1355115469, 3413014711,\n 1944376166, 3461189383, 3917334290, 74695030, 2850087777,\n 707777319, 2516827815, 3224771949, 1141485812, 3171420957,\n 672485961, 459879657, 912596249, 663957204, 5918518,\n 1103621813, 501944213, 1631096881, 3122860076, 2278630995,\n 1209874428, 3744380314, 805023017, 1925639328, 736276791,\n 1947857217, 1882766030, 4257232545, 2499697969, 3285125239,\n 1885861871, 2961227433, 3239844188, 476830246, 357690197,\n 2092642040, 113119071, 2200290090, 764879910, 670953681,\n 1059284608, 1680368602, 136045864, 520938361, 1763018900,\n 3883730164, 3079424491, 755438962, 996289528, 3317809914,\n 3710175121, 287431291, 1664427713, 2096871603, 2197536088,\n 3105003241, 2090557734, 966646221, 2569763368, 1545600799,\n 3857029860, 656123340, 3847674313, 2214159386, 3857745392,\n 2245805411, 1462515455, 2163503196, 2857156793, 3621005311,\n 4256842092, 1155733147, 1125803240, 1767579638, 68928065,\n 1671689657, 1997372333, 498924339, 3576617021, 1013107959,\n 2827611360, 2794339670, 209571897, 1338550303, 361512031,\n 3754572, 652743540, 3519093460, 739889437, 1111465341,\n 3251485047, 362148944, 2206196524, 985862922, 3929751624,\n 3578420643, 1146794489, 843188314, 2610325699, 514280443,\n 3291531068, 3794394289, 2122818980, 369436833, 1951135533,\n 739985190, 441883207, 1389631440, 1350924293, 1546985811,\n 3248905716, 3468017355, 1569406733, 3835309206, 2912654368,\n 1528722370, 2827965180, 3520486755, 1248565327, 3125477371,\n 413493895, 3163916452, 725296147, 3569812165, 3594060977,\n 223769852, 4016913123, 199649659, 2387872526, 2232099730,\n 1017629739, 3763043326, 553275367, 1776501529, 1337103914,\n 3611725888, 1816757911, 3990050011, 1284084626, 3960383656,\n 829247461, 884246041, 4099054010, 2147958793, 2790310388,\n 3725027202, 2047836473, 2699620237, 1625135648, 3480810772,\n 507563469, 3320881602, 3531411735, 1912341960, 1635437378,\n 3675849063, 3792767455, 53301134, 1576145667, 2928897288,\n 1619873387, 1134829289, 2937405113, 2990424722, 960044927,\n 441606876, 2440907884, 1538411584, 2055481117, 4124939760,\n 3930986978, 346900338, 3119371365, 4226580917, 2035090841,\n 1209401160, 2987739125, 4219373111, 2563890038, 3453779645,\n 2354575556, 2685667126, 3282193026, 1370359443, 729245177,\n 1449464241, 2176356266, 3547819587, 497243031, 2240013424,\n 3111489103, 3637367624, 4219434555, 3327925904, 2110342492,\n 1574503668, 3516032106, 2365786000, 2136289405, 4220226430,\n 3767276806, 699927949, 1009560737, 1498612251, 183140526,\n 2313403360, 2609148251, 1562475327, 1136309454, 2507993653,\n 2721628382, 539775799, 2608168597, 107664588, 91058698,\n 2966320812, 1268597914, 2767767069, 2322258733, 255361300,\n 336973049, 611589367, 319867770, 3501940199, 2917296194,\n 2611612347, 3410720766, 2461276906, 2374375340, 3297874812,\n 46968096, 3156597219, 2301873843, 1215765093, 1565322310,\n 4231408440, 1236025328, 3998002477, 3216345736, 2579061465,\n 133375714, 3283182695, 3897317582, 1419982699, 820452103,\n 4199742771, 286486733, 3515446310, 1765780165, 595913573,\n 1591261177, 2328334743, 3970052466, 1516808160, 4016541804,\n 3666318903, 2407791524, 1523542227, 2370124516, 1747425004,\n 1537426418, 407514393, 211538908, 3875078012, 3653001925,\n 451251581, 3352305342, 2651370878, 3226077856, 1230576020,\n 1099768802, 3259751470, 1970069530, 509019668, 3366323700,\n 2926137302, 3981858952, 4021659285, 619849168, 4215963354,\n 2984258796, 3851392388, 3158425985, 3560817656, 2250106785,\n 3206971482, 1655938903, 273422088, 3506736879, 3691541393,\n 3747164664, 1284273582, 2810760774, 3130282239, 3521838548,\n 3479537299, 1729692646, 786506497, 1366221618, 3858955132,\n 1343494639, 1546780048, 2686682139, 678294543, 1175681977,\n 4187815499, 4044112559, 3800305097, 485424669, 968893753,\n 3932571578, 4020402031, 1002653103, 2658597689, 1101714886,\n 894430014, 3384807671, 181496754, 600008401, 389772502,\n 4123780972, 100565123, 250056669, 2083544624, 295455779,\n 577114333, 4265480163, 2448689604, 2185555125, 1542098544,\n 1607705911, 3347715220, 2868801558, 312467210, 2807484054,\n 3257776610, 3115662801, 4063024529, 3798742742, 1222666812,\n 2940413477, 935910877, 3136225329, 922962398, 151534858,\n 1446714361, 2180239133, 1024393977, 284862041, 3724636482,\n 1486941761, 3427128826, 3660018510, 1503210988, 4109626410,\n 728386886, 1398943888, 718286078, 4208402410, 1189727972,\n 790637034, 514415175, 2534820247, 1123228716, 23001759,\n 4082188812, 2392061683, 1431433791, 1409961516, 1948716618,\n 4223370697, 2129109461, 2407060028, 4056760945, 2287892234,\n 4189456823, 2575306936, 2285330651, 3762811416, 1918613495,\n 381829954, 680576880, 3679272033, 1420265841, 654426530,\n 2143017384, 1500689788, 3578824987, 535425024, 376400650,\n 1380266383, 1667345437, 1631624298, 2952264329, 3123649135,\n 142551326, 594501007, 300344446, 3481166875, 236327910,\n 1242977005, 1505960258, 2423519855, 2274266816, 645961304,\n 380544684, 399849397, 3258669357, 1925352863, 2962881697,\n 3851387278, 2592270930, 37628778, 2076754228, 3872413202,\n 3281442657, 3507194069, 3061024331, 3485217810, 1762507053,\n 3238625115, 775753689, 3957571808, 2635345357, 2226946715,\n 2512941414, 3440087588, 19857156, 577436620, 1818870068,\n 4085000082, 590010247, 1636648937, 718782902, 3653737921,\n 3938746433, 3027456748, 324088840, 3177565598, 4277326312,\n 1547345075, 2757352137, 795386085, 2498226824, 1371257679,\n 692346877, 3700231927, 4130090397, 3748672208, 2056006452,\n 1479361294, 1971091411, 2738837275, 1250147263, 3988554168,\n 1735000973, 2245087787, 207499281, 1405366959, 2633088501,\n 4134913239, 1735215863, 781386546, 4069333371, 2504283036,\n 2093545768, 3858025633, 4055885329, 1048810190, 3472525613,\n 2026515774, 387219462, 3127423286, 2387070541, 2151227874,\n 612749484, 3196725532, 2605738273, 3641366231, 1173524352,\n 1029028169, 3975451221, 240705905, 2516290939, 329644749,\n 3518899331, 1323774035, 3476585380, 86964699, 1934246442,\n 952099776, 670385401, 3401150768, 302066928, 842978659,\n 2764670271, 2937450353, 2761196917, 3018308041, 1886997152,\n 147548092, 1486528689, 191796765, 401876355, 3285745255,\n 2649150337, 3560516779, 1659943257, 2444357490, 1477501373,\n 2160692004, 1268424486, 2884722778, 3729914941, 2086156973,\n 2372656643, 3234926284, 1542964012, 2445238322], dtype=uint32), 96, 1, -1.5159457205911733)\n",
"output_type": "stream",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## 参考リンク"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [QuickStartTutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)\n- [私訳「暫定的 NumPy チュートリアル」](http://naoyat.hatenablog.jp/entry/2011/12/29/021414)\n- [機械学習の Python との出会い](http://www.kamishima.net/mlmpyja/)"
}
],
"metadata": {
"language_info": {
"file_extension": ".py",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.1",
"mimetype": "text/x-python",
"name": "python"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"toc": {
"toc_cell": true,
"toc_threshold": "6",
"toc_number_sections": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "NumPy Tutorial メモ1 (Basic)",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment