无法将数组转换为浮点数python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18275720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:21:36  来源:igfitidea点击:

Cannot convert array to floats python

pythonarrayscsvnumpyfloating-point

提问by Ashleigh Clayton

I'm having a problem that seems like the answer would be easily explained. I'm struggling to convert my array elements to floats (so that I can multiply, add on them etc)

我遇到了一个问题,似乎答案很容易解释。我正在努力将我的数组元素转换为浮点数(以便我可以进行乘法、相加等)

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]

and I get the error:

我得到错误:

> --------------------------------------------------------------------------- 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

采纳答案by Jaime

Your script is not the same as the code you've posted... As the traceback of your error shows, in line 20 you are calling np.ndarray. That's the numpy array object, not the np.arrayfactory function. Unless you know very well what you are doing, follow the docs advice and:

您的脚本与您发布的代码不同...正如错误的回溯所示,在第 20 行中,您正在调用np.ndarray. 那是numpy 数组对象,而不是np.array工厂函数。除非您非常清楚自己在做什么,否则请遵循文档建议并:

Arrays should be constructed using array, zerosor empty(refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.

应使用array,zerosempty(请参阅下面的另请参阅部分)构造数组。此处给出的参数是指ndarray(...)用于实例化数组的低级方法 ( )。

So change your line #20 to:

因此,将您的第 20 行更改为:

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

and you should be fine.

你应该没事的。

The error you are getting comes because ndarraytakes your first input as the shape of the array to be created. There is a harcoded limit to the number of dimensions set to 32 on my Windows system (may be platform dependent, not sure). Your all_datalist has more than 32 entries (or whatever that value is in your system), misinterpreted as sizes of dimensions, and that's what triggers the error.

您遇到的错误是因为ndarray将您的第一个输入作为要创建的数组的形状。在我的 Windows 系统上设置为 32 的维度数有一个硬编码限制(可能取决于平台,不确定)。您的all_data列表有超过 32 个条目(或系统中的任何值),被误解为维度大小,这就是触发错误的原因。