I want to resolve the error statement. object of type 'numpy.float128' has no len()

Asked 1 years ago, Updated 1 years ago, 71 views

I'm a python beginner.
I'm trying to show the result of nv calculated by numpy on a bar graph.

import numpy as np

nv_fw = np.longdouble(0)
nv_df = np.longdouble(0)
for trial inv:
    for i in range(0,len(trial)-1):  
        nv_fw_dist=np.linalg.norm (trial.iloc[i, 1:3] - trial.iloc[i+1, 1:3])

        nv_df_dist = np.linalg.norm (trial.iloc [i, 3:5] - trial.iloc [i+1, 3:5])

        if np.isnan(nv_df_dist):
            print(trial)
            break
        nv_fw+=nv_fw_dist
        nv_df+=nv_df_dist
       # print(nv_fw,nv_df)

plt.bar(range(len(nv_fw))), nv_fw, color=[1,0,0])

If you write the code like this, you will see an error similar to the following:

TypeError: object of type 'numpy.float128' has no len()

How can we resolve the error?k

python python3 numpy

2022-09-30 21:37

1 Answers

Conclusion: [The code in the question cannot be found to define the variable nv, so the answer includes guesses]

for trial innv:
    for i in range(0,len(trial)-1):  


The "len(trial)" part of the displays the error "TypeError: object of type 'numpy.float128' has no len()"  ·The trial type is 128 bit floating point number (longdouble).
 ·nv is a longdouble list (array).
It turns out that

Also, if you want to graph the value of the list nv,

plt.bar(range(0,len(nv)-1),nv)

is sufficient.(The number on the x-axis is "0,1,2...")

However,

nv_fw_dist=np.linalg.norm (trial.iloc[i, 1:3]-trial.iloc[i+1, 1:3])

It also contains code, such as where nv is assumed to be a list of two-dimensional arrays.

What I can advise you from these is that you should review the definition of nv.
Also, if you modify the code to match the definition of nv after the review, there will be no errors.

===The questioner replied about the variable nv as follows, so I will add it to the answer (the code has been shaped for easy reading)===

nv=[] 
pattern=r'\d{1,2}(_0)' 
for file inos.listdir('./DATA.csv/'):
    if file.endswith('.csv') and re.match(pattern, file):
        nv.append(pd.read_csv('./DATA.csv/'+file, skiprows=2, usecols=[0,3,4,5,6]).dropna())
        print(re.match(pattern, file)) 

From this code, the variable nv can be determined to be an array of five values (like [[1,2,3,4,5,5][1,2.3,4,6,3][3,4,11,2,9]...].

===
Let's look at the original code from the end.

plt.bar (range(len(nv_fw)) , nv_fw, color=[1,0,0])

The first argument on matplotlib.pyplot.bar is the list of positions (X-axis) of each bar, and the second argument is the list of heights (Y-axis) of each bar (both required), so

plt.bar(nv_fw,nv_df,color=[1,0,0])

Like this, nv_fw and nv_df should be numeric arrays.

So,

// Initialization
nv_fw = np.longdouble(0)
nv_df = np.longdouble(0)
// Inside the loop
nv_fw+=nv_fw_dist
nv_df+=nv_df_dist

Instead of looking for the sum as shown in ,

// Initialization
nv_fw = [ ]
nv_df = [ ]
// Inside the loop
nv_fw.append(nv_fw_dist)
nv_df.append(nv_df_dist)

You should try to add numbers to the list as shown in .

===
As for creating data from nv, the elements of nv (a list of five numbers) are taken out in order (nv[i]), nv_fw_dist is calculated from the first three of the five numbers, nv_df_dist is calculated from the last three, and two-dimensional norm (Euclidean norm) is calculated.

for i in range(0,len(nv)-1): //i is a loop of nv's one-dimensional index (note that the array index starts with 0 instead of 1)
    nv_fw_dist=np.linalg.norm (np[i][0:2]-np[i+1][0:2])
    nv_df_dist=np.linalg.norm (np[i][2:4]-np[i+1][2:4])
    nv_fw.append(nv_fw_dist)
    nv_df.append(nv_df_dist)

That's how it feels.

==How do you feel?==
The code that defines nv and the code written in the question have many inconsistencies.
The process of thinking about solving such contradictions is like the one above.

I don't think it's very common to make two sets of three numbers out of five numbers (one number is used in both sets).
I thought maybe I would take 6 numbers from csv (three for nv_fw_dist and three for nv_df_dist) and make a list of 6 numbers.

At the beginning of the question, it would have been nice if there was information such as what program to do, what meaningful data (csv file) was used as input, and what output to expect.


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.