58

Python semi 第2回 --- What's the Package?

Embed Size (px)

Citation preview

Page 1: Python semi 第2回 --- What's the Package?

PythonSemi第2回黒柳研究室M1

26413508

大脇謙太

Page 2: Python semi 第2回 --- What's the Package?

第1回 Pythonとは , 基本的文法 , 組み込み型

第2回 パッケージを使う

第3回 オブジェクト指向とは

第4回 Numpy , Scipyを使った科学計算

Pythonゼミの予定

Page 3: Python semi 第2回 --- What's the Package?

簡単な文法

インデントによるブロック表記

組み込み型 数値 1 , 2.7 , 3.4j

文字列 “foo” , ‘bar’ , “Hello ‘World’ “

リスト [1, “two” , 3.4 , [5,6] ]

タプル ( 1 , “two” , 3.4 , [5,6] )

辞書 { “foo” : “bar” , (1,2) : [3,4] }

前回のおさらい

Page 4: Python semi 第2回 --- What's the Package?

バブルソート

2系,3系どちらでも動作します

Page 5: Python semi 第2回 --- What's the Package?

オブジェクト指向

オブジェクトという単位で扱う

“クラス”という概念

オブジェクトは

メンバ変数(内部状態)

メソッド (動作)

を含む

例 )

車クラス

速度 ( メンバ変数 )

アクセル( メソッド )

ブレーキ( メソッド )

Page 6: Python semi 第2回 --- What's the Package?

今回の内容

パッケージを使う

Page 7: Python semi 第2回 --- What's the Package?

パッケージ(モジュール)とは

モジュール

関数や定数が定義されたファイル

数学的な関数 : sin() , cos()

mathモジュール

パッケージ

モジュールを束ねて管理する仕組み

1つのパッケージの中には複数のモジュールが入っている

Page 8: Python semi 第2回 --- What's the Package?

モジュール… 関数や定数が定義されたファイル パッケージ… モジュールをまとめ、構造化したもの

パッケージ(モジュール)とは

Sound/ トップレベルのパッケージ__init__.pyサウンドパッケージを初期化する

Formats/ ファイルフォーマット変換用の下位パッケージ__init__.py

wavread.pywavwrite.pyaiffread.py ...

Effects/ サウンド効果用の下位パッケージ__init__.pyecho.pysurround.py...

Filters/ フィルタ用の下位パッケージ__init__.pyequalizer.py vocoder.py ... 引用: http://docs.python.jp/2.5/tut/node8.html

呼び出したい

Page 9: Python semi 第2回 --- What's the Package?

Sound/Format/ wavread.py を呼び出したい

import Sound

呼び出すときは Sound.Format.wavread( )

from Sound import Format

呼び出すときは Format.wavread( )

パッケージを使うには

import文 , from文を使う

Page 10: Python semi 第2回 --- What's the Package?

asを使えばエイリアス(別名)も付けれる

import Sound.Format as fmt

呼び出すときは fmt.wavread( )

(asを使っていない時は Sound.Format.wavread() )

“as”を使う

Page 11: Python semi 第2回 --- What's the Package?

今回紹介する(モジュール)

Math

Wave

Numpy

Matplotlib

パッケージ/モジュールを使う

Page 12: Python semi 第2回 --- What's the Package?

数論、指数・対数、三角関数、定数など

mathモジュール

数論 指数/対数 三角関数 双曲線関数 その他関数 定数

ceil(x) pow(x , y ) sin(x) sinh(x) erf( x ) math.pi

floor(x) sqrt(x) cos(x) cosh(x) erfc( x ) math.e

factorial(x) exp(x) tan(x) tanh(x) gamma( x )

isinf(x) expm1(x) asin(x) asinh(x) lgamma(x)

isnan(x) log(x) acos(x) acosh(x) radians(x)

fmod(x , y) log1p(x) atan(x) atanh(x) degrees(x)

fsum( iter ) log10(x) atan2(y , x)

modf(x) hypot(x , y)

ldexp( x , l )

frexp(x)

※python2.7

Page 13: Python semi 第2回 --- What's the Package?

WAVファイル用のIO

waveモジュール

