Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition

Similar presentations


Presentation on theme: "Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition"— Presentation transcript:

1 Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition >>> A = [[0,0],[1,1],[2,2]] >>> zip(*A) [(0, 1, 2), (0, 1, 2)] How to get the transpose of this matrix.. Any easier ,algorithmic way to do this... 1st question: Input a=[[1,2,3],[4,5,6],[7,8,9]] Expected output a=[[1, 4, 7], [2, 5, 8], [3, 6, 9]] 2nd Question: Zip gives me the following output said below, how can i zip when i dont know how many elements are there in the array,in this case i know 3 elements a[0],a[1],a[2] but how can i zip a[n] elements >>> zip(a[0],a[1],a[2]) [(1, 4, 7), (2, 5, 8), (3, 6, 9)] question answers: >>> import numpy as np >>> first_answer = np.transpose(a) >>> second_answer = [list(i) for i in zip(*a)]

2 Multi-Dimensional Arrays or Matrices
 a simple two-dimensional tabular summary. When rolling two dice, there are 36 possible outcomes a multi-dimensional table can be implemented as a sequence of sequences. A table is a sequence of rows. Each row is a sequence of individual cells. This allows us to use mathematical-like notation. Where the mathematician might say Ai,j , in Python we can say A[i][j]. In Python, we want the row i from table A, and column j from that row. Let's look at a simple two-dimensional tabular summary. When rolling two dice, there are 36 possible outcomes. We can tabulate these in a two-dimensional table with one die in the rows and one die in the columns:

3 List of lists example build a table using a nested list comprehension. The following example creates a table as a sequence of sequences and then fills in each cell of the table. This program did two things. It created a six by six table of zeroes. It then filled this with each possible combination of two dice. This is not the most efficient way to do this, but we want to illustrate several techniques with a simple example. We'll look at each half in detail. The first part of this program creates and prints a 6-item list, named table; each item in the table is a 6-item list of zeroes. It uses a list comprehension to create an object for each value of j in the range of 0 to 6. Each of the objects is a list of zeroes, one for each value of i in the range of 0 to 6. After this initialization, the two-dimensional table of zeroes is printed. The comprehension can be read from inner to outer, like an ordinary expression. The inner list, [ 0 for i in range(6) ], creates a simple list of six zeroes. The outer list, [ [...] for j in range(6) ] creates six copies of these inner lists. The second part of this program then iterates over all combinations of two dice, filling in each cell of the table. This is done as two nested loops, one loop for each of the two dice. The outer enumerates all values of one die, d1. The loop enumerates all values of a second die, d2. Updating each cell involves selecting the row with table[d1]; this is a list of 6 values. The specific cell in this list is selected by ...[d2]. We set this cell to the number rolled on the dice, d1+d2+2.

4

5 Explicit Index Values use a simple list with 13 buckets (numbered from 0 to 12) for the frequency of each die roll fq= 13*[0] for i in range(6): for j in range(6): c= table[i][j] fq[ c ] += 1 We initialize the frequency table, fq, to be a list of 13 zeroes. The outer loop sets the variable i to the values from 0 to 5. The inner loop sets the variable j to the values from 0 to 5. We use the index value of i to select a row from the table, and the index value of j to select a column from that row. This is the value, c. We then accumulate the frequency occurances in the frequency table, fq. This looks very mathematical and formal. However, Python gives us an alternative, which can be somewhat simpler.

6 Using List Iterators Instead of Index Values
Since our table is a list of lists, we can make use of the power of the for statement to step through the elements without using an index. fq= 13*[0] print fq for row in table: for c in row: fq[c] += 1 print fq[2:] We initialize the frequency table, fq, to be a list of 13 zeroes. The outer loop sets the variable row to each element of the original table variable. This decomposes the table into individual rows, each of which is a 6-element list. The inner loop sets the variable c to each column's value within the row. This decomposes the row into the individual values. We count the actual occurances of each value, c by using the value as an index into the frequency table, fq. The increment the frequency value by 1.

7 Mathematical Matrices
We use the explicit index technique for managing the mathematically-defined matrix operations. Matrix operations are done more clearly with this style of explicit index operations. We'll show matrix addition as an example created two input matrices, m1 and m2, each three by four. We initialized a third matrix, m3, to three rows of four zeroes, using a comprehension. Then we iterated through all rows (using the i variable), and all columns (using the j variable) and computed the sum of m1 and m2.

8 Transpose a Matrix Implement a matrix as nested list (list inside a list). Treat each element as a row of the matrix. For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix. First row can be selected as X[0] and the element in first row, first column can be selected as X[0][0].

9 Matrix Transpose using Nested Loop
In this program we have used nested for loops to iterate through each row and each column. At each point we place the X[i][j] element into result[j][i].

10 Matrix Transpose using Nested List Comprehension

11 Multiply Two Matrices Implement a matrix as nested list (list inside a list). Treat each element as a row of the matrix. For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix. First row can be selected as X[0] and the element in first row, first column can be selected as X[0][0].

12 Matrix Multiplication using Nested Loop
Use nested for loops to iterate through each row and each column. Accumulate the sum of products in the result. This technique is simple but computationally expensive as we increase the order of matrix. For larger matrix operations recommend optimized software packages like NumPy which is several (in the order of 1000) times faster than the above code.

13 Using Modules A Python module extends the Python execution environment by adding new classes, functions and helpful constants. Tell the Python interpreter to fetch a module with a variation on the import statement. There are several variations on import , For now, we'll use the simple import : import m This will import module m. Only the module's name, m is made available. Every name inside the module m must be qualified by prepending the module name and a .. So if module m had a function called spam, we'd refer to it as m.spam.

14 Matrix Multiplication using Nested List Comprehension
zip(*Y)是把Y转置 zip(*Y)的意思是:zip(Y[0], Y[1], Y[2]) 然后把这三个数组zip起来,就变成了Y的转置形式了~ To understand the above code we must first know about built-in function zip() and unpacking argument list using * operator. Use nested list comprehension to iterate through each element in the matrix. The code looks complicated and unreadable at first. But once you get the hang of list comprehensions, you will probably not go back to nested loops.

15 zip() in conjunction with the * operator can be used to unzip a list:

16 Unpacking Argument Lists
The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

17 zip(*Y)是把Y转置 They used to behave as the left display in python 2.x, but they were changed to generator-like objects which produce the elements on demand rather than expand an entire list into memory. One advantage was greater efficiency in their typical use-cases (e.g. iterating over them).


Download ppt "Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition"

Similar presentations


Ads by Google