ぴろの狂人日記

2014年から頑張ってブログを継続しようと思います。「継続と習慣」を今年の目標にしたので、頑張って更新を続けようと思います。おいおいはレビューや数学や認知科学などについて記事を書いていければと思っています

数年ぶりのレッスン

先日のことにはなってしまいますが、ビオラのレッスンを再開しました。

今年に入り、ビオラの練習をまた始め、高校のOBオケとか大学オケのエキストラとかの予定を入れ、交響曲の譜面をさらっているのですが、やっぱり基礎力ももっと鍛えたいなーと欲張りだし、レッスンを再開しました。

札幌へ行く前までレッスンを受けていたときと同じ先生の元へ。

どこまで進んでいたのかわかってもらってるし、自分のレベルもだいたいわかってもらっているのでね。

しばらくは基礎力を徹底的に鍛えたいので、音階とボーイングをやることにしました。

ボーイングはセブシックをやります。

セブシック、いろんなパターンのボーイングが紹介されてて、7年やっているのにページ数でいったらあんまり進んでない。ありすぎだろ。。。

で今はスタッカート系の音型をやっています。次回の音型がこれ。163番。

 

f:id:piroshhh:20161125000235p:plain


八分でならまずまずいけるんだけど、三連符になった瞬間うまく音を区切れない。。。
この音型苦手。。。
まあオーケストラの譜面ではあまりでない音型だけど、網羅しておきたいのできちんとやろう。


音階の方は2オクターブの音階。二周目。

過去に全部の調性を一通りやってはいましたが、中には難しいやつもあり、心配なやつがあったので、簡単なやつは飛ばして、シャープとかフラットがたくさんあったりして難しいやつだけを復習。

今回はCis mollをやりました。

一回目にやったときは、ただの音階とはいえ、音を追うのに一杯一杯でしたが、二回目ともなると、ある程度体に染み込んでいるからか、余裕をもって冷静に弾けるようになっていました。

正しい音の場所に指が自然と反応するというか、多少ずれたとしても正しい音程に修正できたり、全体的な精度が格段に増しておりました。


基礎練をしっかりやると、実際オーケストラのあの難しい譜面でも少し弾けるようになっていて、着実に前進している感じがするので、地味だけど意外に好きです。

修行している感じというか、なんというか。

Sklearnを使ってみる1

かなり以前になりますが、

piroshhh.hatenablog.com

でsklearnをインストールしたので、『実践 機械学習システム』を読みながら、sklearnをいじってみたいと思います。

で、特に理由はありませんが、スタートは第2章のIrisデータセットを扱う章から、始めます。Irisデータセット自体はsklearnをインストールしたときに一緒についてきます。

最初にがっつりとしたコードが出てくるのが以下のようなコードです。

from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
import numpy as np

data = load_iris()

features=data["data"]
feature_names=data["feature_names"]
target=data["target"]
target_names=data["target_names"]
labels=target_names[target]

for t,marker,c in zip (xrange(3),">ox","rgb"):

 plt.scatter(features[target == t,0],features[target == t,1],marker=marker,c=c)

 


本の冒頭にて、この本ではpython2.7をベースにかかれているのですが、自分はPython3.4を使っているのでうまく動きません。それに加え、そもそもコードの意味もよくわかりません。。。

とりあえず、これを動くようにしたり解読したりするところから始めたいと思います。

まず、動かす。

まず、動くようにすることを考えます。
どうやら、xrangeといところが問題だったようです。python3.xではrangeの方にxrangeが統合されているようです。
それと最後にplt.show()も追記が必要です。
結局のところ、以下のようにすれば動作します。

from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
import numpy as np

data = load_iris()

features=data["data"]
feature_names=data["feature_names"]
target=data["target"]
target_names=data["target_names"]
labels=target_names[target]

for t,marker,c in zip (range(3),'>ox','rgb'):
                       
 plt.scatter(features[target == t,0],features[target == t,1],marker=marker,c=c)
 plt.show()


Irisデータセットの構造

とりあえず動いたので、もっと詳細を解読していきたいと思います。
次に、何をしているのかよくわからなかったのが、上の例でいう5行目(空白行は除いて)から9行目の部分。

features=data["data"]
feature_names=data["feature_names"]
target=data["target"]
target_names=data["target_names"]
labels=target_names[target]


これはIrisデータセットがディクショナリー型になっていることがわかり理解できました。

Irisデータセットは次のようなディクショナリー型のデータになっています。

####説明のための擬似コード。
load_iris={data:(測定値などの一覧),
DESCR:(引用情報などの参考情報),
target_names:(あやめ(Iris)の種類の名前),
featurenames:(「 花弁の長さ」など特徴量の名前),
target:(あやめの種類の名前にそれぞれ割り当てる番号)
}


