Learn to create NumPy arrays from lists or tuples in this video tutorial by Charles Kelly. These are explained in the context of computer science and data science to technologists and students in preparation for machine learning, applied statistics, neura
- [Narrator] File Open when you're creating arrays from Python Structures File, by clicking on its file name. This will open a new tab in your browser. Your basic data structure with a NumPy is an array. In this video, we'll learn several methods for creating arrays. We'll begin by using a convention that is common among NumPy users. We'll re-name the NumPy name space to NT. This will permit us to pre-fix NumPy functions and methods and attributes, with NP, instead of typing n-u-n-p-y.
To do this, we'll type "import numpy as np." We'll execute this cell by pressing Shift and Enter. If you don't receive an error message, then you'll know that NumPy was imported correctly. There are situations where it's important, that you know which version of NumPy you are using. This is easy to accomplish. You can begin with Python's Print function, and you can access NumPy's Version attribute by typing np, since we re-named the name space from NumPy to np.
np dot underscore underscore, version underscore underscore. And again I emphasize, that each of these underscores are two consecutive underscores. The result of executing this cell, is, in this case, version 1.11.1. I'm recording this course in the summer of 2016. If you're viewing this course at some time in the future, the version number may have increased. The simplest way to create a NumPy array, is to start with a Python list.
I'll create a Python list, and I'll give it the name "My List." To create a list in Python, you use square brackets. Within the square brackets, I'll create five elements. The first element is -17, the second element is zero, the third element is four, the fourth element is five, and the final element is nine. As you can see within this list, as is common with almost every data structure within Python, the separator between elements within a data structure, is a comma.
In order to create a NumPy array, from a Python data structure, we use NumPy's array function. We can access this by typing "np.array" and give the name of our data structure as a parameter to the array function. Finally, we can display the contents of this array, by using the Notebook feature, typing "my", pressing the Tab key, and selecting among the choices.
Now I'm going to press Shift Enter, and we'll see the contents of our array. This tells us that, in contrast to Python's list, we now have a NumPy array with the same elements that were contained in the list that we created in Python. The first thing that we can do with any NumPy array is perform parenthetic operations. These operations are performed element by element. For example, if I want to multiply every element in the list by ten, I can simply type the name of the NumPy array, the operator, in this case multiplication, the multiplicand, which is ten, and the results are, not surprisingly, 170, zero, 40, 50, and 90.
In the next example, I'll create a NumPy array, from a Python tuple. In Python, to create a tuple, you use round brackets for parenthesis. In this case, our tuple has three elements. An integer, a floating point number, and a complex number. Again, I can use the NumPy array function, to create a NumPy array from this particular couple. If I type numpy.array, task my tuple, as a parameter to this function, I'll see the results.
An interesting thing occurred in this example. Each of the elements in our tuple was promoted to the final type within the array. You'll notice that the integer 14, was promoted to a complex number, which has 14 as a floating point number as its real part, and has zero as its imaginary, or complex, part. Our floating point number, remains a floating point number, with a zero as imaginary part.
And our integer five, is promoted to 5.00, a floating point number, and seven, as its complex part. Promotion of data types within NumPy, is an important consideration. Within NumPy, every element within an array has to have the same type. Since an integer can't accommodate a complex number or a floating point number, a floating point number can't accommodate a complex number. We need complex numbers for every element within our array.
Therefore, our original integer, floating point, and complex numbers, were all promoted to complex numbers. I'm going to create a header, that indicates the difference between Python data structures, and NumPy data structures. To do this, I'm going to create mark-down code. The easiest way to create mark-down code, is to use the Escape key, and the M key. This now indicates that this code will not be executed as Python, but will be executed as mark-down.
We'll start with three hashtags. Type the "difference between python an numpy and data structures." The important difference is the way that tuples and arrays are promoted, and then used in mathematical operations. For example, if I type "my tuple," and I use an asterisk, and I execute this code, you'll see that what happens, is the tuple, in this case a Python tuple, was replicated six times.
On the other hand, if I take my array NumPy tuple, which is again, a NumPy array, and I multiply this by six, you'll see that each element in the tuple was multiplied by six. When we use the asterisk with a Python tuple, we're using the asterisk as a replicate operator. When we use the asterisk with a NumPy array, we're using it as a multiplication operator.
The point to remember, is that the operator and its impact will vary, depending upon the type of data structure that you're using. This is an example of object-oriented design, within both NumPy and Python.
- Using Jupyter Notebook
- Creating NumPy arrays from Python structures
- Slicing arrays
- Using Boolean masking and broadcasting techniques
- Plotting in Jupyter notebooks
- Joining and splitting arrays
- Rearranging array elements
- Creating universal functions
- Finding patterns
- Building magic squares and magic cubes with NumPy and Python