2016-08-16 31 views
0

我想通過linalg.solve(A,b)求解方程組,解決線性矩陣方程或來自scipy.org的線性標量方程組。具體來說,我有兩個字典,dict1和dict1,爲了使用上面的腳本,我需要將它們轉換爲矩陣。從字典中創建一定大小的矩陣

food = ['fruits', 'vegetables', 'bread', 'meat'] 
frequency = ['daily', 'rarely'] 
consumptions = {'fruits': {'daily': 6, 'rarely': 4}, 'vegetables': {'daily': 8, 'rarely': 6}, 'bread': {'daily': 2, 'rarely': 1}, 'meat': {'daily': 2, 'rarely': 1}} 

dict1 = {} 
for f in food: #type of food 
    for j in food: 
     dict2 = {} 
     total = 0. 
     for q in frequency: 
      dict2.update({q:(consumptions.get(j).get(q)*consumptions.get(f).get(q))}) 
      key = f+'v'+j #comparing the different foods 
     dict1.update({key:dict2}) 

這給了我:

{'breadvbread': {'daily': 4, 'rarely': 1}, 
'breadvfruits': {'daily': 12, 'rarely': 4}, 
'breadvmeat': {'daily': 4, 'rarely': 1}, 
'breadvvegetables': {'daily': 16, 'rarely': 6}, 
'fruitsvbread': {'daily': 12, 'rarely': 4}, 
'fruitsvfruits': {'daily': 36, 'rarely': 16}, 
'fruitsvmeat': {'daily': 12, 'rarely': 4}, 
'fruitsvvegetables': {'daily': 48, 'rarely': 24}, 
'meatvbread': {'daily': 4, 'rarely': 1}, 
'meatvfruits': {'daily': 12, 'rarely': 4}, 
'meatvmeat': {'daily': 4, 'rarely': 1}, 
'meatvvegetables': {'daily': 16, 'rarely': 6}, 
'vegetablesvbread': {'daily': 16, 'rarely': 6}, 
'vegetablesvfruits': {'daily': 48, 'rarely': 24}, 
'vegetablesvmeat': {'daily': 16, 'rarely': 6}, 
'vegetablesvvegetables': {'daily': 64, 'rarely': 36}} 

我想,因爲我使用的4種類型的食物,這種轉換爲4×4矩陣。我沒有把dict2作爲一次我想出如何用一本字典轉換爲矩陣,我可以做另一個,但如果你需要它,我可以更新。

我是新來的Python,想玩玩字典和矩陣求解:)。使用數組很容易,但是現在我想看看如果我有字典,該怎麼辦。

+0

你能舉一個例子來理解你的意思嗎?將它轉換爲4x4矩陣?你試圖解決什麼系統? –

+0

在進行排列組合時,應考慮使用「V」或下劃線以提高可讀性。 – kmario23

回答

2

可以使用列表解析創建從詞典中numpy的數組:

import numpy as np 

A = np.array([[(consumptions[x]["daily"]*consumptions[y]["daily"], 
        consumptions[x]["rarely"]*consumptions[y]["rarely"]) 
        for y in food] 
        for x in food]) 

這會給你:

array([[[36, 16], 
     [48, 24], 
     [12, 4], 
     [12, 4]], 

     [[48, 24], 
     [64, 36], 
     [16, 6], 
     [16, 6]], 

     [[12, 4], 
     [16, 6], 
     [ 4, 1], 
     [ 4, 1]], 

     [[12, 4], 
     [16, 6], 
     [ 4, 1], 
     [ 4, 1]]]) 

這是一個4x4x2數組:

> A.shape 
(4, 4, 2) 

然後,得到daily值的4x4矩陣和rarelyv分開使用,使用numpy的高級切片。 與Python列表不同,numpy數組可以一次切割多個維度。這是通過在括號內爲數組的每個維放置一個切片對象(例如:3 :, 0,:),並用逗號分隔來完成的。 我們陣列,A,具有三個維度:

> A.ndim 
3 

第三維指示值是否爲 「每日」(0)或 「很少」(1)。因此,要獲得所有日常值,我們需要所有行(:),所有列(:),並且只有第三維(0)中的第一個條目。隨着numpy的先進的切片,我們只是separte我們希望切片用逗號每個維度:

> daily = A[:, :, 0] 
> daily 

array([[36, 48, 12, 12], 
     [48, 64, 16, 16], 
     [12, 16, 4, 4], 
     [12, 16, 4, 4]]) 


> rarely = A[:, :, 1] 
> rarely 

array([[16, 24, 4, 4], 
     [24, 36, 6, 6], 
     [ 4, 6, 1, 1], 
     [ 4, 6, 1, 1]]) 

如果你想使這些值的含義更加明確,可以將numpy的陣列轉換爲大熊貓數據幀:

> import pandas as pd 

> df = pd.DataFrame(daily, columns=food, index=food) 
> df 

      fruits vegetables bread meat 
fruits  36  48   12  12 
vegetables 48  64   16  16 
bread  12  16   4  4 
meat  12  16   4  4 

有關高級切片的更多信息,請參見http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing

+0

謝謝。這正是我所期待的。我覺得作爲一個初學者,在我可以轉向熊貓之前,我仍然在學習基礎知識。不過謝謝你的熊貓榜樣!我期待很快學習熊貓。 –

+0

你能解釋一下預先切片嗎?我無法理解如何創建4x4矩陣。 –

+0

我更新了我的答案,以瞭解關於高級切片的更多細節。 – mohrtw