I want to get values from the number.array array.array array.

Asked 1 years ago, Updated 1 years ago, 63 views

I would like to search for the value I want to get from the array using numpy.array and display it in print.
But

index=np.where (data[powering]==2.35)
 IndexError: arrays used as indications must be of integer (or boolean) type

I get an error saying this.The contents of this array also contain decimal numbers, so the integer in the error cannot do it.How can I get a value?
The current python code is as follows:

def content(volta, currenting, powering):
  import numpy as np
  from path import floor
  from path import ceil

  a=np.array([volta, currenting, powering])
  support=np.round(a,2)
  data=np.transpose(support)

  sample=open("test.txt", "w")
  sample.write(str(data))
  sample.close()

  index=np.where (data[powering]==2.35)
  if index==2.35:
    vout = 2.9 / (2.9 + data [currenting])
    print(str(vout))
    sleep(10)
  else:
    nextvalue=np.round(a,1)
    datatwo=np.transpose(nextvalue)

python numpy

2022-09-30 19:52

1 Answers

"The ""value"" of an array element can be a real number represented by a floating point, but I think the ""additive"" that specifies the array element must be an integer." The error message can be interpreted as data[0] or data[26946,3953964], but I don't know what they say when data[1.594306850] or powering=[2.3353,2.3554,2.295090].Well, that's right.

To guess what you want to do,
current = [0.2398, 0.30234, 0.15930, 0.398111, 0.221598]
power = [2.2901, 2.37831, 2.34988, 2.352199, 2.125985]
If there is a list like this, I thought you should pick up the subscripts 2 and 3 with the power factor rounded to 2.35, and calculate vout using current[2] and current[3].

If that's the case, if you dare to write using numpy.where, why not write like this?

#!/usr/bin/python
#
# This script was written by Norio 2016-09-16.

def calc_v0 (currenting, powering, p0, eps):
    '''
    This function returns a couple (idx, v0arr), where `idx` and 
    `v0arr`are numpy arrays of integer and float types, responsively.
    Here`idx`is an array of these integers`j`such that 
    `p0-eps<powering[j]<p0+eps`, and 
    `v0arr`is an array of these values`2.9/(2.9+currenting[j])`,
    for each `j`in`idx`.
    '''

    import numpy as np 
    carr=np.array(currenting)
    parr=np.array(powering)

    idx=np.where(np.logical_and(p0-eps<parr,parr<p0+eps))[0] 
    c0arr=carr[idx] 
    v0arr = 2.9 / (2.9 + c0arr)
    return(idx,v0arr)


current = [0.2398, 0.30234, 0.15930, 0.398111, 0.221598]
powering = [2.2901, 2.37831, 2.34988, 2.352199, 2.125985 ]
p0 = 2.350
eps = 5.0e-3
(index, vout) = calc_v0 (currenting, powering, p0, eps)

print '#index vout'
for jj, vvin zip (index, vout):
    print '%5i%20.5e'%(jj,vv)

I think the standard output will show this kind of data.

#index vout
    29.47929e-01
    38.79291e-01

Also, I think I can write like this without using numpy.

#!/usr/bin/python
#
# This script was written by Norio 2016-09-16.

current = [0.2398, 0.30234, 0.15930, 0.398111, 0.221598]
powering = [2.2901, 2.37831, 2.34988, 2.352199, 2.125985 ]
p0 = 2.350
eps = 5.0e-3

index=[jj for jj in range(len(powering))] 
       if(p0-eps<powering[jj] and powering[jj]<p0+eps)]
vout = [2.9/(2.9+currenting[jj]) for jjin index ]

print '#index vout'
for jj, vvin zip (index, vout):
    print '%5i%20.5e'%(jj,vv)

If you don't need a list of subscripts in particular, I think it would be easier to write like this

#!/usr/bin/python
#
# This script was written by Norio 2016-09-16.

current = [0.2398, 0.30234, 0.15930, 0.398111, 0.221598]
powering = [2.2901, 2.37831, 2.34988, 2.352199, 2.125985 ]
p0 = 2.350
eps = 5.0e-3

vout = [2.9/(2.9+cc) for cc,ppin zip (currenting, powering) 
       if(p0-eps<pp and pp<p0+eps)]

print '#vout'
for vv in vout:
    print '%20.5e'%(vv)


2022-09-30 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.