pythonらいぶらり備忘録

Numpy

np
  1. arange(n): 0 ~ n-1によって構成される配列の生成。
  2. clip(a, a_min, a_max): 配列a内のa_min以下の要素をa_minに、a_max以上の要素をa_maxにする。
  3. random.randint(size=(n,n), low=0, high=3): sizeのランダム整数配列生成。
  4. unique(array[i]): uniqueを探してnp.arrayにする。
Array
  1. ravel(): 多次元配列を一次元に。

Pandas

DataFrame
  1. head(n): head n行。
  2. dtypes: type表示。
  3. info(): dfの情報表示。
  4. describe(): dfの簡単な分析情報表示。
  5. to_datetime(): データ型を変更。
  6. groupby():

Matplotlib

pyplot(plt)
  1. subplot(121): 1行2列の1番目
  2. subplots(row, line): returnはfig, (row*lineのaxis)
  3. axis('off'): 軸の出力をなくす
  4. hist(list, lw=0, bins=256): listをヒストグラム表示。binsでbinの数、rangeで範囲設定可能。alphaで透過度。normedで正規化。lwはline + width。fcで色設定。
  5. xlim(min, max): x軸の範囲の設定。y軸も同様。
  6. xticks([]): x軸の軸のメーター?の設定。左のようにすると軸がさらさらになる。
  7. legend(loc='best'): legendの付加。locで場所設定。
  8. xtitle("title"): titleの設定。
  9. savefig("filename"): filenameという名前で保存。
  10. imread(img_path): 画像の読み込み。
  11. 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

  1. rescale_intensity(img, in_range=(100, 240), out_range=(0, 255)): img配列の100以下の値は0に、240以上の値は255にする。
  2. equalize_adapthist(img): Contrast Limited Adaptive Histogram Equalization。(上より高度な補正手段)
フィルタ処理
  1. skimage.restoration.denoise_tv_bregman(img, 5.): ノイズ除去。

from skimage.morphology import closing, square

  1. closing(img, selem): 0と1からなる配列で、要素同士の分離をなくす。selemで許容度を設定。
  2. square(n): n×nのnumpy配列を生成。値は全て1。

from skimage.measure import regionprops, label

  1. label(img): imgの中のつながっている部分を検知し、ラベルをつける。
  2. regionprops(labels, ['Area', 'BoundingBox']): labels内のlabelの面積と分割画像を抽出。

from skimage.segmentation import clear_border

  1. clear_border(img): 画像のborderを綺麗にする。

import skimage.filters as skif

  1. gaussian_filter(img, 5.): ガウシアンフィルタ(ぼかし)。
  2. sobel(img): ソーベルフィルタ(エッジ検出)。
  3. threshold_otsu(img): 大津の方法で画像の明るい部分と暗い部分を分けている閾値を判定する。
特徴点検出

from skimage.feature as sf

  1. corner_harris(img): ハリスのコーナー検出法。
  2. corner_peaks(corners): コーナーの抽出。

iPython Notebook

from IPython.html import widgets

Networkx

import networkx as nx

  1. Graph(adj): ノード、枝の情報をもつadjからGraphオブジェクトを生成。
  2. DiGraph(graph): 隣接リストから、有向グラフを生成。
  3. grid_2d_graph(n, n): n×nの格子構造グラフの生成。
  4. adjacency_matrix(g): Graphオブジェクトgの隣接行列。
  5. is_directed_acyclic_graph(g): gが有効非巡回グラフかどうか。
  6. draw_networkx(g, pos, node_color): posの配置で、gを出力。
  7. draw_circular(g): 自動的に環状に配置したグラフを出力。
  8. draw_spectral(g): 自動配置アルゴリズムによる配置。
Graph
  1. nodes(): ノード。
  2. edges(): エッジ。
  3. add_node(n): 値nのノードの追加。
  4. add_edge(a,b): aとbの間にエッジを追加。
  5. node[i].get('color', '#fff'): i番目のnodeにattributeを追加。左は色。
Seaborn

import seaborn as sns

  1. violinplot(x,y):
  2. barplot(x,y):
  3. factorplot(x,y):
  4. countplot(x or y):
  1. @widgets.interact(x=(1, 5, .5)): interact barを出せる。デコレートした関数のxの値をinteractiveに変化させる。