wave Wave_readオブジェクト Wave_Writeオブジェクト

open(file , mode ) close() close()

openfp( file , mode) getnchannels() setnchannels(n)

exception wave.Error getsampwidth() setsampwidth(n)

getframerate() setframerate(n)

getnframes() setnframes(n)

getcomptoype() setcomptype(type , name )

getcompname() setparams( tuple )

getparams() tell()

readframes(n) writeframesraw(data)

rewind() writeframes( data )

setpos( pos )

tell()

※python2.7

Page 14: Python semi 第2回 --- What's the Package?

行列/数値計算用パッケージ

Numpyパッケージ(1/2)

numpy ndarrayオブジェクト

array( obj ) dot(a,b) sin(x) dtype average()

arange( s, e , step) cross(a,b) cos(x) shape var()

linspace(s, e , n ) hstack(a,b) tan(x) size mean()

eye( n ) vstack(a,b) exp(x) nbytes median()

zeros( shape ) log(x) tolist() max()

ones( shape ) sqrt(x) reshape(shape) min()

diag( a ) absolute(x) ravel()

identity(n) square(x) flattern()

copy(a) real(x) astype( t )

fromstring( str ) imag(x) flat

hamming(M) T or transpose()

※v1.8

Page 15: Python semi 第2回 --- What's the Package?

行列/数値計算用パッケージ

Numpyパッケージ(2/2)

lingalgモジュール

randomモジュール

fftモジュール

inv(a) rand(row , col) fft( a )

pinv(a) randn(row , col ) ifft(a)

solve(a,b) randint(min , max , shape) fft2(a)

eigvals(a) ifft2(a)

eig(a) fftn(a)

ifftn(a)

rfft(a)

irfft(a)

※v1.8

Page 16: Python semi 第2回 --- What's the Package?

MATLABの様なグラフ表示

Matplotlib(pylab)

pyplotモジュール

figure() xlim( min , max )

subplot(n , m , index ) ylim( min , max )

axes( [x , y , width, height ] ) xticks( list [, label])

plot(...) yticks( list [, label])

scatter(...) legend( loc )

bar(...) annotate( comment , xy )

contour(...)

pie(...)

show( )

Page 17: Python semi 第2回 --- What's the Package?

行列と1次元配列

Numpyを使う

Page 18: Python semi 第2回 --- What's the Package?

𝑦 = 𝑥3をプロット

プロットする

Page 19: Python semi 第2回 --- What's the Package?

WAVファイルをFFT

Wavファイルを読み込んでFFTをする

Page 20: Python semi 第2回 --- What's the Package?

1. y = cos 𝑥 , 0 ≤ 𝑥 ≤ 2𝜋 をプロットせよ

2. 次のwavファイルをFFTして周波数スペクトル(左右)をプロットせよWavファイル : \\tronco\Clipboard\Owaki\sm.wavサンプリング周波数 : 44.1kH量子化ビット数 : 16bitチャンネル数 : 2ch (ステレオ)FFTの点数 : 1024点

FFTする箇所 : 5.0[sec] から1024点の1フレーム

3. [応用]上のデータのスペクトログラムを作成せよ( 0.0[sec] からデータ終端まで )

課題

Page 21: Python semi 第2回 --- What's the Package?

オブジェクティブ指向とは

オブジェクトという考え方

インスタンスとメソッド

クラス、モジュール、パッケージ

来週は課題はなし

第3回予告

Page 22: Python semi 第2回 --- What's the Package?

モジュール

http://docs.python.jp/2.5/tut/node8.html

9.2 math ---数学関数( Python2.7)

http://docs.python.jp/2/library/math.html

21.5 wave --- WAVファイルの読み書き

http://docs.python.jp/2/library/wave.html

Numpyのリファレンス – TEXT/YUBASCRIPT

http://blog.yubais.net/16.html

参考文献

Page 23: Python semi 第2回 --- What's the Package?

Numpy Reference – Scipy.org

http://docs.scipy.org/doc/numpy/reference/

1.4.Matplotlib:作図

http://turbare.net/transl/scipy-lecture-notes/intro/matplotlib/matplotlib.html

参考文献