Home > Mobile >  ax=b solving with numpy llinalg gives "Last 2 dimensions of the array must be square"
ax=b solving with numpy llinalg gives "Last 2 dimensions of the array must be square"

Time:09-27

I have 3 matrices of shape (157, 4) , (157,) and (4,) respectively. The first matrix (an_array) contains the quantity of the items. the second, (Items matrix) contains the items names and the 3rd (Price matrix) one contains the price. I am trying to workout the price of individual items but I get an error saying "Last 2 dimensions of the array must be square" everytime I am trying to do

X = np.linalg.solve(an_array,price)

 

    

I am trying to solve ax=b but unfortunately its not working out. Any help would be very much appreciated.

**Price matrix**

array([499.25, 381.  ,  59.5 , 290.  , 128.5 , 305.25, 336.25, 268.5 ,
       395.25, 136.5 , 194.5 , 498.75,  62.25, 312.75, 332.  , 171.  ,
       402.5 , 144.5 , 261.5 , 242.75, 381.  , 371.  , 355.5 , 373.  ,
        65.5 , 228.75, 208.75, 204.5 ,  86.5 , 143.  ,  70.5 ,  36.5 ,
        82.  , 302.5 , 365.5 , 158.5 , 316.5 , 508.  ,  86.5 , 359.75,
        25.5 , 345.5 , 304.5 , 491.25, 181.5 , 343.75, 383.5 , 283.5 ,
       140.25, 426.  , 386.  , 337.25, 415.5 , 268.25, 406.  , 149.5 ,
       200.  , 122.  , 510.25, 280.  , 406.75, 191.25, 198.  , 114.5 ,
       211.5 , 241.75, 195.75, 276.25, 276.  , 165.25, 102.  , 425.  ,
       195.  , 132.25,  86.75, 446.5 , 318.  , 290.75, 286.  , 232.  ,
       520.5 , 382.75,  94.  , 482.75, 233.25, 262.  , 368.25, 438.75,
       433.5 , 334.5 , 360.  , 422.  , 191.  , 292.25, 151.75, 440.25,
       370.  , 105.25, 122.  , 455.5 , 363.  , 436.  , 147.5 , 548.5 ,
       365.75, 185.5 , 348.  , 342.5 , 509.25, 465.5 , 380.25, 361.  ,
       271.25, 414.25, 366.75, 145.5 , 348.  , 471.25, 254.5 , 329.  ,
       441.  , 253.25, 448.5 , 142.  , 312.5 , 350.  ,  94.  , 333.  ,
       418.  , 194.5 , 543.  , 212.5 ,  66.5 , 370.  , 423.  , 164.  ,
       393.25, 299.75, 529.5 , 166.25, 228.5 , 476.  , 373.  , 383.25,
       409.  , 241.  , 107.75, 194.5 , 350.  , 221.75, 633.25, 444.25,
       155.25,  76.  , 542.  , 346.  , 159.75])

**Item matrix**
array(['Cereal', 'Coffee', 'Eggs', 'Pancake'], dtype='<U7')


