我需要一些库仑分子矩阵来完成机器学习任务。
库仑矩阵?这里有一个paper描述它
我找到了python包molml它有一个方法。但是我不知道如何将api仅用于单个分子。总的来说,他们提供的方法是用两个分子来调用的,为什么?
示例如何提供方法:
H2 = (['H', 'H'],
[[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0]])
HCN = (['H', 'C', 'N'],
[[-1.0, 0.0, 0.0],
[ 0.0, 0.0, 0.0],
[ 1.0, 0.0, 0.0]])
feat.transform([H2, HCN])
我需要这样的东西:
atomnames = [list of atomsymbols]
atomcoords = [list of [x,y,z] for the atoms]
coulombMatrice = CoulombMatrix((atomnames,atomcoords)
我还发现另一个lib(examples)承诺可以生成库仑矩阵,但是,我无法在windows上安装它,因为它依赖于linux-gcc-fortran编译器,为此我已经安装了cygwin和gcc-fortran。
谢谢你们,伙计们
最佳答案
我已经实现了自己的解决方案。还有很大的改进余地。例如,随机排序的库仑矩阵和一袋键仍然没有实现。
import numpy as np
def get_coulombmatrix(molecule, largest_mol_size=None):
"""
This function generates a coulomb matrix for the given molecule
if largest_mol size is provided matrix will have dimension lm x lm.
Padding is provided for the bottom and right _|
"""
numberAtoms = len(molecule.atoms)
if largest_mol_size == None or largest_mol_size == 0: largest_mol_size = numberAtoms
cij = np.zeros((largest_mol_size, largest_mol_size))
xyzmatrix = [[atom.position.x, atom.position.y, atom.position.z] for atom in molecule.atoms]
chargearray = [atom.atomic_number for atom in molecule.atoms]
for i in range(numberAtoms):
for j in range(numberAtoms):
if i == j:
cij[i][j] = 0.5 * chargearray[i] ** 2.4 # Diagonal term described by Potential energy of isolated atom
else:
dist = np.linalg.norm(np.array(xyzmatrix[i]) - np.array(xyzmatrix[j]))
cij[i][j] = chargearray[i] * chargearray[j] / dist # Pair-wise repulsion
return cij