From Python Nested Lists to Multidimensional numpy Arrays

Figure 8: An example of matrix addition 2-D numpy arrays Turns out we can cast two nested lists into a 2-D array, with the same index conventions.  For example, we can convert the following nested list into a 2-D array:   V=np.array([[1, 0, 0],[0,1, 0],[0,0,1]]) Example 4: creating a 2-D array or array with two access   The convention for indexing is the exact same, we can represent the array using the table form like in figure 5..In numpy the dimension of this array is 2, this may be confusing as each column contains linearly independent vectors..In numpy, the dimension can be seen as the number of nested lists..The 2-D arrays share similar properties to matrices like scaler multiplication and addition.  For example, adding two 2-D numpy arrays corresponds to matrix addition..  X=np.array([[1,0],[0,1]]) Y=np.array([[2,1][1,2]]) Z=X+Y; Z:array([[3,1],[1,3]]) Example 5.1: the result of adding two numpy arrays   The resulting operation corresponds to matrix addition as shown in figure 9: Figure 9: An example of matrix addition..Similarly, multiplication of two arrays corresponds to an element-wise product:   X=np.array([[1,0],[0,1]]) Y=np.array([[2,1][1,2]]) Z=X*Y; Z:array([[2,0],[2,0]]) Example 5.2: the result of  multiplying numpy arrays   Or Hadamard product: Figure 10: An example of Hadamar product..To perform standard matrix multiplication you world use np.dot(X,Y)..In the next section, we will review some strategies to help you navigate your way through arrays in higher dimensions..Nesting List within a List within a List  and 3-D Numpy Arrays We can nest three lists, each of these lists intern have nested lists that have there own nested lists as shown in figure 11..List “A” contains three nested lists, each color-coded..You can access the first, second and third list using A[0], A[1] and A[2] respectively..Each of these lists contains a list of three nested lists.. More details

Leave a Reply