Python Forum
Loading an array into a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading an array into a matrix
#1
Hi everyone,

I am trying to load the output of my model into matrix. Below is the model output:

[array([[3.3480644, 2.6733143, 2.3946035, 1.651863 , 1.4944869, 1.4064457,
         2.0166957, 2.7554176]], dtype=float32),
 array([[2.6761208 , 2.147566  , 1.2485328 , 0.6944433 , 0.87446946,
         1.1815729 , 1.879112  , 2.3758152 ]], dtype=float32),
 array([[2.3128846 , 1.2720771 , 0.9003729 , 0.92187667, 0.9644935 ,
         1.2748135 , 2.2232022 , 2.5829005 ]], dtype=float32),
 array([[1.9967098, 2.0049589, 1.0679721, 1.3402292, 2.1737716, 3.2332737,
         3.9912915, 4.124048 ]], dtype=float32),
 array([[1.0747261, 2.0689538, 3.204754 , 3.952746 , 5.636964 , 5.665282 ,
         4.85702  , 3.7058673]], dtype=float32),
 array([[0.9920357, 1.9206009, 2.5796123, 4.0336523, 5.374252 , 5.6010337,
         5.218882 , 4.5167184]], dtype=float32),
 array([[3.4540672, 4.065122 , 4.130976 , 4.1544843, 3.7034013, 3.9074   ,
         3.742108 , 2.6969736]], dtype=float32),
 array([[1.3718978, 2.7911968, 3.7615182, 5.0639954, 4.2722535, 3.632765 ,
         3.5549235, 2.8956242]], dtype=float32),
 array([[1.9010936, 2.4020395, 3.4859495, 3.2695432, 3.007401 , 2.263217 ,
         2.2069273, 1.3685739]], dtype=float32),
 array([[2.3215306 , 2.7285109 , 3.6797216 , 4.2266564 , 3.4872656 ,
         2.6049411 , 0.78771996, 0.6663305 ]], dtype=float32),
 array([[2.126574 , 3.1755157, 3.8428771, 5.507214 , 4.951423 , 3.8779044,
         3.7308226, 3.217761 ]], dtype=float32),
 array([[3.785448 , 4.4183755, 4.792441 , 5.056678 , 4.3832984, 4.204617 ,
         3.2348392, 2.3076746]], dtype=float32),
 array([[3.6433454, 3.9527392, 3.840207 , 3.5228324, 3.198389 , 2.898097 ,
         2.2972064, 1.6669679]], dtype=float32),
 array([[3.4212291, 3.6168365, 3.2267141, 3.4284039, 3.5152335, 3.36041  ,
         3.3625872, 2.1926944]], dtype=float32),
 array([[4.014863 , 4.2513857, 4.336287 , 4.214694 , 4.285961 , 3.636494 ,
         2.4518902, 1.901188 ]], dtype=float32),
 array([[3.7877383, 4.413838 , 5.1336374, 4.8160186, 4.048491 , 2.8648334,
         1.7532759, 1.0061626]], dtype=float32),
 array([[4.076805 , 4.0782094, 4.5997   , 4.610984 , 3.9340289, 2.4861271,
         1.4596449, 1.421582 ]], dtype=float32),
 array([[3.3311005, 3.1229572, 3.663089 , 3.2575672, 3.149508 , 2.0260038,
         2.0294707, 0.8982593]], dtype=float32),
 array([[4.253025 , 3.6342578, 3.6889012, 3.4614203, 2.7032697, 2.108417 ,
         1.8192803, 1.922114 ]], dtype=float32),
 array([[4.9552517 , 5.4846144 , 4.1990805 , 3.0923777 , 2.1978126 ,
         0.6762241 , 0.1946709 , 0.29030347]], dtype=float32),
 array([[3.8667407, 3.7753189, 3.4018555, 2.4203453, 2.8018296, 2.6180742,
         2.4156811, 2.4616084]], dtype=float32),
 array([[ 4.4066114 ,  4.488062  ,  4.170538  ,  3.3538008 ,  2.780805  ,
          2.0708318 ,  0.9603991 , -0.04771591]], dtype=float32),
 array([[3.369717 , 3.334411 , 2.4926825, 2.415385 , 1.5112386, 1.4150862,
         1.7556384, 1.8881752]], dtype=float32),
 array([[2.836572 , 2.9929788, 3.2093453, 3.1515193, 2.9640412, 2.755675 ,
         2.2785902, 2.261478 ]], dtype=float32),
 array([[3.6565807, 3.4455597, 3.568813 , 2.6705475, 2.1754317, 2.349119 ,
         2.1309204, 2.204011 ]], dtype=float32)]
