December 29, 2019python courses
This video covers the basics of creating, inspecting and using NumPy arrays, and the rules for making a NumPy array.
# import numpy import numpy as np # array from list arr = np.array([10, 20, 30, 40, 50]) # print the array print(arr) # [10 20 30 40 50] # dimensionality and shape arr.ndim # 1 arr.shape # (5,) # number of elements len(arr) # 5 # We can also make a 2-dimensional array arr_2d = np.array([ [10, 20, 30, 40, 50], [100, 200, 300, 400, 500] ]) # print the array print(arr_2d) # [[ 10 20 30 40 50] # [100 200 300 400 500]] # dimensionality and shape arr_2d.ndim # 2 arr_2d.shape # (5,) # number of elements len(arr_2d) # 2 # number of nested elements arr_2d.size # 10 # type of object type(arr_2d) # <class 'numpy.ndarray'> # type of internal data arr_2d.dtype # int64 # There are two basic rules for every numpy array # 1) every element in the array must be of the same type # 2) If an array's elements are also arrays, those inner arrays must have the same number of elements as each other # In other words, multidimensional arrays must be rectangular and not jagged. # examples np.array([1, 2, 3]) # good np.array([1, 'hello', 3]) # bad array(['1', 'hello', '3'], dtype='<U21') np.array([ [1, 2, 3, 4], [5, 6, 5, 7], ]) # good np.array([ [1, 2, 3, 4], [5, 6] ]) # bad (results in a 1d array of 3 python lists instead of a 2d array)
Most of numpy is centered around a data structure called an “n dimensional array” or “n d array”, but we’ll just refer to them as arrays.
Before we can start using numpy arrays, we need to install and import numpy. The common convention is to import numpy as np.
Let’s start by making an array from a list of numbers.
We can print the array.
We can check its dimensionality and shape.
And we can see how many elements the array contains. In this case, 5.
We can also make a 2-dimensional array from a list of lists.
We can print the array.
We can check its dimensionality and shape. Here we have a 2d array with shape “2 by 5”. We can see how many elements it has.
Now, you might be surprised to see arr_2d has 2, not 10, elements. Indeed, arr_2d is an array that contains two arrays inside of it. If you want to get the total number of nested elements in the array, use the dot size attribute.
If you want to know the type of data in the array, you might try using python’s type() function, but this just tells you that the object is a numpy array.
If you want to see what kind of data the array is storing, use the dot dtype attribute.
So, what makes a numpy array?
Well, there are two basic rules for every numpy array. Every element in the array must be of the same type.
And, if you have an array of arrays, the inner arrays must have the same number of elements as each other. In other words, multidimensional arrays must be rectangular and not jagged.
Let’s see some examples
1. Here we have a normal 1d array of 3 integers. That’s fine and dandy.
2. Here we have an array of mixed types. In this case numpy won’t error, but it’ll coerce the integers to strings and give us back an array of strings.
3. Here we make a 2x4 array of integers. That’s fine.
4. Here we have a jagged array which is not allowed. Instead of giving us an error, numpy gives us a 1d array of python lists.
So far, we’ve only looked at 1d and 2d arrays, but we can make higher dimensional arrays as long as they follow these rules.