Home > Enterprise >  Float of each element in a matrix python ::
Float of each element in a matrix python ::

Time:10-26

I am trying the convert the elements in a matrix to a float number, wanna the output to be 0.200, instead of 0.2 ? (as the numerical precision is not the same as in Matlab for example, and it affects the results on what i want ? When I tried float() I got the following error: "TypeError: only size-1 arrays can be converted to Python scalars"

Any help, I attached the code:

import numpy as np

 A=np.array([[ 0.0186428,  -0.0056,         -0.0056,          0,          0,
            0],
             [-.1263,  0.42087542, -.1263,          0,          0,
             0],
             [-.1263, -.1263, 0.42087542,  0,          0,
             0],
            [0,         0,          0,          0.2,        -0,
             0        ],
            [ 0,          0,          0,         -0,          0.2,
              0        ],
            [-0,         -0,          0,          0,          0,
              0.2       ]])

            B=np.array([[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[0,0,0,1,0,0],[0,0,0,0,1,0], 
            [0,0,0,0,0,1]])

           C=B*A*B  # float(C) NOT working ?
           print(C)

CodePudding user response:

Your variable C is already a numpy array with float valued numbers. You can check it yourself by printing

In [23]: C.dtype
Out[23]: dtype('float64')

If you want to change how the numpy arrays are printed to the console, you can edit the settings with np.set_printoptions. For example:

In [21]: np.set_printoptions(precision=3, floatmode='fixed')

In [22]: C
Out[22]:
array([[ 0.019, -0.000, -0.000,  0.000,  0.000,  0.000],
       [-0.000,  0.421, -0.000,  0.000,  0.000,  0.000],
       [-0.000, -0.000,  0.421,  0.000,  0.000,  0.000],
       [ 0.000,  0.000,  0.000,  0.200,  0.000,  0.000],
       [ 0.000,  0.000,  0.000,  0.000,  0.200,  0.000],
       [ 0.000,  0.000,  0.000,  0.000,  0.000,  0.200]])
  • The precision of 3 sets the values to be printed with three digits of precision
  • The floatmode of 'fixed' means:

Always print exactly precision fractional digits, even if this would print more or fewer digits than necessary to specify the value uniquely.

Note about multiplication

Based on your comment, it seems that what you are trying to achieve is matrix multiplication of matrices A and B. The * operator is element-wise multiplication. For matrix multiplication, you would want to use np.matmul(np.matmul(B,A), B).

CodePudding user response:

float is built-in function and it is supposed to return single float, so you encountered error when you feed array of numbers, you should use .astype method i.e.:

import numpy as np
arr = np.array([1,2,3])
arr2 = arr.astype(float)
print(arr2)

output

[1. 2. 3.]

(note .s after digits)

CodePudding user response:

As pointed out in the other answer, use C.astype(float) for conversion.

If you you want to print with 3 decimal places use set_printoptions:

np.set_printoptions(precision=3)
print(C)

CodePudding user response:

You can round/set precision of your array with np.around() and printing settings with np.set_printoptions:

...

A = np.around(A, 3)
B = np.around(B, 3)

C=B*A*B
np.set_printoptions(precision=3, floatmode="fixed")
print(C)
  • Related