Programing

2d numpy 배열을 목록 목록으로 변환

lottogame 2020. 10. 22. 07:37
반응형

2d numpy 배열을 목록 목록으로 변환


이 질문에 이미 답변이 있습니다.

numpy 배열을 지원하지 않고 튜플, 목록 및 사전 만 지원 하는 외부 모듈 ( libsvm )을 사용합니다 . 하지만 내 데이터는 2d numpy 배열에 있습니다. 루프없이 파이썬 방식으로 어떻게 변환 할 수 있습니까?

>>> import numpy
>>> array = numpy.ones((2,4))
>>> data_list = list(array)
>>> data_list
[array([ 1.,  1.,  1.,  1.]), array([ 1.,  1.,  1.,  1.])]

>>> type(data_list[0])
<type 'numpy.ndarray'>  # <= what I don't want

# non pythonic way using for loop
>>> newdata=list()
>>> for line in data_list:
...     line = list(line)
...     newdata.append(line)
>>> type(newdata[0])
<type 'list'>  # <= what I want

>>> import numpy
>>> a = numpy.ones((2,4))
>>> a
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
>>> a.tolist()
[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
>>> type(a.tolist())
<type 'list'>
>>> type(a.tolist()[0])
<type 'list'>

참고 URL : https://stackoverflow.com/questions/9721884/convert-2d-numpy-array-into-list-of-lists

반응형