ディクショナリのキーを取得しようとすると次のようになります。

#最初の>>python3を実行する部分 は省略
>>from sklearn.datasets import load_iris
>>data=load_iris()
>>data.keys()
dict_keys(['DESCR', 'data', 'feature_names', 'target', 'target_names'])
#表示される順番は毎回違うようです。



#####ファイルに保存して実行する場合はこうする。
from sklearn.datasets import load_iris
data=load_iris()
print(data.keys())

###→例えばiris.pyという名前で保存し、ターミナルから実行すると同じような結果になる。


各項目の中をみてみます。まずは、'data'というキーに対応する要素は次のようになっています。

#最初の>>python3を実行する部分 は省略
>>from sklearn.datasets import load_iris
>>data=load_iris()
>>data.data()
array([[ 5.1,  3.5,  1.4,  0.2],
       [ 4.9,  3. ,  1.4,  0.2],
       [ 4.7,  3.2,  1.3,  0.2],
       [ 4.6,  3.1,  1.5,  0.2],
       [ 5. ,  3.6,  1.4,  0.2],
       [ 5.4,  3.9,  1.7,  0.4],
       [ 4.6,  3.4,  1.4,  0.3],
       [ 5. ,  3.4,  1.5,  0.2],
       [ 4.4,  2.9,  1.4,  0.2],
       [ 4.9,  3.1,  1.5,  0.1],
       [ 5.4,  3.7,  1.5,  0.2],
       [ 4.8,  3.4,  1.6,  0.2],
       [ 4.8,  3. ,  1.4,  0.1],
       [ 4.3,  3. ,  1.1,  0.1],
       [ 5.8,  4. ,  1.2,  0.2],
       [ 5.7,  4.4,  1.5,  0.4],
       [ 5.4,  3.9,  1.3,  0.4],
       [ 5.1,  3.5,  1.4,  0.3],
       [ 5.7,  3.8,  1.7,  0.3],
       [ 5.1,  3.8,  1.5,  0.3],
       [ 5.4,  3.4,  1.7,  0.2],
       [ 5.1,  3.7,  1.5,  0.4],
       [ 4.6,  3.6,  1. ,  0.2],
       [ 5.1,  3.3,  1.7,  0.5],
       [ 4.8,  3.4,  1.9,  0.2],
       [ 5. ,  3. ,  1.6,  0.2],
       [ 5. ,  3.4,  1.6,  0.4],
       [ 5.2,  3.5,  1.5,  0.2],
       [ 5.2,  3.4,  1.4,  0.2],
       [ 4.7,  3.2,  1.6,  0.2],
       [ 4.8,  3.1,  1.6,  0.2],
       [ 5.4,  3.4,  1.5,  0.4],
       [ 5.2,  4.1,  1.5,  0.1],
       [ 5.5,  4.2,  1.4,  0.2],
       [ 4.9,  3.1,  1.5,  0.1],
       [ 5. ,  3.2,  1.2,  0.2],
       [ 5.5,  3.5,  1.3,  0.2],
       [ 4.9,  3.1,  1.5,  0.1],
       [ 4.4,  3. ,  1.3,  0.2],
       [ 5.1,  3.4,  1.5,  0.2],
       [ 5. ,  3.5,  1.3,  0.3],
       [ 4.5,  2.3,  1.3,  0.3],
       [ 4.4,  3.2,  1.3,  0.2],
       [ 5. ,  3.5,  1.6,  0.6],
       [ 5.1,  3.8,  1.9,  0.4],
       [ 4.8,  3. ,  1.4,  0.3],
       [ 5.1,  3.8,  1.6,  0.2],
       [ 4.6,  3.2,  1.4,  0.2],
       [ 5.3,  3.7,  1.5,  0.2],
       [ 5. ,  3.3,  1.4,  0.2],
       [ 7. ,  3.2,  4.7,  1.4],
       [ 6.4,  3.2,  4.5,  1.5],
       [ 6.9,  3.1,  4.9,  1.5],
       [ 5.5,  2.3,  4. ,  1.3],
       [ 6.5,  2.8,  4.6,  1.5],
       [ 5.7,  2.8,  4.5,  1.3],
       [ 6.3,  3.3,  4.7,  1.6],
       [ 4.9,  2.4,  3.3,  1. ],
       [ 6.6,  2.9,  4.6,  1.3],
       [ 5.2,  2.7,  3.9,  1.4],
       [ 5. ,  2. ,  3.5,  1. ],
       [ 5.9,  3. ,  4.2,  1.5],
       [ 6. ,  2.2,  4. ,  1. ],
       [ 6.1,  2.9,  4.7,  1.4],
       [ 5.6,  2.9,  3.6,  1.3],
       [ 6.7,  3.1,  4.4,  1.4],
       [ 5.6,  3. ,  4.5,  1.5],
       [ 5.8,  2.7,  4.1,  1. ],
       [ 6.2,  2.2,  4.5,  1.5],
       [ 5.6,  2.5,  3.9,  1.1],
       [ 5.9,  3.2,  4.8,  1.8],
       [ 6.1,  2.8,  4. ,  1.3],
       [ 6.3,  2.5,  4.9,  1.5],
       [ 6.1,  2.8,  4.7,  1.2],
       [ 6.4,  2.9,  4.3,  1.3],
       [ 6.6,  3. ,  4.4,  1.4],
       [ 6.8,  2.8,  4.8,  1.4],
       [ 6.7,  3. ,  5. ,  1.7],
       [ 6. ,  2.9,  4.5,  1.5],
       [ 5.7,  2.6,  3.5,  1. ],
       [ 5.5,  2.4,  3.8,  1.1],
       [ 5.5,  2.4,  3.7,  1. ],
       [ 5.8,  2.7,  3.9,  1.2],
       [ 6. ,  2.7,  5.1,  1.6],
       [ 5.4,  3. ,  4.5,  1.5],
       [ 6. ,  3.4,  4.5,  1.6],
       [ 6.7,  3.1,  4.7,  1.5],
       [ 6.3,  2.3,  4.4,  1.3],
       [ 5.6,  3. ,  4.1,  1.3],
       [ 5.5,  2.5,  4. ,  1.3],
       [ 5.5,  2.6,  4.4,  1.2],
       [ 6.1,  3. ,  4.6,  1.4],
       [ 5.8,  2.6,  4. ,  1.2],
       [ 5. ,  2.3,  3.3,  1. ],
       [ 5.6,  2.7,  4.2,  1.3],
       [ 5.7,  3. ,  4.2,  1.2],
       [ 5.7,  2.9,  4.2,  1.3],
       [ 6.2,  2.9,  4.3,  1.3],
       [ 5.1,  2.5,  3. ,  1.1],
       [ 5.7,  2.8,  4.1,  1.3],
       [ 6.3,  3.3,  6. ,  2.5],
       [ 5.8,  2.7,  5.1,  1.9],
       [ 7.1,  3. ,  5.9,  2.1],
       [ 6.3,  2.9,  5.6,  1.8],
       [ 6.5,  3. ,  5.8,  2.2],
       [ 7.6,  3. ,  6.6,  2.1],
       [ 4.9,  2.5,  4.5,  1.7],
       [ 7.3,  2.9,  6.3,  1.8],
       [ 6.7,  2.5,  5.8,  1.8],
       [ 7.2,  3.6,  6.1,  2.5],
       [ 6.5,  3.2,  5.1,  2. ],
       [ 6.4,  2.7,  5.3,  1.9],
       [ 6.8,  3. ,  5.5,  2.1],
       [ 5.7,  2.5,  5. ,  2. ],
       [ 5.8,  2.8,  5.1,  2.4],
       [ 6.4,  3.2,  5.3,  2.3],
       [ 6.5,  3. ,  5.5,  1.8],
       [ 7.7,  3.8,  6.7,  2.2],
       [ 7.7,  2.6,  6.9,  2.3],
       [ 6. ,  2.2,  5. ,  1.5],
       [ 6.9,  3.2,  5.7,  2.3],
       [ 5.6,  2.8,  4.9,  2. ],
       [ 7.7,  2.8,  6.7,  2. ],
       [ 6.3,  2.7,  4.9,  1.8],
       [ 6.7,  3.3,  5.7,  2.1],
       [ 7.2,  3.2,  6. ,  1.8],
       [ 6.2,  2.8,  4.8,  1.8],
       [ 6.1,  3. ,  4.9,  1.8],
       [ 6.4,  2.8,  5.6,  2.1],
       [ 7.2,  3. ,  5.8,  1.6],
       [ 7.4,  2.8,  6.1,  1.9],
       [ 7.9,  3.8,  6.4,  2. ],
       [ 6.4,  2.8,  5.6,  2.2],
       [ 6.3,  2.8,  5.1,  1.5],
       [ 6.1,  2.6,  5.6,  1.4],
       [ 7.7,  3. ,  6.1,  2.3],
       [ 6.3,  3.4,  5.6,  2.4],
       [ 6.4,  3.1,  5.5,  1.8],
       [ 6. ,  3. ,  4.8,  1.8],
       [ 6.9,  3.1,  5.4,  2.1],
       [ 6.7,  3.1,  5.6,  2.4],
       [ 6.9,  3.1,  5.1,  2.3],
       [ 5.8,  2.7,  5.1,  1.9],
       [ 6.8,  3.2,  5.9,  2.3],
       [ 6.7,  3.3,  5.7,  2.5],
       [ 6.7,  3. ,  5.2,  2.3],
       [ 6.3,  2.5,  5. ,  1.9],
       [ 6.5,  3. ,  5.2,  2. ],
       [ 6.2,  3.4,  5.4,  2.3],
       [ 5.9,  3. ,  5.1,  1.8]])


