简体   繁体   English

如何将numpy转换为float32?

[英]How to convert numpy to float32?

I have a data I want to split and convert it to float32 but it is showing the real number as a string我有一个data我想拆分并将其转换为float32但它以字符串形式显示实数

   data = open('Path dataset')
        for line in data:
        train = np.array([np.float32(x) for x in line.split(",")[:]])

And the error that showing me is:显示我的错误是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-83-53e8671c416d> in <module>
      1 for line in data_coba:
----> 2     train = np.array([np.float32(x) for x in line.split(",")[:]])          
      3 #print(train_test_coba)

<ipython-input-83-53e8671c416d> in <listcomp>(.0)
      1 for line in data_coba:
----> 2     train = np.array([np.float32(x) for x in line.split(",")[:]])           
      3 #print(train_test_coba)

ValueError: could not convert string to float: '50.89482266'

What is wrong with this?这有什么问题?

It seems that your dataset contains characters other than just comma-separated numbers.您的数据集似乎包含除逗号分隔数字以外的字符。 So the error is possibly occurring when it is trying to convert these non-numeric characters to float32.因此,当它尝试将这些非数字字符转换为 float32 时,可能会发生错误。 I suggest you check your dataset again and maybe try splitting it more.我建议你再次检查你的数据集,也许尝试更多地拆分它。

你必须在 open 函数中使用 encoding='utf-8'

data = open('Path dataset',encoding='utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM