AbuOmair
AbuOmair

Reputation: 153

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

Upvotes: 0

Views: 528

Answers (2)

davidbilla
davidbilla

Reputation: 2222

You have to use encoding='utf-8' in the open function

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

Upvotes: 0

Arijit Gupta
Arijit Gupta

Reputation: 73

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. I suggest you check your dataset again and maybe try splitting it more.

Upvotes: 1

Related Questions