Numpy asarray or np.asarray() allows you to convert python data structures into NumPy array. You can convert list, list of list, tuple, or list of tuples into NumPy array. We convert python data structure to NumPy array for fast computation and to increase efficiency.
In this entire tutorial, you will know the various examples of using numpy.asarray() in python.
Examples on Numpy asarray() or np.asarray()
In this section, you will know how to use and convert list, tuples, list of list, list of tuples e.t.c into NumPy array.
Example 1: Converting a list to a Numpy array
Suppose I have a python list and want to convert it into a NumPy array. Then I have to pass the list as an argument to the np.asarray() method. Execute the below lines of code.
import numpy as np python_list = [1,2,3,4,5] list_to_numpy_arr = np.asarray(python_list) print(list_to_numpy_arr) print(type(list_to_numpy_arr))
You can see the output type of the list is the NumPy array.
Example 2: Convert List of the lists to Numpy array
Now, let’s convert a list of lists to a NumPy array. In the above example, you have got a single-dimensional NumPy array. But here you will convert a two-dimensional NumPy array. Let’s execute the code and see the output.
import numpy as np python_list_of_list = [[1,2],[3,4],[5,6]] list_of_list_to_numpy_arr = np.asarray(python_list_of_list) print(list_of_list_to_numpy_arr) print(type(list_of_list_to_numpy_arr))
You can see the output of the list of lists is the multi-dimensional NumPy array.
Example 3: Numpy array from tuples using numpy asarray
In the above example, I have converted list or list of list to NumPy array, lets’s apply the np.asarray() method on tuples. Just execute the given lines of code and see the output.
import numpy as np python_tuple = (10,20,30,40,50) tuple_to_numpy_arr = np.asarray(python_tuple) print(tuple_to_numpy_arr) print(type(tuple_to_numpy_arr))
Example 4: Converting a list of tuples to a Numpy array
In the last example, you will convert the list of tuples to the NumPy array. Here the output will be the multi-dimensional array. Copy, paste and execute the code.
import numpy as np python_list_of_tuple = [(10,20),(30,40),(50,60)] list_of_tuple_to_numpy_arr = np.asarray(python_list_of_tuple) print(list_of_tuple_to_numpy_arr) print(type(list_of_tuple_to_numpy_arr))
You will get the following output.
In all the above examples you can convert the data type of the elements of the array by passing the dtype as an argument inside the np.asarray() function. For example, if you are converting a list of integers to a NumPy array of float elements then you will use np.asarray(your_list,dtype=np.float32).
import numpy as np python_list = [1,2,3,4,5] list_to_numpy_arr = np.asarray(python_list,dtype=np.float32) print(list_to_numpy_arr) print(type(list_to_numpy_arr))
[1. 2. 3. 4. 5.] <class 'numpy.ndarray'>
Numpy asarray allows you to convert any simple data structures into a NumPy array. After the conversion, you can easily manipulate the output array. These are the examples I have aggregated for you. I hope these examples have solved your queries. If you want to get more information on it then you can contact us for more help.
Question: No module named np error
Many readers have asked me, they are getting ModuleNotFoundError error. The error ModuleNotFoundError: No module named np comes when you have not installed NumPy in your system. To solve this issue you have to install NumPy in your system. You can also read the dedicated post on solve No module named np or NumPy error.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.