実際に各サンプルに対する測定値が格納されている様子がわかります。

次は、'target_names'に対応する要素、あやめ(Iris)の種類の名前としてどういう要素が格納されているかみます。
調べ方は上記とほとんど同じです。

#最初の>>python3を実行する部分 は省略
>>from sklearn.datasets import load_iris
>>data=load_iris()
>>data.target_names
array(['setosa', 'versicolor', 'virginica'], 
      dtype='


setosaとversicolorとvirginicaという名前があるのがわかります。最後についている余分なやつはよく意味はわかりませんが、とりあえずスルーします。

同様に、'feature_names'に対応する要素の中をみます。

#最初の>>python3を実行する部分 は省略
>>from sklearn.datasets import load_iris
>>data=load_iris()
>>data.feature_names
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']


最後に、'target'に対応する要素の中身。

#最初の>>python3を実行する部分 は省略
>>from sklearn.datasets import load_iris
>>data=load_iris()
>>data.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])


で最後のlabels=target_names[target]の行で、0→setosa、1→versicolor、2→virginicaに対応させる処理をしています。

これも最初あまり意味がわかりませんでしたが、numpyの文法です。

target_namesのリストから0か1か2に対応するインデックスの要素を取り出し、新しいリストを作るという意味です。

事実上ラベル付けを行っていることになります。よく使う利用法らしい。

