ndarray.resize: passing the correct value for the refcheck argument

前端 未结 2 1822
逝去的感伤
逝去的感伤 2021-01-22 21:58

Like many others, my situation is that I have a class which collects a large amount of data, and provides a method to return the data as a numpy array. (Additional data can con

2条回答
  •  无人共我
    2021-01-22 22:16

    I will use array.array() to do the data collection:

    import array
    a = array.array("d")
    for i in xrange(100):
        a.append(i*2)
    

    Every time when you want to do some calculation with the collected data, convert it to numpy.ndarray by numpy.frombuffer:

    b = np.frombuffer(a, dtype=float)
    print np.mean(b)
    

    b will share data memory with a, so the convertion is very fast.

提交回复
热议问题