RDKit一个用于化学信息学的python库。使用支持向量回归(SVR)来预测logP。 分子的输入结构特征是摩根指纹,输出是logP。
代码示例:
#导入依赖库
import numpy as np
from rdkit import Chem
from rdkit.Chem.Crippen import MolLogP
from rdkit import Chem, DataStructs
from rdkit.Chem import AllChem
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error, r2_score
from scipy import stats
import matplotlib.pyplot as plt
载入smile分子库,计算morgan指纹和logP
num_mols = 5000
f = open('smiles.txt', 'r')
contents = f.readlines()
fps_total = []
logP_total = []
for i in range(num_mols):
smi = contents[i].split()[0]
m = Chem.MolFromSmiles(smi)
fp = AllChem.GetMorganFingerprintAsBitVect(m,2)
arr = np.zeros((1,))
DataStructs.ConvertToNumpyArray(fp,arr)
fps_total.append(arr)
logP_total.append(MolLogP(m))
fps_total = np.asarray(fps_total)
logP_total = np.asarray(logP_total)
划分训练集和测试集
num_total = fps_total.shape[0]
num_train = int(num_total*0.8)
num_total, num_train, (num_total-num_train)
fps_train = fps_total[0:num_train]
logP_train = logP_total[0:num_train]
fps_test = fps_total[num_train:]
logP_test = logP_total[num_train:]
将SVR模型用于回归模型
https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html
_gamma = 5.0
clf = SVR(kernel='poly', gamma=_gamma)
clf.fit(fps_train, logP_train)
完成训练后,应该检查预测的准确性。对于评估,将使用r2和指标的均方误差。
logP_pred = clf.predict(fps_test)
r2 = r2_score(logP_test, logP_pred)
mse = mean_squared_error(logP_test, logP_pred)
r2, mse
模型结果可视化
slope, intercept, r_value, p_value, std_error = stats.linregress(logP_test, logP_pred)
yy = slope*logP_test+intercept
plt.scatter(logP_test, logP_pred, color='black', s=1)
plt.plot(logP_test, yy, label='Predicted logP = '+str(round(slope,2))+'*True logP + '+str(round(intercept,2)))
plt.xlabel('True logP')
plt.ylabel('Predicted logP')
plt.legend()
plt.show()
参考:
https://github.com/SeongokRyu/CH485---Artificial-Intelligence-and-Chemistry
https://blog.csdn.net/zb123455445/article/details/78354489