2017-02-15 89 views
0

我在Python非常初學者。 我有矩陣乘法的問題。 我使用列表(矩陣)列表從txt文件中讀取矩陣。矩陣乘法在Python,多處理

當我想用多我有鴻溝列出了問題和池功能使用新的列表。

我該如何解決這個問題?

請從評論幫助

def matrix_multiplication(list1, list2): 
    A = numpy.matrix(list1) 
    B = numpy.matrix(list2) 
    return A*B 

def counting(dane): 

    left_matrix = matrices[0] 

    for matrix in matrices[1:]: 
     left_matrix = numpy.matrix(left_matrix) 
     matrix = numpy.matrix(matrix) 
     left_matrix = matrix_multiplication(left_matrix, matrix) 


if __name__ == "__main__": 


    matrices = [] 
    with open('sample-probka2.txt', 'r') as file: 
     matrix_reader = csv.reader(file, delimiter=';') 
     current_matrix = [] 

     for row in matrix_reader: 
      if len(row) == 0: 
       matrices.append(current_matrix) 
       current_matrix = [] 
      else: 
       current_matrix.append(list(map(float, row))) 

    print (matrices) 

    counting(matrices) 


    np = multiprocessing.cpu_count() 
    print('You have', np, 'processors') 


    matrices2 = numpy.array_split(matrices, np) 
    print(matrices2) 


    pool = Pool(processes=np) 
    count = pool.starmap(liczenie, matrices2) 
    print count 

錯誤,與企圖恢復格式化:

multiprocessing.pool.RemoteTraceback: 
Traceback (most recent call last): 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/pytho‌​n3.6/multiprocessing‌​/pool.py", 
line 119, in worker result = (True, func(*args, **kwds)) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/pytho‌​n3.6/multiprocessing‌​/pool.py", line 47, 
in starmapstar return list(itertools.starmap(args[0], args[1])) TypeError: counting() takes 1 positional argument but 61 were given 
+0

你得到的錯誤是什麼? – Aaron

+0

multiprocessing.pool.RemoteTraceback: 「」「 Traceback(最近呼叫的最後一個): File」/Library/Frameworks/Python.framework/Versions/3。6/lib/python3.6/multiprocessing/pool.py「,第119行,在worker result =(True,func(* args,** kwds)) File」/Library/Frameworks/Python.framework/Versions/ 3.6/lib/python3.6/multiprocessing/pool.py「,第47行,在starmapstar 返回列表(itertools.starmap(args [0],args [1])) TypeError:counting()取1個位置參數,但61給予 「」「 –

+0

在我看來,我應該修改'矩陣2'。在這種情況下,我不能使用'pool.starmap(計數,矩陣2)。我應該改變一些東西,但我不知道是什麼。 –

回答

0

我不認爲你想在這種情況下pool.starmap。它在做什麼是每個矩陣拆包到61層的元件,因爲它是交給counting()僅期望一個參數。

docs:

starmap(func, iterable[, chunksize])

Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments.

Hence an iterable of [(1,2), (3, 4)] results in [func(1,2), func(3,4)].

,你可以在多種方式解決這個問題,但我認爲最明智的方法是使用pool.map代替,因爲它不會把你的投入,讓他們作爲一個單一的變量。你也可以定義counting(*dane)以允許可變數量的輸入連接到列表dane(在功能中你不使用,但我會留給你弄清楚如何處理它)。

0

count = pool.starmap(liczenie, matrices2) 

是什麼liczenie?我在代碼中看不到這樣的功能。錯誤消息表明它是counting

但是在counting(dane),dane從不使用。

它看起來像matrices是浮動列表的列表。所有的子列表都是相同的長度嗎?如果是這樣,你爲什麼不讀csv文件與numpy的管理器,例如np.genfromtxt?但這是一個側面。

是否counting(matrices)工作?除了搞砸的調用參數之外,它不會返回任何東西。我猜它應該是做鏈式矩陣產品,M1*M2*M3*...* for np.matrix是矩陣乘積,而不是逐元素(*np.array())。

但是,這是行不通的; np.matrix(alist)是(1,n)矩陣。它無法複製其他(1,N)矩陣,不管有多少次你把它包在np.matrix(你爲什麼做幾次?)

In [25]: M1 = np.matrix([1,2,3,4]) 
In [26]: M1*M1 
.... 
ValueError: shapes (1,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0) 

什麼是多重的目的是什麼?什麼是應該這樣做,一個電話修正counting不能做?