>>from sklearn.datasets import load_iris
>>data=load_iris()
>>> features=data['data']
>>> features_name=data['feature_names']
>>> target=data['target']
>>> target_names=data['target_names']
>>> label=target_names[target]
>>> label
array(['setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'setosa', 'setosa', 'setosa', 'setosa',
       'setosa', 'setosa', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'versicolor',
       'versicolor', 'versicolor', 'versicolor', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica', 'virginica', 'virginica',
       'virginica', 'virginica', 'virginica'], 
   )


 

for文のブロック

次にわからなかったのが、この行です。

for t,marker,c in zip (range(3),’>ox’,’rgb’):


まず、このzipというのはfor文を3つ同時に処理する関数のようです。(なんて便利なんだ。。。)
上の例でいえば、変数tにはrange(3)の中身を順番に代入、変数markerには{>,o,x}を順番に代入、変数cには{r,g,b}を順番に代入、ということです。

で、rgbの方はいいとして、>oxってなんなのかと。

これは、実際に描画するときのマーカーの形(?)だそうです。matplotlibのオプションです。
詳しくはここのページを(Markers--matplotlib.markers)


もう少しだ。。。次はこの行。

plt.scatter(features[target == t,0],features[target == t,1],marker=marker,c=c)


まず、scatterは散布図を描画するmatplotlib提供の関数です。
features[target==t,0]というのは、features自体が実際にデータ(数値)が格納されているリストなわけですが、その中でも[ ]の中の条件に一致するものだけを抽出していますよ、という意味です。
つまり上の例でいえば、featuresの0番目の要素の一覧の中、targetが例えば0(=0であればそれはsetosaなわけですが)に該当するもの、
さらに言い換えておけば、target=0のときはsetosaのfeaturesの0番目要素だけを抽出するという意味です。

なんか言い換えたけどわかりやすくなっている気がしない。

さらに、plt.scatter()やplt.plot()などmatplotlibの描画系は先にグラフを生成して、最後にplt.show()をすると、最後にグラフが重ねて描画されます。

今回のケースではfor文として3回処理されるので、3種類のプロットが重ねて描かれます。


上記のようなことも含め、実際に冒頭のコードを実行すると、次のような画面が出てきます。



そうすると、本に書かれている6枚のグラフのうち、一番左上のグラフが描画されます。


Graphviz/PyGraphvizのインストール

前々から使ってみたいとは思っていました。

調べてみたら以外にインストールのハードル自体は低そうなので、サクっと入れてみました。
brewとpip3を用いてインストールできるようです。
brewについてはbrew searchを実行してたくさん吐き出されるリストの中にgraphvizが確かにあることを自分で確認してみましょう。

環境は以下の通り。

  • El'Captan(10.11.5)
  • Homebrewはインストール済み

 

Graphvizのインストール

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew install graphviz


と実行するだけです。例のごとく自分のPCのパスワードを求められるので、パスワードを入力すれば、次のような処理がはしります。それほど長くない処理。

