我有一个numpy数组
[['6.5' '3.2' '5.1' '2.0' 'Iris-virginica']
['6.1' '2.8' '4.0' '1.3' 'Iris-versicolor']
['4.6' '3.2' '1.4' '0.2' 'Iris-setosa']
['6.0' '2.2' '4.0' '1.0' 'Iris-versicolor']
['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
['6.7' '3.1' '5.6' '2.4' 'Iris-virginica']]
根据标签' Iris-virginica'
,'Iris-setosa'
和'Iris-virginica'
将这些数据分成3个单独的numpy数组的最快方法是什么?
Iris-virginica
数组仅包含
[['6.5' '3.2' '5.1' '2.0']['6.7' '3.1' '5.6' '2.4']]
Iris-setosa
数组仅包含[['4.6' '3.2' '1.4' '0.2'] ['4.7' '3.2' '1.3' '0.2']]
Iris-versicolor
数组仅包含[['6.1' '2.8' '4.0' '1.3']['6.0' '2.2' '4.0' '1.0']]
答案 0 :(得分:1)
使用numpy
并列出comprehension
,
import numpy as np
data = [['6.5', '3.2', '5.1', '2.0', 'Iris-virginica'],
['6.1', '2.8', '4.0', '1.3', 'Iris-versicolor'] ,
['4.6', '3.2', '1.4', '0.2', 'Iris-setosa'],
['6.0', '2.2', '4.0', '1.0', 'Iris-versicolor'],
['4.7', '3.2', '1.3', '0.2', 'Iris-setosa'],
['6.7', '3.1', '5.6', '2.4', 'Iris-virginica']]
filtered = [map(float, item[:4]) for item in data if item[4] == 'Iris-virginica']
print 'mean', np.mean(filtered, axis=0)
print 'var ', np.var(filtered, axis=0)
其中item[4] == 'Iris-virginica'
过滤了您想要的内容,map(float, item[:3])
适用于str
到float
,然后np.mean(..., axis=0)
将获得mean
过滤数据。
输出
mean [ 6.6 3.15 5.35]
var [ 0.01 0.0025 0.0625]
<强>更新强>
这里只有numpy
版本,但这似乎比上面慢。
data = np.array(data)
filtered = data[data[:, 4] == 'Iris-virginica'][:, :3].astype(np.float)
print 'mean', np.mean(filtered, axis=0)
print 'var ', np.var(filtered, axis=0)
timeit
结果
In [5]: %timeit filtered = [map(float, item[:4]) for item in data if item[4] == 'Iris-virginica']
100000 loops, best of 3: 1.93 µs per loop
In [6]: data = np.array(data)
In [7]: timeit data[data[:, 4] == 'Iris-virginica'][:, :4].astype(np.float)
100000 loops, best of 3: 15.5 µs per loop