xtl*_*tlc 1 python arrays loops mean
我仍然在调整编写代码的"更多pythonian方式"时遇到麻烦...现在我正在迭代一些值(x).我有很多数组,我总是比较所有数组的第一个值,第二个值......很快:数组中所有条目的平均值按数组中的位置.
sum_mean_x = []
for i in range(0, int_points):
for j in range(0, len(x)):
mean_x.append(x[j][i])
sum_mean_x.append(sum(mean_x)/len(x))
mean_x = []
Run Code Online (Sandbox Code Playgroud)
我很确定可以做得超级漂亮.我知道我可以将第二行改成类似sum_mean_x.append(mean_x.mean)但是,我想我会错过一些严肃的魔法.
使用numpy包进行数值处理.假设您在普通Python中有以下三个列表:
a1 = [1., 4., 6.]
a2 = [3., 7., 3.]
a3 = [2., 0., -1.]
Run Code Online (Sandbox Code Playgroud)
并且您希望获得每个职位的平均值.将向量排列在一个数组中:
import numpy as np
a = np.array([a1, a2, a3])
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样得到每列的意思:
>>> a.mean(axis=0)
array([ 2. , 3.66666667, 2.66666667])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |