0

I am using a package that is fetching values from a csv file for me. If I print out the result I get ['0.12' '0.23']. I checked the type, which is <class 'numpy.ndarray'> I want to convert it to a numpy array like [0.12, 0.23]. I tried np.asarray(variabel) but that did not resolve the problem.

MrYouMath
  • 547
  • 2
  • 13
  • 34
  • 1
    They are the same thing? `a = np.array([1, 2, 3]); print(type(a))`. Yours just happens to contain strings. Just do `a = a.astype(np.float64)`. You haven't stated what the problem is. – roganjosh Dec 20 '18 at 15:23
  • @roganjosh: Thank you for your comment, but `a = a.astype(np.float64)` does not add the comma. It generates `[1 2 3]` – MrYouMath Dec 20 '18 at 15:26
  • 2
    What is the issue though? That's entirely visual; lists don't physically contain commas either, it's just so that you can see individual elements. You already _have_ a normal numpy array, there is no conversion to actually do other than convert the type. The only other thing I can suggest is `a.tolist()` after the conversion but.... well, that's no longer an array. – roganjosh Dec 20 '18 at 15:27
  • @roganjosh: If I want to calculate with it it gives me an error `TypeError: only size-1 arrays can be converted to Python scalars` – MrYouMath Dec 20 '18 at 15:29
  • 2
    Then you're completely misdiagnosing your issue and the necessary information is missing from the question to solve the real problem. – roganjosh Dec 20 '18 at 15:29
  • @roganjosh: You were right! Thank you for your help. If you answer this question I would accept your answer, because you were the first to answer it in the comment. – MrYouMath Dec 20 '18 at 15:31
  • I'm not actually sure what issue I solved. I have left a dupe target; if that answers your question then you can unilaterally accept that dupe and it gives all the detail needed for future readers. Otherwise, feel free to write your own answer. – roganjosh Dec 20 '18 at 15:32
  • I don't think https://stackoverflow.com/questions/15879315/what-is-the-difference-between-ndarray-and-array-in-numpy answers this question. Most of the answers go down a rabbit-trail of when to use `np.array` versus `np.ndarray` constructors. I'm reopening so someone can write a answer that builds on the comments. – hpaulj Dec 20 '18 at 17:55

2 Answers2

0

Solution

import numpy as np
array = array.astype(np.float)


# If you are just initializing array you can do this
ar= np.array(your_list,dtype=np.float)
Martin
  • 3,333
  • 2
  • 18
  • 39
0

It might help to know how the csv was read. But for what ever reason it appears to have created a numpy array with a string dtype:

In [106]: data = np.array(['0.12', '0.23'])
In [107]: data
Out[107]: array(['0.12', '0.23'], dtype='<U4')
In [108]: print(data)
['0.12' '0.23']

The str formatting of such an array omits the comma, the repr display keeps it.

A list equivalent also displays with comma:

In [109]: data.tolist()
Out[109]: ['0.12', '0.23']

We call this a numpy array, but technically it is of class numpy.ndarray

In [110]: type(data)
Out[110]: numpy.ndarray

It can be converted to an array of floats with:

In [111]: data.astype(float)
Out[111]: array([0.12, 0.23])

It is still a ndarray, just the dtype is different. You may need to read more in the numpy docs about dtype.

The error:

If I want to calculate with it it gives me an error TypeError: only size-1 arrays can be converted to Python scalars

has a different source. data has 2 elements. You don't show the code that generates this error, but often we see this in plotting calls. The parameter is supposed to be a single number (often an integer), where as your array, even with a numeric dtype) is two numbers.

hpaulj
  • 221,503
  • 14
  • 230
  • 353