将数据分成3个类的最佳方法

时间:2014-05-08 14:27:28

标签: python python-2.7 python-3.x numpy pandas

我有一个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']]

1 个答案:

答案 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])适用于strfloat,然后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