I am using the below functions to do it:

def add_in_front(data, n):

    data_new = []

    for i in range(n):
        data_new.append([0][0][0])
    return data_new + data

def add_behind(data, n):

    data_new = data

    for i in range(n):
        data_new.append([0][0][0])
    return data_new

def sum_row(data_row):

    count = 0
    total = 0
    for data in data_row:
        total += data
        if data != 0:
            count += 1

    return total / count
And my code to load it is:

overall = []
infront = 0
behind = 24


for calculation in calculations:

    calculation = add_in_front(calculation, infront)
    calculation = add_behind(calculation, behind)

    overall.append(calculation)

    infront += 1
    behind -= 1

projection = np.transpose(overall)[0]

matrix_final = []

for row in projection:
    matrix_final.append(round(sum_row(row), 2))


for element in matrix_final:
    print(element)
I am getting the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-774883fae2e5> in <module>
      6 for calculation in calculations:
      7 
----> 8     calculation = add_in_front(calculation, infront)
      9     calculation = add_behind(calculation, behind)
     10 

<ipython-input-15-aadb2e2a0eb1> in add_in_front(data, n)
      5     for i in range(n):
      6         data_new.append([0][0][0])
----> 7     return data_new + data
      8 
      9 def add_behind(data, n):

ValueError: operands could not be broadcast together with shapes (0,) (1,8) 
The functions were developed on lists not arrays and I think that might be the issue. I tried converting my arrays to lists with the tolist() function from numpy but it just said there was no such object:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-63dbe332ed6e> in <module>
79 behind = 24
80
---> 81 calculations = calculations.tolist()
82
83 for calculation in calculations:

AttributeError: 'list' object has no attribute 'tolist'

Can anyone help me solve the error and get the functions to work on my model array output?

Thanks
Scott
Reply
#2
Not sure to figure out exactly what you want to do, but I guess the following code may help you. Note that I think it's better to convert directly the list:

import numpy as np

List = [[3.3480644 , 2.6733143 , 2.3946035 , 1.651863  , 1.4944869  , 1.4064457 , 2.0166957 , 2.7554176 ], 
        [2.6761208 , 2.147566  , 1.2485328 , 0.6944433 , 0.87446946 , 1.1815729 , 1.879112  , 2.3758152 ]]

List2Array = np.asarray(List)       # convert a list into a Numpy array
print(f"List2Array={List2Array}\n")

r, c = np.shape(List2Array)         # r=number of rows and c=number of columns

# to add zeros in front
M1 = np.zeros((r, 1), dtype=np.float32)
InFront = np.hstack((M1, List2Array))
print(f"InFront={InFront}\n")

# to add zeros at the bottom
M2 = np.zeros((1, c), dtype=np.float32)
AtBottom = np.vstack((List2Array, M2))
print(f"AtBottom={AtBottom}\n")

# to sum the array per row
SumPerRow = np.sum(List2Array, axis=1)
print(f"SumPerRow={SumPerRow}\n")

# to sum the array per column
SumPercolumn = np.sum(List2Array, axis=0)
print(f"SumPercolumn={SumPercolumn}\n")

# to eventually sum all terms
SumAllTerms = np.sum(List2Array)
print(f"SumAllTerms={SumAllTerms}")
Output:
List2Array=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ]] InFront=[[0. 3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [0. 2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ]] AtBottom=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ] [0. 0. 0. 0. 0. 0. 0. 0. ]] SumPerRow=[17.7408911 13.07763246] SumPercolumn=[6.0241852 4.8208803 3.6431363 2.3463063 2.36895636 2.5880186 3.8958077 5.1312328 ] SumAllTerms=30.81852356
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,922 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  Check if two matrix are equal and of not add the matrix to the list quest 3 854 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,399 May-03-2021, 06:30 AM
Last Post: Gribouillis
  matrix from matrix python numpy array shei7141 1 3,726 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020