Contenu connexe Similaire à R -> Python (20) Plus de Kazufumi Ohkawa (13) R -> Python2. 自己紹介
• kzfm (@fmkz___)
– blog.kzfmix.com
– Shizuoka.py
• とある製薬企業の研究員
• 日本酒とdrum’n’bass好き
• Python歴は6年くらい
– (その前はPerl)
• よく使うのはFlask, Pandas
11. Series(ベクトル)
>>> a = pd.Series(range(5), index=list(“abcde”)) # 0..5のリストにa..eのインデックス
>>> a[list(“ace”)] #indexアクセス
a 0
c 2
e 4
dtype: int64
>>> a[[0,2,4]] # 0,2,4番目の要素
a 0
c 2
e 4
dtype: int64
>>> a[(a<1)|(a>3)] #1より小さい、または3より大きい要素
a 0
e 4
dtype: int64
16. データフレームの結合
>>> x
0 1
0 1 0
1 -2 3
>>> pd.concat([x, x], axis=0) # rbind
0 1
0 1 0
1 -2 3
0 1 0
1 -2 3
>>> pd.concat([x, x], axis=1) # cbind
0 1 0 1
0 1 0 1 0
1 -2 3 -2 3
28. データ作成
• pychembldbを使います
• 出力をsyk.csvとして保存
– ChEMBL便利☆
from pychembldb import *
#Inhibition of recombinant Syk
#Bioorg. Med. Chem. Lett. (2009) 19:1944-1949
assay = chembldb.query(Assay).filter_by(chembl_id="CHEMBL1022010").one()
print '"ID","IC50","ALOGP","MWT"'
for act in assay.activities:
if act.standard_relation == "=":
print '"{}",{},{},{}'.format(act.compound.molecule.chembl_id,
act.standard_value,
act.compound.molecule.property.alogp,
act.compound.molecule.property.mw_freebase)
29. データはこんな感じ
ID IC50 ALOGP MWT pIC50
0 CHEMBL475575 4.0 1.99 426.47 8.397940
1 CHEMBL162 3.0 3.82 466.53 8.522879
2 CHEMBL473229 9.0 3.52 397.49 8.045757
3 CHEMBL475250 30.0 2.38 401.41 7.522879
4 CHEMBL475251 41.0 3.88 470.45 7.387216
5 CHEMBL515756 50.0 3.21 339.43 7.301030
6 CHEMBL105427 60.0 1.23 454.50 7.221849
7 CHEMBL30873 90.0 3.59 320.43 7.045757
8 CHEMBL474361 230.0 1.13 286.33 6.638272
9 CHEMBL515271 300.0 3.93 312.30 6.522879
10 CHEMBL474362 310.0 3.08 335.45 6.508638
11 CHEMBL443514 500.0 2.69 270.31 6.301030
12 CHEMBL105740 940.0 3.32 439.53 6.026872
13 CHEMBL470716 2000.0 3.84 304.37 5.698970
14 CHEMBL470717 3800.0 3.23 299.28 5.420216
30. pandasで読み込んでggplotで描画
import pandas as pd
from ggplot import *
import numpy as np
d = pd.read_csv("syk.csv")
d["pIC50"] = 9 - np.log10(d["IC50"])
p = ggplot(aes(x='MWT', y='ALOGP', color="pIC50", size="pIC50"), data=d) + geom_point()
#print p
ggsave("2dplot.png", p)
33. (おまけ)時系列データ
p = ggplot(aes(x='Date', y='nw'), data=d) +
geom_point(color='lightblue') +
stat_smooth(span=.15, color='black', se=True) +
ggtitle("Simple Diet") +
xlab("Date") +
ylab("Weight")
39. pychembldb
from pychembldb import *
#Inhibition of recombinant Syk
#Bioorg. Med. Chem. Lett. (2009) 19:1944-1949
assay = chembldb.query(Assay).filter_by(chembl_id="CHEMBL1022010").one()
for act in assay.activities:
if act.standard_relation == "=":
print act.compound.molecule.structure.molfile, "n$$$$"
先に使ったSYKのデータから
sdfを作っておく
41. PCA
from rdkit import Chem
from rdkit.Chem import AllChem, DataStructs
from sklearn.decomposition import PCA
from ggplot import *
import numpy as np
import pandas as pd
suppl = Chem.SDMolSupplier('syk.sdf')
fps = []
for mol in suppl:
fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2)
arr = np.zeros((1,))
DataStructs.ConvertToNumpyArray(fp, arr)
fps.append(arr)
Morganフィンガープリントを作って
Scikit-learnで扱えるように
NumpyArrayに変換
42. PCA
pca = PCA(n_components=2)
pca.fit(fps)
v = pca.components_
d = pd.DataFrame(v).T
d.columns = ["PCA1", "PCA2"]
g = ggplot(aes(x="PCA1", y="PCA2"), data=d) + geom_point(color="lightblue") + xlab("PCA1") +
ylab("PCA2")
ggsave("pca.png", g)
PCAで第二主成分まで計算して、
Xに第一、Yに第二主成分をプロット
45. RandamForest
from rdkit import Chem
from rdkit.Chem import AllChem, DataStructs
from sklearn import cross_validation
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import pandas as pd
d = pd.read_csv("syk.csv")
d["pIC50"] = 9 - np.log10(d["IC50"])
d["ACT"] = d.pIC50.apply(lambda x: 1 if x > 8 else 0)
先に使ったSYKのデータpIC50をもとめて
8オーダーより強いものを活性ありとした
(0:活性あり、1:活性なし)
46. RandamForest
suppl = Chem.SDMolSupplier('syk.sdf')
fps = []
for mol in suppl:
fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2)
arr = np.zeros((1,))
DataStructs.ConvertToNumpyArray(fp, arr)
fps.append(arr)
RDKitでsdfを読み込みMorganFingerprint
を計算し、それをScikit-learnで使えるように
NumpyArrayに変換
47. RandamForest
x_train, x_test, y_train, y_test = cross_validation.train_test_split(fps, d, test_size=0.4,
random_state=0)
print x_train.shape, y_train.shape
print x_test.shape, y_test.shape
rf = RandomForestClassifier(n_estimators=100, random_state=1123)
rf.fit(x_train, y_train[:,5])
print "predictn", rf.predict(x_test)
print "nresultn", y_test[:,5]
#print y_test[:,[0,5]]
データを訓練、テストセットにわけ、
RandamForestでモデルをつくり
テスト
48. Result
predict
[0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
result
[0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]