python - initialize objects in numpy matrix -
i have numpy matrix filled unique objects. creating list of list , converting numpy array (see code below, under workaround). using because want use slicing access elements in matrix
i wondering if there better way create such matrix.
import random import numpy np class randomcell(object): def __init__(self): self.value = random.randint(0, 10) def __repr__(self): return str(self.value) # workaround temp_matrix = [[randomcell() row in range(3)] col in range(3)] workaround_matrix = np.array(temp_matrix) edit: want create matrix of objects not generate matrix of random numbers
your method of building array list of lists fine. option be
arr = np.array([randomcell() item in range(9)]).reshape(3,3) usually, save memory use np.fromiter build array iterator. however, since array has dtype object, unfortunately np.fromiter not option in case.
Comments
Post a Comment