#an_array matrix
    array([[ 7,  6,  6,  9],
           [ 6,  7,  0, 10],
           [ 0,  7,  1,  0],
           [ 4,  0, 10,  0],
           [ 0,  0,  4,  2],
           [10,  7,  0,  3],
           [ 9,  1,  4,  3],
           [ 9,  8,  0,  2],
           [10,  0,  4,  5],
           [ 6,  3,  0,  0],
           [ 0,  1,  9,  0],
           [ 3,  9,  9,  9],
           [ 2,  0,  0,  1],
           [ 6,  0,  6,  3],
           [ 9,  0,  3,  4],
           [ 8,  2,  0,  0],
           [ 9,  0,  0, 10],
           [ 5,  0,  0,  2],
           [ 8,  7,  3,  0],
           [ 0,  1,  6,  5],
           [ 7,  0,  3,  8],
           [ 0,  5, 10,  6],
           [ 7,  1, 10,  0],
           [ 4,  7, 10,  2],
           [ 0,  0,  1,  2],
           [ 2,  6,  0,  7],
           [ 6,  4,  0,  3],
           [ 8,  0,  0,  2],
           [ 0,  0,  2,  2],
           [ 3,  7,  0,  2],
           [ 0,  9,  1,  0],
           [ 1,  3,  0,  0],
           [ 3,  4,  0,  0],
           [ 4,  0,  0, 10],
           [ 4,  9,  7,  4],
           [ 6,  7,  0,  0],
           [ 6,  9,  7,  0],
           [ 6,  0, 10,  8],
           [ 0,  0,  2,  2],
           [ 6,  0,  4,  7],
           [ 1,  1,  0,  0],
           [ 3,  9,  7,  4],
           [ 0,  1, 10,  4],
           [ 6,  1, 10,  7],
           [ 0,  2,  6,  2],
           [ 4,  1,  7,  5],
           [ 0,  3,  9,  8],
           [ 3,  2,  8,  2],
           [ 0, 10,  3,  1],
           [ 4,  0,  8,  8],
           [ 2,  0,  8,  8],
           [ 7,  8,  2,  5],
           [ 7,  2,  2, 10],
           [ 0,  9,  3,  7],
           [ 4,  4,  6,  8],
           [ 5,  9,  0,  0],
           [ 0,  2,  9,  0],
           [ 4,  0,  2,  0],
           [10,  9,  5,  7],
           [ 4,  4,  0,  8],
           [ 9, 10,  5,  3],
           [ 4,  0,  0,  5],
           [ 1,  0,  0,  8],
           [ 1,  1,  0,  4],
           [ 4,  1,  6,  0],
           [ 0,  8,  2,  7],
           [ 1,  5,  6,  1],
           [ 4,  4,  3,  5],
           [ 9,  6,  3,  0],
           [ 2,  3,  2,  3],
           [ 4,  4,  0,  0],
           [ 8, 10, 10,  0],
           [ 6,  6,  2,  0],
           [ 0,  0,  1,  5],
           [ 1,  0,  0,  3],
           [ 7,  0,  4, 10],
           [ 4,  8,  5,  4],
           [ 9,  8,  0,  3],
           [ 4,  6,  4,  4],
           [10,  2,  1,  0],
           [10,  3,  6,  8],
           [ 6,  8,  3,  7],
           [ 0,  9,  0,  2],
           [ 8,  7,  4,  9],
           [ 0,  6,  0,  9],
           [ 0,  0,  4,  8],
           [ 0,  0,  8,  9],
           [ 8,  8,  8,  3],
           [ 3,  5,  8,  8],
           [ 7,  3,  0,  8],
           [ 9,  6,  7,  0],
           [ 8,  0,  4,  8],
           [ 9,  2,  0,  0],
           [ 6,  3,  0,  7],
           [ 0,  4,  3,  3],
           [10,  1,  8,  3],
           [ 5, 10,  6,  4],
           [ 0,  7,  0,  3],
           [ 4,  0,  2,  0],
           [10,  6,  0, 10],
           [ 4,  0,  5,  8],
           [10,  0,  7,  4],
           [ 1,  7,  0,  4],
           [10,  0,  6, 10],
           [ 8, 10,  4,  3],
           [ 9,  1,  0,  0],
           [ 9,  0,  8,  0],
           [ 6,  0,  0, 10],
           [ 9,  1,  8,  7],
           [ 1, 10,  8, 10],
           [ 0,  6,  7,  9],
           [10,  5,  0,  6],
           [ 0, 10,  5,  5],
           [ 8,  6,  1,  9],
           [ 0,  4,  9,  7],
           [ 4,  0,  1,  2],
           [ 3,  9,  5,  6],
           [10, 10,  5,  5],
           [ 5,  0,  1,  6],
           [ 9,  8,  5,  0],
           [ 8,  3,  2, 10],
           [ 0,  2,  2,  9],
           [ 5,  9, 10,  4],
           [ 6,  4,  0,  0],
           [ 8,  1,  7,  0],
           [ 6,  7,  7,  2],
           [ 0,  9,  0,  2],
           [10,  8,  0,  4],
           [10,  1,  8,  2],
           [ 0,  3,  0,  8],
           [10,  8, 10,  4],
           [ 0,  0,  8,  2],
           [ 0,  4,  0,  2],
           [ 8,  0, 10,  0],
           [ 7,  0,  5,  8],
           [ 6,  8,  0,  0],
           [ 8,  6,  0,  9],
           [10,  6,  0,  3],
           [ 9,  4,  5, 10],
           [ 0, 10,  0,  5],
           [ 6,  4,  2,  2],
           [ 9, 10,  3,  8],
           [ 8,  3,  3,  6],
           [ 6,  0,  3,  9],
           [ 3,  1, 10,  6],
           [ 0,  0,  3,  8],
           [ 4,  1,  0,  1],
           [ 5,  1,  0,  4],
           [ 7,  0, 10,  0],
           [ 5, 10,  0,  3],
           [10,  8,  9,  9],
           [10,  6,  9,  1],
           [ 0,  8,  0,  5],
           [ 0, 10,  1,  0],
           [ 8,  7, 10,  6],
           [ 0,  0,  8,  8],
           [ 0,  5,  1,  5]])

CodePudding user response:

you have 4 variables that are present in 157 equations, and you need to solve for the 4 variables, this system has no solution and is not solved by a linear system solution, rather as a least squares solution using np.linalg.lstsq

X = np.linalg.lstsq(an_array, price)[0]
print(X)

CodePudding user response:

Using the lstsqr as recommended by @Ahmed:

In [88]: X = np.linalg.lstsq(quant, price)[0]
C:\Users\paul\AppData\Local\Temp\ipykernel_6428\995094510.py:1: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
  X = np.linalg.lstsq(quant, price)[0]

In [90]: X
Out[90]: array([20.  ,  5.5 , 21.  , 22.25])

Multiplying the quantities by these prices and summing gives the same numbers as your price array:

In [91]: quant@X
Out[91]: 
array([499.25, 381.  ,  59.5 , 290.  , 128.5 , 305.25, 336.25, 268.5 ,
       395.25, 136.5 , 194.5 , 498.75,  62.25, 312.75, 332.  , 171.  ,
      ... , 159.75])

So there's no noise in these prices - the values are exact. That means you could use solve with any 4 of the price and quant values. That addresses the "must be square" error. quant[:4,:] has (4,4) shape.

In [92]: np.linalg.solve(quant[:4,:], price[:4])
Out[92]: array([20.  ,  5.5 , 21.  , 22.25])
  • Related