==> Downloading https://homebrew.bintray.com/bottles/graphviz-2.38.0.el_capitan.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring graphviz-2.38.0.el_capitan.bottle.1.tar.gz
🍺  /usr/local/Cellar/graphviz/2.38.0: 469 files, 67M


Graphvizのインストール自体はこれだけ。
PyGraphvizのインストール、と先へ進む前に、Graphvizのインストールがきちんとできているか確認します。

テキストファイルに次のように記述して、Sample.dotというように拡張子をdotにして保存します。

#####以下のように記述し、dotという拡張子で保存
graph g{
    "Hello World"
    }


でSample.dotという、今保存したファイルが保存してあるディレクトリへカレントディレクトリを移し、そこで次のようなコマンドを実行します。

######Sample.svgというファイルで出力する場合
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ dot -Tsvg Sample.dot -o Sample.svg


そうするとSample.dotが保存されているディレクトリと同じディレクトリにSample.svgとファイルが生成されます。
それをブラウザで開けば、次のようなものが表示されます。

 

注意)紛らわしいけど、上に貼り付けてある画像はSVGファイルをブラウザで開いたものをスクリーンショットで撮影したpng画像です!BloggerだとSVGファイルは挿入できないらしい。

PyGraphvizのインストール

pip3を入れているので、以下のようにコマンドを打つだけで完了です。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ sudo pip3 install pygraphviz


例のごとく自分のPCのパスワードを入れると次のような処理が進み(すぐ終わります)、完了です。

The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pygraphviz
  Downloading pygraphviz-1.3.1.zip (123kB)
    100% |████████████████████████████████| 126kB 2.0MB/s 
Building wheels for collected packages: pygraphviz
  Running setup.py bdist_wheel for pygraphviz
  Stored in directory: /Users/hiroshi/Library/Caches/pip/wheels/81/03/ed/eb3b7ef51ee6527abfaf2701e222e74107b97344cfa2201881
Successfully built pygraphviz
Installing collected packages: pygraphviz
Successfully installed pygraphviz-1.3.1


確認作業は、

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ python3    #python3と打ってrerurnを押す。
Python 3.4.3 (default, Aug 11 2015, 08:57:25) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygraphviz       #import pygraphvizと打ってreturn
>>> 


としてエラーがでなければOK。

シグモイド関数の描画

gnuplotもインストールしたし、matplotlibも使える状態だしということで、なんだかんだ可視化やグラフ描画をするツールは割りと揃ってきました。

それぞれのツールの使い方の勉強という意味含め、ニューラルネットとかやるときのシグモイド関数関するの振る舞いをイメージしておきたいなども理由もあるので、シグモイド関数を描画してみようと思い描画しました。

gnuplotでの描画

gnuplotでもfor文が使えるらしいのでそれを使います。
ターミナルを起動して、gnuplotを起動します。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ gnuplot     #gnuplotと打ち込んでreturnを押す


起動したら次のように打ちます。

gnuplot> set yrange[0:1]       #見栄えを良くするための設定。y軸方向の描画範囲の指定。
gnuplot> plot for [i=0:10] 1/(1+exp(-i*x/10))


これを実行すると、次のような図が出てきます。

 
 


gnuplotを起動したときに、毎回表示されるバージョンを確認しましょう。
どうやら、plot forという書き方はバージョン4.4からの機能のようです。
(参考:

termoshtt.blog110.fc2.com

)

matplotlibでの描画

matplotlibでは次のように描画します。 matplotlibとscipyがインストールできていることは前提。 インストールできていない場合はまずこちら。

piroshhh.hatenablog.com

 

piroshhh.hatenablog.com

from matplotlib import pyplot as plt
import scipy as sp

x=sp.arange(-10,10,0.1)
for t in range(10):        #必要なグラフは先にすべて生成する。
  y=1/(1+sp.exp(-(2*t*x/10)))
  plt.plot(x,y)         


plt.show()   #最後にまとめて描画すると重ねて表示される。


matplotlibの使い方の説明にもなってしまいますが、グラフを重ねて描画するときは、先にplt.plot()、plt.plot()という感じで必要なグラフをすべて生成して、最後にplt.show()をして最後に描画する。

上記を例えば、テキストファイルに書き、sigmoid.pyという名前で保存。sigmoid.pyがあるカレントディレクトリに移動して、

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ python3 sigmoid.py


とコマンドを打って実行すると、次のような画面が出てきます。

 


for文のところで、あまり数を刻みすぎてしまうと、グラフが表示されたときよくわからない表示になってしまうので、適当に間引いて描画したほうがそれらしくみえる。

参考にしたページや文献・書籍

termoshtt.blog110.fc2.com


シグモイド関数 - Wikipedia

gnuplotのインストール

ここ最近いろいろなものをインストールしております。

 
前回はTeXをインストールしましたが、gnuplotもインストールしました。
 
