How to convert a python list to another data type in numpy?
Question:
Write a function that creates a numpy array with the float64 type based on an existing list
import numpy as np
def solution(lst):
arr = #your code here
return arr
I try a lot of ways:
import numpy as np
def solution(lst):
arr = np.array(lst)
arr = lst.astype(np.float64)
return arr
import numpy as np
def solution(lst):
arr = np.array(lst)
arr = np.astype(np.float64)
return arr
Answers:
A good question will have a minimal test case, and show the errors that you get with various tries.
Define a small list:
In [15]: lst = [1,2,3]
and your two attempts (note the correct indentation for the return
):
In [18]: def solution1(lst):
...: arr = np.array(lst)
...: arr = lst.astype(np.float64)
...: return arr
...: def solution2(lst):
...: arr = np.array(lst)
...: arr = np.astype(np.float64)
...: return arr
...:
First try:
In [19]: solution1(lst)
AttributeError: 'list' object has no attribute 'astype'
astype
is a method of a ndarray
(as the doc link in my comment shows). list
does not have such a method. In python, each class has a defined set of methods
. Just because one class has such a method, it does not mean that another has the same.
For the second:
In [21]: solution2(lst)
AttributeError: module 'numpy' has no attribute 'astype'
In [22]: np.astype
AttributeError: module 'numpy' has no attribute 'astype'
Again you create an arr
, but you try to use astype
as np
function. You aren’t specifying arr
at all in that line.
Make an array from the list:
In [23]: np.array(lst)
Out[23]: array([1, 2, 3])
With this list the dtype is int:
In [24]: np.array(lst).dtype
Out[24]: dtype('int32')
The resulting array has a astype
method:
In [25]: np.array(lst).astype(np.float64)
Out[25]: array([1., 2., 3.])
But np.array
also accepts a dtype
parameter:
In [26]: np.array(lst,dtype=np.float64)
Out[26]: array([1., 2., 3.])
If your list contains floats, you don’t need any further conversion:
In [27]: np.array([1,2,.3])
Out[27]: array([1. , 2. , 0.3])
In [28]: np.array([1,2,.3]).dtype
Out[28]: dtype('float64')
In other words, you should simply have written:
def solution(lst):
arr = np.array(lst)
arr = arr.astype(np.float64) # arr, not lst or np
return arr
Write a function that creates a numpy array with the float64 type based on an existing list
import numpy as np
def solution(lst):
arr = #your code here
return arr
I try a lot of ways:
import numpy as np
def solution(lst):
arr = np.array(lst)
arr = lst.astype(np.float64)
return arr
import numpy as np
def solution(lst):
arr = np.array(lst)
arr = np.astype(np.float64)
return arr
A good question will have a minimal test case, and show the errors that you get with various tries.
Define a small list:
In [15]: lst = [1,2,3]
and your two attempts (note the correct indentation for the return
):
In [18]: def solution1(lst):
...: arr = np.array(lst)
...: arr = lst.astype(np.float64)
...: return arr
...: def solution2(lst):
...: arr = np.array(lst)
...: arr = np.astype(np.float64)
...: return arr
...:
First try:
In [19]: solution1(lst)
AttributeError: 'list' object has no attribute 'astype'
astype
is a method of a ndarray
(as the doc link in my comment shows). list
does not have such a method. In python, each class has a defined set of methods
. Just because one class has such a method, it does not mean that another has the same.
For the second:
In [21]: solution2(lst)
AttributeError: module 'numpy' has no attribute 'astype'
In [22]: np.astype
AttributeError: module 'numpy' has no attribute 'astype'
Again you create an arr
, but you try to use astype
as np
function. You aren’t specifying arr
at all in that line.
Make an array from the list:
In [23]: np.array(lst)
Out[23]: array([1, 2, 3])
With this list the dtype is int:
In [24]: np.array(lst).dtype
Out[24]: dtype('int32')
The resulting array has a astype
method:
In [25]: np.array(lst).astype(np.float64)
Out[25]: array([1., 2., 3.])
But np.array
also accepts a dtype
parameter:
In [26]: np.array(lst,dtype=np.float64)
Out[26]: array([1., 2., 3.])
If your list contains floats, you don’t need any further conversion:
In [27]: np.array([1,2,.3])
Out[27]: array([1. , 2. , 0.3])
In [28]: np.array([1,2,.3]).dtype
Out[28]: dtype('float64')
In other words, you should simply have written:
def solution(lst):
arr = np.array(lst)
arr = arr.astype(np.float64) # arr, not lst or np
return arr