본문 바로가기
파이썬

파이썬 배열을 부동 파이썬으로 변환 할 수 없습니다.

by º기록 2021. 1. 9.
반응형

대답이 쉽게 설명 될 것 같은 문제가 있습니다. 배열 요소를 수레로 변환하는 데 어려움을 겪고 있습니다 (곱하기, 추가 등)

import csv
import os
import glob
import numpy as np

def get_data(filename):
    with open(filename, 'r') as f:
        reader = csv.reader(f)                      
        return list(reader)

all_data = []

path=raw_input('What is the directory?')       
for infile in glob.glob(os.path.join(path, '*.csv')):
     all_data.extend(get_data(infile))
a = np.array(all_data)
current_track_data=a[0:,[8,9,10,11,12]]
abs_track_data=a[0:,7]

그리고 오류가 발생합니다.

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) C:\Users\AClayton\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.0.3.1262.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:
    175                 filename = fname
--> 176             exec compile(scripttext, filename, 'exec') in glob, loc
    177     else:
    178         def execfile(fname, *where):
> 
> C:\Users\AClayton\Current\python begin\code_tester2.py in <module>()
>      18 for infile in glob.glob(os.path.join(path, '*.csv')): # performs loop for each file in the specified path with extension .csv
>      19      all_data.extend(get_data(infile))
> ---> 20      a = np.ndarray(all_data, dtype=float)
>      21 
>      22      current_track_data=a[0:,[8,9,10,11,12]]
> 
> ValueError: sequence too large; must be smaller than 32

 

해결 방법

 


배열은 array , zeros 또는 empty 를 사용하여 구성해야합니다 ( See Also section below). The parameters given here refer to a 배열을 인스턴스화하기위한 저수준 메서드 ( ndarray (...) ).

따라서 라인 # 20을 다음과 같이 변경하십시오.

 a = np.array(all_data, dtype=float)

그리고 당신은 괜찮을 것입니다.

ndarray 가 생성 될 배열의 형태로 첫 번째 입력을 받기 때문에 발생하는 오류입니다. 내 Windows 시스템에서 32로 설정된 차원 수에 하 코딩 된 제한이 있습니다 (확실하지 않은 플랫폼에 따라 다를 수 있음). all_data 목록에 32 개가 넘는 항목 (또는 시스템에있는 값)이 있으며 치수 크기로 잘못 해석되어 오류가 발생합니다.

 

참조 페이지 https://stackoverflow.com/questions/18275720

 

 

반응형

댓글