これについてもインストール方法を残しておこうと思います。
 
 

Aquatermのインストール

やや天下り的な感じになってしまいますが、gnuplotだけインストールしても出力をする先がないので、描画ソフトもインストールする必要があります。
調べた感じでは、ここで書くAquatermと次で書くX11が代表的なもののようなので、それをインストールします。

Aquatermのインストールについてはまずダウンロードのサイトへアクセスします。

ダウンロードしたファイルを実行すると、インストールの画面が立ち上がります。


右下の「続ける」ボタンを押して次へ行きます。そのあとも画面の指示通りに進んで行きます。

 

続けるを押すと同意するかどうかを聞かれますので、同意をして先へ進みます。

 

 

「インストール」ボタンを押すと、いろいろ処理が始まり、最終的には次の画面になります。

Finderのアプリケーションのディレクトリをみると、確かにAquatermがインストールされています。


X11(Xquarz)のインストール

いろいろ調べてみると、現在はX11はXquarzは統合されているようです。
これもインストール方法は、ほとんどAquatermを同じです。

まずはダウンロードサイトへアクセスします。

ファイルをダウンロードして実行すると、インストール画面が立ち上がりますので、それに従って進んでいくだけです。


 

 

 

 

インストール完了の画面のスクリーンショットと撮り忘れましたが、最後にインストール完了の画面が表示されます。

でFinder中のアプリケーションの中のユーティリティの中をみるとXQuarzがインストールされています。

 

gnuplotのインストール

ここまでやってしまえば、あとはすぐです。 ターミナルを起動して次のようにコマンドを打ちます。

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew install gnuplot --with-aquaterm


そうすると、次のような処理が始まります。

==> Installing dependencies for gnuplot: pkg-config, libpng, freetype, fo
==> Installing gnuplot dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.el_capitan.
######################################################################## 100.0%
==> Pouring pkg-config-0.29.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29: 10 files, 624.4K
==> Installing gnuplot dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.21.el_capitan.bo
######################################################################## 100.0%
==> Pouring libpng-1.6.21.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.21: 17 files, 1.2M
==> Installing gnuplot dependency: freetype
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.6.3.el_capitan.b
######################################################################## 100.0%
==> Pouring freetype-2.6.3.el_capitan.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/freetype-config
Target /usr/local/bin/freetype-config
already exists. You may want to remove it:
  rm '/usr/local/bin/freetype-config'

To force the link and overwrite all conflicting files:
  brew link --overwrite freetype

To list all files that would be deleted:
  brew link --overwrite --dry-run freetype

Possible conflicting files are:
/usr/local/bin/freetype-config
/usr/local/include/freetype2/ft2build.h
/usr/local/share/aclocal/freetype2.m4
/usr/local/share/man/man1/freetype-config.1
/usr/local/lib/libfreetype.dylib -> /usr/local/lib/libfreetype.6.dylib
==> Summary
🍺  /usr/local/Cellar/freetype/2.6.3: 60 files, 2.5M
==> Installing gnuplot dependency: fontconfig
==> Downloading https://homebrew.bintray.com/bottles/fontconfig-2.11.1_2.el_capi
######################################################################## 100.0%
==> Pouring fontconfig-2.11.1_2.el_capitan.bottle.tar.gz
==> /usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache -frv
Last 15 lines from /Users/hiroshi/Library/Logs/Homebrew/fontconfig/01.fc-cache:
2016-05-29 23:47:46 +0900

/usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache
-frv

