[英]calculate mean using numpy ndarray
The text file look like: 文本文件如下:
david weight_2005 50
david weight_2012 60
david height_2005 150
david height_2012 160
mark weight_2005 90
mark weight_2012 85
mark height_2005 160
mark height_2012 170
How to calculate mean of weight and height for david and mark as follows: 如何计算大卫的重量和高度的平均值并标记如下:
david>> mean(weight_2005 and weight_2012), mean (height_2005 and height_2012)
mark>> mean(weight_2005 and weight_2012), mean (height_2005 and height_2012)
my incomplete code is: 我的不完整代码是:
import numpy as np
import csv
with open ('data.txt','r') as infile:
contents = csv.reader(infile, delimiter=' ')
c1,c2,c3 = zip(*contents)
data = np.array(c3,dtype=float)
Then how to apply np.mean?? 然后如何申请np.mean ??
The mean
function is for computing the average of an array of numbers. mean
函数用于计算数字数组的平均值。 You will need to come up with a way to select the values of c3
by applying a condition to c2
. 您需要通过将条件应用于
c2
来选择c3
的值。
What would probably suit your needs better would be splitting up the data into a hierarchical structure, I prefer using dictionaries. 什么可能更适合您的需求将数据分成层次结构,我更喜欢使用词典。 Something like
就像是
data = {}
with open('data.txt') as f:
contents = csv.reader(f, delimiter=' ')
for (name, attribute, value) in contents:
data[name] = data.get(name, {}) # Default value is a new dict
attr_name, attr_year = attribute.split('_')
attr_year = int(attr_year)
data[name][attr_name] = data[name].get(attr_name, {})
data[name][attr_name][attr_year] = value
Now data
will look like 现在
data
看起来像
{
"david": {
"weight": {
2005: 50,
2012: 60
},
"height": {
2005: 150,
2012: 160
}
},
"mark": {
"weight": {
2005, 90,
2012, 85
},
"height": {
2005: 160,
2012: 170
}
}
}
Then what you can do is 那你可以做的是
david_avg_weight = np.mean(data['david']['weight'].values())
mark_avg_height = np.mean([v for k, v in data['mark']['height'].iteritems() if 2008 < k])
Here I'm still using np.mean
, but only calling it on a normal Python list. 在这里我仍然使用
np.mean
,但只在普通的Python列表上调用它。
I'll make this community wiki, because it's more "here's how I think you should do it instead" than "here's the answer to the question you asked". 我会创建这个社区wiki,因为它更“我认为你应该这样做”而不是“这就是你问的问题的答案”。 For something like this I'd probably use
pandas
instead of numpy
, as its grouping tools are much better. 对于像这样的东西,我可能会使用
pandas
而不是numpy
,因为它的分组工具要好得多。 It'll also be useful to compare with numpy
-based approaches. 与基于
numpy
的方法进行比较也很有用。
import pandas as pd
df = pd.read_csv("data.txt", sep="[ _]", header=None,
names=["name", "property", "year", "value"])
means = df.groupby(["name", "property"])["value"].mean()
.. and, er, that's it. ..而且,呃,就是这样。
First, read in the data into a DataFrame
, letting either whitespace or _
separate columns: 首先,将数据读入
DataFrame
,允许空格或_
分隔列:
>>> import pandas as pd
>>> df = pd.read_csv("data.txt", sep="[ _]", header=None,
names=["name", "property", "year", "value"])
>>> df
name property year value
0 david weight 2005 50
1 david weight 2012 60
2 david height 2005 150
3 david height 2012 160
4 mark weight 2005 90
5 mark weight 2012 85
6 mark height 2005 160
7 mark height 2012 170
Then group by name
and property
, take the value
column, and compute the mean: 然后按
name
和property
分组,获取value
列,并计算平均值:
>>> means = df.groupby(["name", "property"])["value"].mean()
>>> means
name property
david height 155.0
weight 55.0
mark height 165.0
weight 87.5
Name: value, dtype: float64
.. okay, the sep="[ _]"
trick is a little too cute for real code, though it works well enough here. ..好吧,
sep="[ _]"
技巧对于真正的代码来说有点太可爱了,虽然它在这里工作得很好。 In practice I'd use a whitespace separator, read in the second column as property_year
and then do 在实践中,我使用空格分隔符,在第二列中读取
property_year
然后执行
df["property"], df["year"] = zip(*df["property_year"].str.split("_"))
del df["property_year"]
to allow underscores in other columns. 允许其他列中的下划线。
You can read your data directly in a numpy array with: 您可以直接在numpy数组中读取数据:
data = np.recfromcsv("data.txt", delimiter=" ", names=['name', 'type', 'value'])
then you can find appropriate indices with np.where : 那么你可以用np.where找到合适的索引:
indices = np.where((data.name == 'david') * data.type.startswith('height'))
and perform the mean on thoses indices : 并在thoses指数上执行均值:
np.mean(data.value[indices])
If your data is always in the format provided. 如果您的数据始终采用提供的格式。 Then you could do this using array slicing:
然后你可以使用数组切片来做到这一点:
(data[:-1:2] + data[1::2]) / 2
Results in: 结果是:
[ 55. 155. 87.5 165. ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.