pythonらいぶらり備忘録
Numpy
np
- arange(n): 0 ~ n-1によって構成される配列の生成。
- clip(a, a_min, a_max): 配列a内のa_min以下の要素をa_minに、a_max以上の要素をa_maxにする。
- random.randint(size=(n,n), low=0, high=3): sizeのランダム整数配列生成。
- unique(array[i]): uniqueを探してnp.arrayにする。
Pandas
DataFrame
- head(n): head n行。
- dtypes: type表示。
- info(): dfの情報表示。
- describe(): dfの簡単な分析情報表示。
- to_datetime(): データ型を変更。
- groupby():
Matplotlib
pyplot(plt)
- subplot(121): 1行2列の1番目
- subplots(row, line): returnはfig, (row*lineのaxis)
- axis('off'): 軸の出力をなくす
- hist(list, lw=0, bins=256): listをヒストグラム表示。binsでbinの数、rangeで範囲設定可能。alphaで透過度。normedで正規化。lwはline + width。fcで色設定。
- xlim(min, max): x軸の範囲の設定。y軸も同様。
- xticks([]): x軸の軸のメーター?の設定。左のようにすると軸がさらさらになる。
- legend(loc='best'): legendの付加。locで場所設定。
- xtitle("title"): titleの設定。
- savefig("filename"): filenameという名前で保存。
- imread(img_path): 画像の読み込み。
- imshow(img): 画像の出力。cmapパラメターいじると面白い。
imgplot = plt.imshow(img)
imgplot.set_cmap('spectral')
とかでもいい。
scikit-learn
scikit-image
import skimage.data as skid
skid.lena()でレナさんの画像出せる。
露出補正
import skimage.exposure as skie
- rescale_intensity(img, in_range=(100, 240), out_range=(0, 255)): img配列の100以下の値は0に、240以上の値は255にする。
- equalize_adapthist(img): Contrast Limited Adaptive Histogram Equalization。(上より高度な補正手段)
フィルタ処理
- skimage.restoration.denoise_tv_bregman(img, 5.): ノイズ除去。
from skimage.morphology import closing, square
- closing(img, selem): 0と1からなる配列で、要素同士の分離をなくす。selemで許容度を設定。
- square(n): n×nのnumpy配列を生成。値は全て1。
from skimage.measure import regionprops, label
- label(img): imgの中のつながっている部分を検知し、ラベルをつける。
- regionprops(labels, ['Area', 'BoundingBox']): labels内のlabelの面積と分割画像を抽出。
from skimage.segmentation import clear_border
- clear_border(img): 画像のborderを綺麗にする。
import skimage.filters as skif
- gaussian_filter(img, 5.): ガウシアンフィルタ(ぼかし)。
- sobel(img): ソーベルフィルタ(エッジ検出)。
- threshold_otsu(img): 大津の方法で画像の明るい部分と暗い部分を分けている閾値を判定する。
特徴点検出
from skimage.feature as sf
- corner_harris(img): ハリスのコーナー検出法。
- corner_peaks(corners): コーナーの抽出。
iPython Notebook
from IPython.html import widgets
Networkx
import networkx as nx
- Graph(adj): ノード、枝の情報をもつadjからGraphオブジェクトを生成。
- DiGraph(graph): 隣接リストから、有向グラフを生成。
- grid_2d_graph(n, n): n×nの格子構造グラフの生成。
- adjacency_matrix(g): Graphオブジェクトgの隣接行列。
- is_directed_acyclic_graph(g): gが有効非巡回グラフかどうか。
- draw_networkx(g, pos, node_color): posの配置で、gを出力。
- draw_circular(g): 自動的に環状に配置したグラフを出力。
- draw_spectral(g): 自動配置アルゴリズムによる配置。
Graph
- nodes(): ノード。
- edges(): エッジ。
- add_node(n): 値nのノードの追加。
- add_edge(a,b): aとbの間にエッジを追加。
- node[i].get('color', '#fff'): i番目のnodeにattributeを追加。左は色。
Seaborn
import seaborn as sns
- violinplot(x,y):
- barplot(x,y):
- factorplot(x,y):
- countplot(x or y):
- @widgets.interact(x=(1, 5, .5)): interact barを出せる。デコレートした関数のxの値をinteractiveに変化させる。