问题描述
有人可以找出下面的代码有什么问题吗?
Can someone find out what is wrong with the code below?
import numpy as np
data = np.recfromcsv("data.txt", delimiter=" ", names=['name', 'types', 'value'])
indices = np.where((data.name == 'david') * data.types.startswith('height'))
mean_value = np.mean(data.value[indices])
我要计算大卫的体重和身高的平均值,并标记如下:
I want 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)
从文本(data.txt)文件中:
From the text (data.txt) file:
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
我正在使用python 3.2和numpy 1.8
I am using python 3.2 and numpy 1.8
上面的代码提供了如下类型错误:
The above code provides the type error as follows:
TypeError: startswith first arg must be bytes or a tuple of bytes, not numpy.str_
推荐答案
使用Python3.2和numpy 1.7,此行有效
With Python3.2 and numpy 1.7, this line works
indices = np.where((data.name == b'david') * data.types.startswith(b'height'))
data
显示为:
rec.array([(b'david', b'weight_2005', 50),...],
dtype=[('name', 'S5'), ('types', 'S11'), ('value', '<i4')])
type(data.name[0])
是<class 'bytes'>
.
b'height'
也可以在Python2.7中使用.
b'height'
works in Python2.7 as well.
另一种选择是将所有数据转换为unicode(Python 3字符串)
another option is to convert all the data to unicode (Python 3 strings)
dtype=[('name','U5'), ('types', 'U11'), ('value', '<i4')]
dataU=data.astype(dtype=dtype)
indices = np.where((dataU.name == 'david') * dataU.types.startswith('height'))
或
data = np.recfromtxt('data.txt', delimiter=" ",
names=['name', 'types', 'value'], dtype=dtype)
看起来recfromcsv
不需要dtype
,但是recfromtxt
可以.
It looks like recfromcsv
does not take a dtype
, but recfromtxt
does.
这篇关于在numpy中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!