且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中计算质心

更新时间:2022-12-09 18:39:14

首先,使用numpy的genfromtxt函数可以更轻松地读取文件.您不需要导入字符串,也不需要遍历所有行并添加文本或计算字符.

First of all, an easier way to read your file is with numpy's genfromtxt function. You don't need to import string, and you don't need to loop through all the lines and append text or count the characters.

from __future__ import division
import numpy as nx

data = nx.genfromtxt('file.pdb')

然后,可以通过以下方式访问最后三列:

Then, the last three columns can be accessed as:

data[:, -3:]

第一个:表示所有行",而-3:表示从第三到最后一列到最后一列.

Where the first : means "all rows", and -3: means from the third-to-last column to the last column.

因此,您可以对它们进行平均:

So, you can average them as such:

nx.mean(data[:,-3:], axis=0)

其中axis=0参数告诉nx.mean沿第一(0th)轴取平均值.看起来像这样:

where the axis=0 argument tells nx.mean to take the average along the first (0th) axis. It looks like this:

In : data[:,-3:]
Out: 
array([[ 27.13,   7.77,  34.39],
       [ 27.99,   7.76,  34.93],
       [ 27.16,   6.96,  33.79],
       [ 27.17,   8.58,  33.79],
       [ 25.94,   7.78,  35.25],
       [ 25.98,   9.09,  36.02],
       [ 26.74,  10.1 ,  35.32],
       [ 26.75,  10.94,  35.86],
       [ 24.64,   7.79,  34.46],
       [ 24.53,   8.51,  33.5 ],
       [ 23.59,   7.07,  34.76],
       [ 23.59,   6.55,  35.61],
       [ 22.42,   7.01,  33.9 ],
       [ 21.62,   5.76,  34.27],
       [ 22.48,   4.21,  33.97],
       [ 21.59,   8.22,  34.04],
       [ 21.37,   8.69,  35.16]])

In : np.mean(data[:,-3:], axis=0)
Out: array([ 24.74647059,   7.81117647,  34.64823529])


其他一些事情:

1)删除以下行:import math as mean,它将导入整个math模块并将其重命名为mean.您想要的是from math import mean,它从math模块导入mean函数.但是在代码中,无论如何,最终还是要使用numpy(nx)模块中的math函数,因此您从未使用过math版本.

1) remove this line: import math as mean, which imports the entire math module and renames it mean. What you intended was from math import mean which imports the mean function from the math module. But in your code, you end up using the math function from the numpy (nx) module anyway, so you never used the math version.

2)您的循环没有缩进,这意味着您要么错误地粘贴到***中,要么循环被错误地缩进了.可能这就是您的代码的实际外观:

2) your loop is not indented, which means you either pasted incorrectly into ***, or your loop is incorrectly indented. Possibly, this is what your code actually looks like:

for j in text:
    x1 = eval(replace(j[30:38], ' ', ''))         #extract x-coordinate
    y1 = eval(replace(j[38:46], ' ', ''))         #extract y-coordinate
    z1 = eval(replace(j[46:54], ' ', ''))         #extract z-coordinate

    idcord = []
    idcord.append(x1); idcord.append(y1); idcord.append(z1)

    centroid = nx.mean(idcord)
    print centroid

但是问题在于,每次循环时,idcord都会被设置为空列表 ,并为每个粒子计算一个新的质心.如果您如上所述一次全部导入数据文件,则甚至根本不需要循环.实际上,您的整个代码可以是:

But the problem is that idcord gets set to an empty list every time the loop goes through, and a new centroid is calculated, for each particle. You don't even need the loop at all if you import the data file all at once as above. In fact, your entire code can be:

from __future__ import division
import numpy as nx

data = nx.genfromtxt('file.pdb')
nx.mean(data[:,-3:], axis=0)