如何使用fromiter构造一个np.array [英] How to construct an np.array with fromiter
问题描述
我正在尝试通过从python生成器采样来构造np.array
,每次调用next
都会产生数组的一行.这是一些示例代码:
I'm trying to construct an np.array
by sampling from a python generator, that yields one row of the array per invocation of next
. Here is some sample code:
import numpy as np
data = np.eye(9)
labels = np.array([0,0,0,1,1,1,2,2,2])
def extract_one_class(X,labels,y):
""" Take an array of data X, a column vector array of labels, and one particular label y. Return an array of all instances in X that have label y """
return X[np.nonzero(labels[:] == y)[0],:]
def generate_points(data, labels, size):
""" Generate and return 'size' pairs of points drawn from different classes """
label_alphabet = np.unique(labels)
assert(label_alphabet.size > 1)
for useless in xrange(size):
shuffle(label_alphabet)
first_class = extract_one_class(data,labels,label_alphabet[0])
second_class = extract_one_class(data,labels,label_alphabet[1])
pair = np.hstack((first_class[randint(0,first_class.shape[0]),:],second_class[randint(0,second_class.shape[0]),:]))
yield pair
points = np.fromiter(generate_points(data,labels,5),dtype = np.dtype('f8',(2*data.shape[1],1)))
extract_one_class
函数返回数据的子集:属于一个类标签的所有数据点.我想将点设为np.array
和shape = (size,data.shape[1])
.目前,上面的代码段返回错误:
The extract_one_class
function returns a subset of data: all data points belonging to one class label. I would like to have points be an np.array
with shape = (size,data.shape[1])
. Currently the code snippet above returns an error:
ValueError: setting an array element with a sequence.
fromiter
的文档声称返回一维数组.还有一些人以前使用fromiter在numpy中构造记录数组(例如 http://iam .al/post/21116450281/numpy-is-my-homeboy ).
The documentation of fromiter
claims to return a one-dimensional array. Yet others have used fromiter to construct record arrays in numpy before (e.g http://iam.al/post/21116450281/numpy-is-my-homeboy).
假设我可以以这种方式生成数组时,我是否超出预期?还是我的numpy不太正确?
Am I off the mark in assuming I can generate an array in this fashion? Or is my numpy just not quite right?
推荐答案
您可以修改generate_points
以产生单个浮点数而不是np.arrays,使用np.fromiter
形成一维数组,然后使用.reshape(size, -1)
使其成为2D数组.
You could modify generate_points
to yield single floats instead of np.arrays, use np.fromiter
to form a 1D array, and then use .reshape(size, -1)
to make it a 2D array.
points = np.fromiter(
generate_points(data,labels,5)).reshape(size, -1)
这篇关于如何使用fromiter构造一个np.array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!