dyld: Library not loaded: /usr/local/opt/freetype/lib/libfreetype.6.dylib
  Referenced from: /usr/local/Cellar/fontconfig/2.11.1_2/bin/fc-cache
  Reason: image not found
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall fontconfig`
==> Summary
🍺  /usr/local/Cellar/fontconfig/2.11.1_2: 449 files, 2.9M
==> Installing gnuplot dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.6.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libtiff-4.0.6.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libtiff/4.0.6: 259 files, 3.4M
==> Installing gnuplot dependency: gd
==> Downloading https://homebrew.bintray.com/bottles/gd-2.1.1_2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gd-2.1.1_2.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/gd/2.1.1_2: 34 files, 1M
==> Installing gnuplot dependency: lua
==> Downloading https://homebrew.bintray.com/bottles/lua-5.2.4_3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring lua-5.2.4_3.el_capitan.bottle.tar.gz
==> Caveats
Please be aware due to the way Luarocks is designed any binaries installed
via Luarocks-5.2 AND 5.1 will overwrite each other in /usr/local/bin.

This is, for now, unavoidable. If this is troublesome for you, you can build
rocks with the `--tree=` command to a special, non-conflicting location and
then add that to your `$PATH`.
==> Summary
🍺  /usr/local/Cellar/lua/5.2.4_3: 82 files, 687.2K
==> Installing gnuplot
==> Downloading https://downloads.sourceforge.net/project/gnuplot/gnuplot/5.0.2/gnuplot-5.0.2.tar.gz
==> Downloading from http://tenet.dl.sourceforge.net/project/gnuplot/gnuplot/5.0.2/gnuplot-5.0.2.tar.gz
######################################################################## 100.0%
==> ./configure --disable-silent-rules --prefix=/usr/local/Cellar/gnuplot/5.0.2 --with-readline=/usr/local/opt/readline --with-
==> make
==> make install
==> Caveats
AquaTerm support will only be built into Gnuplot if the standard AquaTerm
package from SourceForge has already been installed onto your system.
If you subsequently remove AquaTerm, you will need to uninstall and then
reinstall Gnuplot.
==> Summary
🍺  /usr/local/Cellar/gnuplot/5.0.2: 44 files, 2.2M, built in 2 minutes 26 seconds
hiroshi-no-MacBook-Air:~ hiroshi$


これでインストールは完了です。

確認として、次のようになれば問題なしです。

hiroshi-no-MacBook-Air:~ hiroshi$ gnuplot

G N U P L O T
Version 5.0 patchlevel 2    last modified 2015-12-24

Copyright (C) 1986-1993, 1998, 2004, 2007-2015
Thomas Williams, Colin Kelley and many others

gnuplot home:     http://www.gnuplot.info
faq, bugs, etc:   type "help FAQ"
immediate help:   type "help"  (plot window: hit 'h')

Terminal type set to 'aqua'
gnuplot>


最後の行で、

gnuplot> plot sin(x)


と打てば、また別に画面が立ち上がり、

 

 

その他

このままでは、せっかくX11もインストールしたのに、出力先としてX11がつかえません。
本当はオプションを指定する際にX11も一緒に指定しなければならないようですが、間違ってAquatermだけ指定してしまいました。

改めてX11も指定する方法については、またの機会にします。。。

TeXのパッケージのインストール

前回は、最低限TeXが動くようにするところまでは行ったので、パッケージをインストールする方法について書きます。

とりあえず、bm.styを使えるようにすることを目標にします。

CTANからダウンロード

まずはCTANというTeXのパッケージなどをまとめているサイトへ行き、必要なファイルをインストールします。
検索Boxのところにキーワードを打ち込んで欲しいパッケージをまずは見つけます。今回は「bm」と打ちました。


検索をすると、このような画面になります。


「Package bm」のリンクをクリックします。


TDS archiveの行にある.zipファイルをクリックすればダウンロードされます。
それを解凍します。
今回の場合、解凍されてできるのは、latex-tools.tdsです。

styファイルの生成

styleファイル(拡張子が.styのファイル)は.insのファイルと.dtxのファイルから生成します。
今回の場合はbm.insとbm.dtxのファイルです。まずはこの二つのある場所を探します。
探してみると、latex-tools.tds→source→latex→toolsの中にあることがわかります。

カレントディレクトリをtoolsに移動します。(絶対パスで移動するなり、相対パスで地道に移動するなりして)

そこで、

hiroshi-no-MacBook-Air:tools hiroshi$ sudo latex bm.ins


とコマンドを打ちます。例によってパスワードを求められるので自分のPCのパスワードを入力します。
そうすると、以下のような処理が始まり、bm.styが生成されます。あとbm.logもできます。

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./bm.ins
LaTeX2e <2015>
Babel <3 9l=""> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/docstrip.tex
Utility: `docstrip' 2.5e <2014>
English documentation    <2014>

**********************************************************
* This program converts documented macro-files into fast *
* loadable files by stripping off (nearly) all comments! *
**********************************************************

********************************************************
* No Configuration file found, using default settings. *
********************************************************

)

Generating file(s) ./bm.sty 

Processing file bm.dtx (package) -> bm.sty
Lines  processed: 1679
Comments removed: 1262
Comments  passed: 5
Codelines passed: 403

 )
No pages of output.
Transcript written on bm.log.



もし、

sudo: latex: command not found


とかいうエラーがでたらそれはPATHが通っていません。

hiroshi-no-MacBook-Air:tools hiroshi$ PATH=/Library/TeX/texbin:$PATH


として「Library/Tex/texbin」にPATHを通してから再度実行しましょう。

生成したファイル類をまとめる。

生成に要した.insと.dtxと、生成された.styと.logを別フォルダにまとめます。

「/usr/local/texlive/texmf-local/tex/latex/local」の中に、新しいフォルダを作成します。
今回は「bm」という名前のフォルダにしておきます。
その中に、上記4つのファイルを移動させれば完了です。

これでbmというパッケージが使用できるようになります。

El'CapitanにMacTeXのインストール

