Skip to content

Instantly share code, notes, and snippets.

@3kazu8
Last active June 4, 2019 21:35
Show Gist options
  • Save 3kazu8/64504b221bb159fbc6a44f698f5addb2 to your computer and use it in GitHub Desktop.
Save 3kazu8/64504b221bb159fbc6a44f698f5addb2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# codExa - Numpy 入門"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"このコースがカバーしている内容\n",
"\n",
"- Numpy配列の基本的な作り方\n",
"- Numpy配列の要素のアクセスの仕方\n",
"- Numpy配列生成の便利な関数の使い方\n",
"- Numpy配列と基本的な演算子\n",
"- Numpy配列とPython Listの簡単な比較\n",
"- 行列の基本的な演算(ドット積、アダマール積)\n",
"- 行列の処理(逆行列、転置行列、対角成分の取得、内積/外積)\n",
"- 数学系の関数(三角関数、指数/対数関数、小数の扱い)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Numpyとは?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター3では、Numpyの基本的な概要をみていきましょう。実際にiPythonを立ち上げて、Numpyのインポートとバージョン確認を行います。\n",
"\n",
"- Numpyは数値計算を効率的に行うためのPython拡張ライブラリ\n",
"- 多次元配列の操作/演算を行うのに便利\n",
"- 機械学習では必須のライブラリです\n",
"- 全ての操作を覚える必要はありません\n",
"- 基本的な操作のみ覚えて必要に応じて公式ドキュメントを利用"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.15.1\n"
]
}
],
"source": [
"import numpy as np\n",
"print(np.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Numpy配列"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター4「Numpy 配列」では、Numpy配列の生成や、データのアクセスの方法を学びましょう。またNumpy配列生成で覚えておくと便利な関数も紹介しています。\n",
"\n",
"- Numpy配列の生成はnp.array()\n",
"- typeはnumpy.ndarray\n",
"- shapeで配列のサイズを確認\n",
"- array[0]で0行目の要素のアクセス\n",
"- array[2,3」で2行目3列目の要素のアクセス\n",
"- array[:2]で2行までアクセス\n",
"- array[2:]で行2からアクセス\n",
"- array[:2, 1]行2までの列1へアクセス\n",
"- np.zeros() 要素が全て0のNumpy配列の生成\n",
"- np.ones() 要素が全て1のNumpy配列の生成\n",
"- np.full() 指定した値を持つNumpy配列の生成\n",
"- np.eye() 単位行列のNumpy配列を生成"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列の作成\n",
"a = np.array([1,2,3])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# タイプの確認\n",
"type(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ndarray = N Dimension(次元) Array(配列) … N次元の配列"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3,)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列のサイズの確認\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 要素のデータ型の確認/指定\n",
"a.dtype"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列aの0番目の要素へアクセス\n",
"a[0]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[1]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[2]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新しい配列bの作成\n",
"b = np.array([[1,2,3],[4,5,6]])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.shape"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.dtype"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列bの0行目の要素\n",
"b[0]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列bの1行目の要素\n",
"b[1]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列bの0行目0列目の要素\n",
"b[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列bの0行目0列目の要素\n",
"b[0,0]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1,2]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8],\n",
" [ 9, 10, 11, 12]])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新しい配列cの作成\n",
"c = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.shape"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4],\n",
" [5, 6, 7, 8]])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列cの2行目以前の要素(行範囲指定)\n",
"c[:2]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 9, 10, 11, 12]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列cの2行目以降の要素(行範囲指定)\n",
"c[2:]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 6])"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 配列cの2行1列の要素\n",
"c[:2, 1]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8],\n",
" [ 9, 10, 11, 12]])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.]])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 0を要素とする配列を生成\n",
"np.zeros((3,3))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1.],\n",
" [1., 1.]])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1を要素とする配列を生成\n",
"np.ones((2,2))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[10, 10, 10, 10, 10],\n",
" [10, 10, 10, 10, 10],\n",
" [10, 10, 10, 10, 10]])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 指定した値とサイズの配列を生成\n",
"np.full((3,5),10)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 単位行列を生成\n",
"np.eye(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. 基本的な演算"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター5「基本的な演算」では、Numpy配列と基本的な演算子「+」や「-」を使って、どのような処理が行われるかを確認しましょう。またPythonのリストとNumpy配列の異なる点にも触れています。\n",
"\n",
"- array + arrayは各要素の足し算\n",
"- array - arrayは各要素の引き算\n",
"- array * arrayは各要素の掛け算\n",
"- array ** 2は各要素の自乗\n",
"- array / 2は各要素の2の割り算\n",
"- array < 2は各要素が2より小さいかどうかのboolean\n",
"- PythonリストとNumpy配列の処理の違い"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[1,2,3],[4,5,6]])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([[1,2,3],[4,5,6]])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 4, 6],\n",
" [ 8, 10, 12]])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 足し算\n",
"a + b"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0],\n",
" [0, 0, 0]])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 引き算\n",
"a - b"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 4, 6],\n",
" [ 8, 10, 12]])"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 掛け算\n",
"a * 2"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 4, 9],\n",
" [16, 25, 36]])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 各要素に対して自乗\n",
"a ** 2"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.5, 1. , 1.5],\n",
" [2. , 2.5, 3. ]])"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 割り算\n",
"a / 2"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, False, False],\n",
" [False, False, False]])"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 各要素に対して2より小さいかBooleanで判別\n",
"a < 2"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"# PythonリストとNumpy配列の処理の違い\n",
"array = np.array([1,2,3])\n",
"list = [1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リストに値を追加\n",
"list + [4]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([5, 6, 7])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# arrayに値を追加\n",
"array + [4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"→ 各要素毎に4が足し算される"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6, 7]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リストの各要素に追加する方法 → for文を使う\n",
"list2 = []\n",
"for e in list:\n",
" list2.append(e + 4)\n",
"list2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pythonのリストに対して、Numpy配列の方がfor文を使わずに簡単な構文で計算処理ができる。"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 1, 2, 3]"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 要素毎の掛け算\n",
"list * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pythonのリストは、リストの同じ値が追加される"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy配列は、各要素に対して掛け算される"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6]"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リストの各要素に対して掛け算をする方法\n",
"list2 = []\n",
"for e in list:\n",
" list2.append(e*2)\n",
"list2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. 行列の積"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター6「行列の積」では、Numpyを使って行列(マトリックス)の積を求めましょう。またPythonリストからNumpy配列への変換も説明します。行列の積の求め方や線形代数の基礎に不安がある方は、線形代数 入門の講座も是非受講ください。\n",
"\n",
"- 行列の積の計算方法\n",
"- ndarray * ndarray は各要素の掛け算\n",
"- np.dot()で行列の積を求められる\n",
"- a.dot(b) = np.dot(a,b)\n",
"- np.dot(a,b)とnp.dot(b,a)の解は異なる場合がある\n",
"- np.array(python-list)でPythonリストをNumpy配列へ変換\n",
"- np.ndarray.tolist()でNumpy配列からPythonリストへ変換"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[1,2],[3,4]])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[6, 7],\n",
" [8, 9]])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([[6,7],[8,9]])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 6, 14],\n",
" [24, 36]])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# numpy行列の掛け算(行列a * 行列b)\n",
"a * b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"各要素毎の掛け算(アダマール積)になる"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[22, 25],\n",
" [50, 57]])"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 通常の行列の積\n",
"np.dot(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A x B ≠ B x A → 解が同一とは限らない"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[27, 40],\n",
" [35, 52]])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.dot(b, a)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[22, 25],\n",
" [50, 57]])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[27, 40],\n",
" [35, 52]])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.dot(a)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2], [3, 4]]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# PythonリストからNumpy配列へ変換方法\n",
"c = [[1,2],[3,4]]\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(c)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.array(c)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(c)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3, 4, 5], [2, 3]]"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = [[1,2,3,4,5],[2,3]]\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(d)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([list([1, 2, 3, 4, 5]), list([2, 3])], dtype=object)"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = np.array(d)\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(d)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2], [3, 4]]"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Numpy配列からリストに変換\n",
"c = c.tolist()\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. 乱数生成"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター7「乱数生成」では、Numpyを様々な種類の乱数生成を行います。また基本的な乱数の種類を説明します。分布や基本的な統計用語に不安がある方は、統計 入門 前編の講座も是非受講ください。\n",
"\n",
"- 乱数とはランダムな数値のこと\n",
"- 値や分布に応じて種類が分かれている\n",
"- 一様乱数は0〜1の一様分布の乱数\n",
"- 標準正規分布は標準偏差を1、平均値が0の分布の乱数\n",
"- np.random.rand() 一様乱数の生成\n",
"- np.random.randn() 標準正規分布の乱数生成\n",
"- np.random.randint() 整数の乱数生成\n",
"- np.random.seed() 乱数の種(シード)の指定"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.64429942, 0.08410252, 0.44132879, 0.9965415 , 0.38564625,\n",
" 0.78607999, 0.66841587, 0.34154764, 0.30820283, 0.76351899])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 一様乱数 (0 - 1 の一様分布)\n",
"np.random.rand(10)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.68596931, 0.7329969 , 0.34187383, 0.62919599, 0.18701337,\n",
" 0.28053138, 0.42958595, 0.15361446, 0.83551952, 0.73346555],\n",
" [0.70217503, 0.59122422, 0.19405769, 0.53352816, 0.63163549,\n",
" 0.35772648, 0.44990012, 0.84809652, 0.16822018, 0.20827498],\n",
" [0.04957431, 0.93979935, 0.69779295, 0.35574373, 0.78083765,\n",
" 0.80000915, 0.49188873, 0.94338823, 0.37007049, 0.98743581],\n",
" [0.24057157, 0.22767824, 0.06132668, 0.86331089, 0.11389848,\n",
" 0.93891793, 0.24190703, 0.49923403, 0.29573072, 0.96923687],\n",
" [0.86792911, 0.02542416, 0.43191579, 0.0299004 , 0.42334695,\n",
" 0.10055232, 0.3645184 , 0.20666588, 0.59122563, 0.83036306],\n",
" [0.2629109 , 0.12490734, 0.39961616, 0.41488037, 0.31148159,\n",
" 0.54200951, 0.87629852, 0.61116409, 0.84112978, 0.56510964],\n",
" [0.62548336, 0.2082473 , 0.13101559, 0.48733085, 0.69277673,\n",
" 0.55748893, 0.53848885, 0.69247538, 0.41603632, 0.07231138],\n",
" [0.38683013, 0.44412988, 0.26367839, 0.31839903, 0.75317197,\n",
" 0.68735094, 0.38097885, 0.64144949, 0.76436488, 0.81690692],\n",
" [0.51507236, 0.31275952, 0.01099007, 0.02988375, 0.10370674,\n",
" 0.28795816, 0.69628754, 0.36235947, 0.02154381, 0.79708329],\n",
" [0.2933661 , 0.51074854, 0.57796772, 0.9289292 , 0.00977707,\n",
" 0.18020133, 0.71194819, 0.53897919, 0.52643007, 0.77101125]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.rand(10, 10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.00118311])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 標準正規分布(Standard Normal Distribution) \n",
"# 平均値 = 0 & 標準偏差 = 1\n",
"# 平均値(Mean) / 中央値(Median)\n",
"np.random.rand(1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.53646749, 0.07247673, 0.29478375, ..., 0.03026731, 0.14167959,\n",
" 0.20154307],\n",
" [0.45999956, 0.67430069, 0.54125576, ..., 0.44881399, 0.61067608,\n",
" 0.27569965],\n",
" [0.59099534, 0.79278391, 0.03250473, ..., 0.14785069, 0.28648595,\n",
" 0.55266365],\n",
" ...,\n",
" [0.77118998, 0.16422199, 0.32952605, ..., 0.03422843, 0.99281963,\n",
" 0.45203582],\n",
" [0.7104178 , 0.54111323, 0.81364574, ..., 0.61618342, 0.4904523 ,\n",
" 0.38321151],\n",
" [0.64430953, 0.42188679, 0.46482228, ..., 0.30527422, 0.24844327,\n",
" 0.06296547]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"randn5000 = np.random.rand(5000, 5000)\n",
"randn5000"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5000, 5000)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"randn5000.shape"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.49997501551830226"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"randn5000.mean()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.2887417348263192"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"randn5000.std()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"69"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 整数乱数の生成\n",
"np.random.randint(100)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 7, 15, 16, 22, 3, 29, 26, 27, 9, 3])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1 〜 30 までの50故の乱数を生成\n",
"np.random.randint(1,30,10)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[17, 28, 6, 23, 20, 11, 28, 26, 13, 4],\n",
" [29, 17, 4, 11, 27, 13, 22, 12, 13, 27],\n",
" [ 4, 15, 21, 1, 11, 9, 13, 10, 6, 5],\n",
" [11, 21, 24, 14, 22, 2, 2, 2, 9, 26],\n",
" [26, 13, 15, 29, 20, 20, 20, 2, 10, 13],\n",
" [ 9, 14, 9, 14, 20, 17, 15, 5, 25, 9],\n",
" [25, 14, 17, 16, 13, 7, 11, 27, 4, 8],\n",
" [27, 19, 12, 25, 29, 8, 22, 9, 24, 15],\n",
" [22, 5, 3, 14, 16, 8, 24, 29, 3, 22],\n",
" [25, 20, 4, 29, 25, 17, 24, 13, 13, 6]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 10行 x 10列の30までの乱数を生成\n",
"randint30 = np.random.randint(1, 30,(10,10))\n",
"randint30"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10, 10)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"randint30.shape"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# 乱数発生の初期化\n",
"# seed を固定すると、毎回同じ乱数生成が可能\n",
"np.random.seed(100)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1.7497654730546974"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.34268040332750216"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.153035802563644"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# 乱数の種(シード)の指定\n",
"np.random.seed(100)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1.7497654730546974"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.153035802563644"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. 行列の演算と処理"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"チャプター8「行列の演算と処理」では、Numpyを使って逆行列や転置行列など、行列の処理・演算を行いましょう。行列の積の求め方や線形代数の基礎に不安がある方は、線形代数 入門の講座も是非受講ください。\n",
"\n",
"- np.linalg.inv(ndarray) ndarrayの逆行列を算出\n",
"- ndarray.T ndarrayの転置行列を算出\n",
"- np.diag(ndarray) ndarrayの対角成分の取得\n",
"- np.inner() ベクトルの内積を算出\n",
"- np.outer() ベクトルの外積を算出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 逆行列\n",
"何かをかけたら、単位行列になる行列"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 5],\n",
" [1, 3]])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[2,5],[1,3]])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 3., -5.],\n",
" [-1., 2.]])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# np.linalg.inv() : 逆行列の計算\n",
"b = np.linalg.inv(a)\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0.],\n",
" [0., 1.]])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# np.dot : 行列の積で、単位行列になることを確認\n",
"np.dot(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 1],\n",
" [2, 6],\n",
" [4, 5]])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 転置行列(Transposed Matrix)\n",
"# a.T : a transposed = 行列aの転置行列\n",
"c = np.array([[3,1],[2,6],[4,5]])\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 2, 4],\n",
" [1, 6, 5]])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.T"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 5],\n",
" [1, 3]])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [5, 3]])"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 1]])"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 対角成分の取得\n",
"d = np.array([[1,0,0],[0,1,0],[0,0,1]])\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 1])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 対角の成分だけ取り出したい(1だけ)\n",
"# diagonal = 対角の\n",
"np.diag(d)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ベクトルの外積と内積\n",
"a = np.array([1,2,3,4])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([5, 6, 7, 8])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([5,6,7,8])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"70"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 内積を求める\n",
"np.inner(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 6, 7, 8],\n",
" [10, 12, 14, 16],\n",
" [15, 18, 21, 24],\n",
" [20, 24, 28, 32]])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 外積を求める\n",
"np.outer(a, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment