(公開)
Contents
お知らせ
- 本日は期末試験を実施します.
- 試験範囲は...
- 持込用紙
- (持込用紙があるからといって過信せず)基本的な項目を復習して,使えるようになっておいてくださいね.
- 条件判断の制御構文(if文), 繰り返し構文(while文,for文),
- ループ制御のbreak文,continue文,
関数定義の方法
- 入出力
- 基本的な組込み関数の使い方
- 数学関数
- 文字列操作
データ型,データ構造(基本的なコンテナ)
- リスト処理,辞書(dict)操作,タプルの使い方,
- 試験範囲は...
今回の内容
概念編 (先週の続き)
[edit]
NumPyモジュール (先週の話題の続き)
- Matplotlib.pyplot (今週の新しい話題)
- 穴埋め
多次元配列(ndarray: n-dimensional array)
[edit]
- 多次元配列(ndarray: n-dimensional array)
- この授業では,リストを配列の代用に使っていましたが,正確には両者は異なります.
- 配列は,同じ型のデータだけで構成され,添え字でのアクセスが高速です..
import numpy as np x = np.array( [1,3]) y = np.array( [2,4]) print( np.dot(x,y) )
from numpy import array x = array( [1,3]) y = array( [2,4]) print( np.dot(x,y) )
from numpy import array,dot x = array( [1,3]) y = array( [2,4]) print( dot(x,y) )
from numpy import * x = array( [1,3]) y = array( [2,4]) print( dot(x,y) )
多次元配列 arr = np.asarray([[1,2,3], [4,5,6]])
NumPyによるベクトル計算や行列計算
* これまでやってきた,以下の,ベクトル計算,行列計算は,NumPyで実装されています.
- もっとも,授業では,アルゴリズムや計算手順を,Pythonで実装できるようになることが目標なので,
NumPyをつかってしまっては勉強になりません.
- なので、これまで使わずに来ましたが、知っていることは悪いことではありません.
内積,行列積
- numpy.dotを使います.
- ベクトルの内積
- 学力確認試験 第01回
- (3) 行列の積
転置行列
- transpose()メソッドか.Tを使います.
- 基本課題 - 第03回
距離
- numpy.linalg.norm()を使います.
- 例題 - 第04回
- 2点(平面座標)間の距離
- 例題 - 第04回
グラフを描きましょう
[edit]
- Matplotlib.pyplot (今週の新しい話題)
https://matplotlib.org/tutorials/introductory/pyplot.html
- 以下の例題は,このページからの引用です(2019-01-16)
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
- y-軸が与えられた[1,2,3,4]
- 対して、x-軸は添え字に対応する [0,1,2,3]
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.ylabel('some numbers')
plt.show()
- y-軸が[1,2,3,4]でy-軸が[1.4.9.16]
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
- プロットは、redの〇
import matplotlib.pyplot as plt
import numpy as np
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
- redの点線と、blueのsquareと、greenの△
import matplotlib.pyplot as plt
import numpy as np
data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
plt.scatter('a', 'b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
- 散布図
Edit conflict - other version:
- 散布図
Edit conflict - your version:
End of edit conflict
import matplotlib.pyplot as plt
import numpy as np
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
- 複数のグラフ
import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
配布資料
授業中に作成するプログラム(授業後などに念のために配布する予定です)
今回の内容
概念編
- 内容
実習編
- 内容
プログラムの説明
- 配布プログラムの概要
- サンプルプログラム
*
*