Home > Mobile >  could not convert string to float: '.ipynb_checkpoints'
could not convert string to float: '.ipynb_checkpoints'

Time:03-15

hey guys i am trying to visualiz a dataset in my programm so i can know how many classs i have in my data but this error keep showing up could anyone please help me with this problem because i can't access to my data

 folders = os.listdir(train_path)
    train_number=[]
    class_num=[]
    for folder in folders:
      train_files= os.listdir(train_path   '/'   folder)
      train_number.append(len(train_files))
      class_num.append(classes[int(float(folder))])
    
    #sorting the dataset on the basis of number images in each class
    zipped_lists=zip(train_number,class_num)
    sorted_pairs= sorted(zipped_lists)
    tuples= zip(*sorted_pairs)
    train_number, class_num = [list(tuple) for tuple in tuples]
    
    #plotting the number of images in each class
    plt.figure(figsize=(21,10))
    plt.bar(class_num,train_number)
    plt.xticks(class_num, rotation='vertical')
    plt.show

    ValueError                                Traceback (most recent call last)
    <ipython-input-28-10b88599517e> in <module>()
          5   train_files= os.listdir(train_path   '/'   folder)
          6   train_number.append(len(train_files))
    ----> 7   class_num.append(classes[int(float(folder))])
          8 
          9 #sorting the dataset on the basis of number images in each class
    
    ValueError: could not convert string to float: '.ipynb_checkpoints'

CodePudding user response:

In your folders = os.listdir(train_path) you have a hidden folder call ".ipynb_checkpoints" that Python could not convert to float, you can guard it in a try/except block:

for folder in folders:
    try:
       train_files= os.listdir(train_path   '/'   folder)
       train_number.append(len(train_files))
       class_num.append(classes[int(float(folder))])
    except ValueError:
       # do something with the error
   
  • Related