最近、TeXで資料を作らなければならない状況があったため、TeX環境を入れました。

まあ過去にwindowsとかに入れたこともあったりするのですが、Macに入れる手順も自分の忘備録として残しておきたいと思います。

前提としている環境は以下の状況です。

  • Homebrewはインストール済み(まだインストールできていない場合はこちらから

    piroshhh.hatenablog.com

  • 該当の端末には初めてMacTeXをインストールする
  • OSはEl'Capitan(10.11.5)

 

Homebrew Caskのインストール

まずHomebrewの拡張であるHomebrewcaskをインストールします。
ターミナルを開き以下のようにコマンドを実行します。

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew tap caskroom/cask


sudoで実行しているので、パスワードを求められます。自分のパソコンにログインするときのパスワードを入力すると、以下のように処理が始まります。

==> Tapping caskroom/cask
Cloning into '/usr/local/Library/Taps/caskroom/homebrew-cask'...
remote: Counting objects: 3710, done.
remote: Compressing objects: 100% (3655/3655), done.
remote: Total 3710 (delta 63), reused 865 (delta 34), pack-reused 0
Receiving objects: 100% (3710/3710), 6.48 MiB | 880.00 KiB/s, done.
Resolving deltas: 100% (63/63), done.
Checking connectivity... done.
Tapped 1 formula (3,675 files, 15.2M)
hiroshi-no-MacBook-Air:~ hiroshi$


処理はすぐ終わるので、終わったらさらに次のようにコマンドを打ちます。

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew install caskroom/cask/brew-cask


そうすうと以下のようになります。

==> Installing brew-cask from caskroom/cask
==> Cloning https://github.com/caskroom/homebrew-cask.git
Cloning into '/Library/Caches/Homebrew/brew-cask--git'...
remote: Counting objects: 3408, done.
remote: Compressing objects: 100% (3369/3369), done.
remote: Total 3408 (delta 56), reused 528 (delta 18), pack-reused 0
Receiving objects: 100% (3408/3408), 5.88 MiB | 4.06 MiB/s, done.
Resolving deltas: 100% (56/56), done.
Checking connectivity... done.
Note: checking out 'b4c92f3658d47712aa6114fb29be50a763cf78ee'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b 

==> Checking out tag v0.60.1
==> Caveats
You must uninstall this formula. It is no longer needed to stay up to date,
as Homebrew now takes care of that automatically.
==> Summary
🍺  /usr/local/Cellar/brew-cask/0.60.1: 4 files, 7.6K, built in 11 seconds
hiroshi-no-MacBook-Air:~ hiroshi$

 

MacTeXのインストール

いまインストールしたHomebrewCaskを使ってMacTeXをインストールします。
同様にターミナルで次のように打ちます。

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew cask install mactex


つぎのような処理が始まります。

==> We need to make Caskroom for the first time at /opt/homebrew-cask/Caskroom
==> We'll set permissions properly so we won't need sudo in the future
==> Downloading http://tug.org/cgi-bin/mactex-download/MacTeX.pkg
######################################################################## 100.0%
==> Verifying checksum for Cask mactex
==> Running installer for mactex; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
==> installer: Package name is MacTeX-2015
==> installer: Installing at base path /
==> installer: The install was successful.
🍺  mactex staged at '/opt/homebrew-cask/Caskroom/mactex/20150613' (2.5G)
hiroshi-no-MacBook-Air:~ hiroshi$


特に問題なく完了すれば、Finderのアプリケーションディレクトリの中にTeXのフォルダができています。



で、そのTeXフォルダの中にはこんなものが入ってます。


TeXShopがいわゆる統合開発環境です。

PATHを通す

しかし、このままではまだTeXは動きません。

試しにTeXshopを起動し、こんな感じで打ってみます。


これで左上に表示されている「タイプセット」のボタンを押せばpdfファイルが出力されるはずなのですが、こんなエラーが表示されてしまいます。




どうやらいろいろ調べてみると、El'CapitanからPATHを通す場所が違うようです。

画面左上から「TeXShop→環境設定→内部設定」と進み、パス設定の中の「(pdf)TeX」を編集します。

デフォルトは、「/usr/texbin」になっているようですが、これを消して、「/Library/TeX/texbin」と入力します。


それで「OK」を押します。

それでもう一度「タイプセット」を押すと、無事pdfファイルが出力されます。されるはずです。されなかったらごめんなさい。

ちなみに、ターミナルのほうで直接PATHは通す必要はないようです。
シェルのコマンドでPATHからTeXに関するコマンドを削除しても、少なくとも上記環境設定でPATHが通っていれば大丈夫なようです。


最低限の記述はこれでできるようになりましたが、パッケージなどはまだ使えません。

パッケージのインストール方法は、これはこれでまた若干手順があるので、またの機会にします。