аватар question@mail.ru · 01.01.1970 03:00

The whole of integers: Typeerror: Only Length-1 Arrays Can Be Converted to Python Scalars

   IMPORT  ScipY  Import  ScientpAc  Import  cmath   import  math  import  matplotlib.pyplot  as  print   Import  numpy  as  np  from  scipy  idl  from  scipy.signal  import  freeqs, irfilterxx = np.linespace ( 0 ,  1024 ,  1024 ) # the range of values ​​x  byts = ([ 0 ,  0 , the  0 ,  0 ,  1 ,  0 ,  1 ,  0 ,  0 ,  0 ,  1  0 ,  0 ,,,,  0 ,  0 ,  1 ]) ff =  64   2  * np.pi * (x -  1 ) * ff/ 1024 ) # bearing  y = ns * Byts [ round  ( int  (x +  31 )/ plt.plot (y)     

Error:

  Typeerror: Only Length-  1  Arrays can be converted to python scalars    

how to set an array of integers x so that this error does not arise?

аватар answer@mail.ru · 01.01.1970 03:00

We will figure out why this one arises. Let's look at the expression:

   ROUND  ( int  (x +  31 )/ 64 )     

x + 31 this is an array of length 1024, and we cite it to into '. It tus out as a result. Python says so:

typeerror: only arrays of the length of one can be led to a scalar type (int is a scalar type)

to remove int 'y:

  y = ns * byts [ round  ((x +  31 )/ 64 )]  

the error will disappear, but it will appear:

typeerror: type numpy.ndarray soresn`t define __round__ method

can be replaced with:

  y = ns * byts [np.  round  ((x +  31 )/ 64 )]     

we will get another :

typeerror: only integer scalar arrays can be converted to a scalar index

the fact is that np.round (x + 31) / 64) This is an array, with a type of Float elements, and we use this array as an array of indices, and indices can only be whole. It is necessary to change the type of array elements using:

  y = ns * bys [np.  round  (x +  31 )/ 64 ). Astype ( int )]   

we will get another again :

typeerror: only integer scalar arrays can be converted to a scalar index

is that the fact is that byts This is a regular array, and through square brackets you can not handle the index massage. Let's make byts numpy massive:

  byts = np.array ([ 0 ,  0 ,  0 ,  0 ,  1  0 ,  1 , it  0 ,  0 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0 ,  1 ])     

:

indexerror: index 16 is out of bounts for axis 1 with size 16

arises due to the fact that the np.round array (x + + 31)/64 there are elements equal to 16. A possible solution will be a change in the type of array elements x on int :

  x = "> x ="> x = "> x ="> x = "> x ="> x = "> x ="> x = "> x =" np.Linspace ( 0 ,  1023 ,  1024 ). Astype ( int )     

and replacing a complex expression np.round ((x + 31)/64 with a simple x // 64

  y = ns * byts [x //  64 ]  class = "">   import  scipy  Import  scipvtpack  Import  cmath   import  math  import  matplotlib.pyplot  as  print   Import  numpy  as  np  from  scipy  idl  from  scipy.signal  import  frequs, Iirfilterx = np.Linspace ( 0 ,  1023 ,  1024 ). Astype ( int ) # range of values ​​x  byts = np.array ([ 0 ,  0 ,  0 ,  0  1 ,  0 ,, the  1 ,  0 ,  0 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0  1 ]) ff =  64  # %Frequency carrier  ns = np.sin ( 2  * np.pi * (x -  1 ) * ff/ 1024 ) # carrier  y = ns * byts [x //  64 ] #### %signal  plt.plot (y)                                       

Latest

Similar