You may want to use the ndarray.item method, as in a.item(). This is also equivalent to (the now deprecated) np.asscalar(a). This has the benefit of working in situations with views and superfluous axes, while the above solutions will currently break. For example,
>>> a = np.asarray(1).view()
>>> a.item() # correct
1
>>> a[0] # breaks
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array
>>> a = np.asarray([[2]])
>>> a.item() # correct
2
>>> a[0] # bad result
array([2])
This also has the benefit of throwing an exception if the array is not actually a scalar, while the a[0] approach will silently proceed (which may lead to bugs sneaking through undetected).
>>> a = np.asarray([1, 2])
>>> a[0] # silently proceeds
1
>>> a.item() # detects incorrect size
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: can only convert an array of size 1 to a Python scalar
Answer from Aaron Voelker on Stack OverflowYou may want to use the ndarray.item method, as in a.item(). This is also equivalent to (the now deprecated) np.asscalar(a). This has the benefit of working in situations with views and superfluous axes, while the above solutions will currently break. For example,
>>> a = np.asarray(1).view()
>>> a.item() # correct
1
>>> a[0] # breaks
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array
>>> a = np.asarray([[2]])
>>> a.item() # correct
2
>>> a[0] # bad result
array([2])
This also has the benefit of throwing an exception if the array is not actually a scalar, while the a[0] approach will silently proceed (which may lead to bugs sneaking through undetected).
>>> a = np.asarray([1, 2])
>>> a[0] # silently proceeds
1
>>> a.item() # detects incorrect size
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: can only convert an array of size 1 to a Python scalar
Just access the first item of the list/array, using the index access and the index 0:
>>> list_ = [4]
>>> list_[0]
4
>>> array_ = np.array([4])
>>> array_[0]
4
This will be an int since that was what you inserted in the first place. If you need it to be a float for some reason, you can call float() on it then:
>>> float(list_[0])
4.0
python - Convert array element to float - Stack Overflow
Convert list or numpy array of single element to float in python
Convert elements within an array into floats in python - Stack Overflow
numpy - How to convert singleton array to a scalar value in Python? - Stack Overflow
How do you convert to float in Python?
Can you convert NumPy array to list in Python?
How do I convert a NumPy array to a list?
Your object monteCarloPerf is a one dimensional array containing elements of one dimensional arrays, or a list of lists. In order to access the value of the first element of the object you have to change your access to that element to the following:
finalValue = monteCarloPerf[arrayValue][0]
In fact, that is a 'TWO dimensional' array.
To get the float value you can do the following:
finalValue = monteCarloPerf[arrayValue][0]
Or you can transform the two dimensional array to a one dimensional array:
one_dim = [item[0] for item in monteCarloPerf]
I hope this helps.
Here's one pretty simple way:
>>> [map(float, x) for x in A1]
[[1.0, 4.0], [2.0, 5.0], [3.0, 6.0], [1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
I like it's because it's short (some would say terse) and since using map() makes it not hardcode or be explicit about the expected format of each x, it just says that it assumes A1 to be a list of sequences.
I have no idea how this compares performance-wise to other solutions (such as the more explicit [(float(x), float(y) for (x, y) in A1] seen below).
Each element of A1 is a tuple ('1.000000', '4.000000'). You will have to convert each item of the tuple:
A2 = [(float(i), float(j)) for (i, j) in A1]
You can use the item() function:
Use with AIimport numpy as np
matrix = np.array([[[[7]]]])
print(matrix.item())
Output
Use with AI7
Numpy has a function explicitly for this purpose: asscalar
Use with AI>>> np.asscalar(np.array([24]))
24
This uses item() in the implementation.
I guess asscalar was added to more explicit about what's going on.
Update:
As of 2023 this function has been removed from NumPy (see v1.23 release notes).
Try numpy.ndarray.astype(float). Read more here.
NOTE: This is not an in place operation so remember to set it back to a variable
import numpy as np
arr = np.array([["5","6"],
["3","4.5"]])
arr = arr.astype(float) #<---
print(arr)
print(type(arr[0,0])) #printing type of a single element in the matrix
array([[5. , 6. ],
[3. , 4.5]])
<class 'numpy.float64'>
Here is a way to do it inplace without extra lib/module:
ll = [["5","6"], ["3","4.5"]]
def transform(ll):
for i in range(len(ll)):
if type(ll[i])==str:
ll[i]=float(ll[i])
elif type(ll[i])==list:
transform(ll[i])
return ll
print(transform(ll))
output is:
[[5.0, 6.0], [3.0, 4.5]]