0

我面临的问题是我的代码不断在 Jupyter Notebook 中抛出 AttributionError,即使我什至没有调用指定为错误的函数。我的python代码如下:

 import numpy as np
 import matplotlib
 matplotlib.use('nbagg')
 import matplotlib.pyplot as plt
 import seaborn as sns
 import pandas as pd 
 import os
 from PIL import Image

cube_features = []
 for cube in cubes:
df_cube = pd.read_csv(cube_base + 'props_cube{}.csv'.format(cube),
                        names=['cube', 'phi_x', 'phi_y', 'fir_x', 'fir_y', 'bush_x', 'bush_y', 'sun_x', 'sun_y', 'poplar_x', 'poplar_y', 'img_path'])
area_features = [] 
for area in areas:
    if area == 'Moc':
        features = []
        for row in df_cube['img_path']:
            image = Image.open(row).convert('RGB')
            features.append(np.array(image).flatten())
        features = np.array(features)
    else:
        features_dict = np.atleast_1d(np.load('/Users/tol/Features/CORnetZ/cube{}_CORnet-Z_{}_output_feats.npy'.format(cube, area), allow_pickle=True))[0]
        if not all(np.array(features_dict['fnames']) == df_cube['img_path'].values): 
            raise ValueError('My Error')
        features = features_dict['model_feats']
    print('Cube {}, Area {} - Representation size {}'.format(cube, area, features.shape))
    #area_corr = pd.DataFrame(features.transpose()).corr() # num_images x num_images
    area_features.append(features)
cube_features.append(area_features)

我的错误代码是:

    AttributeError                            Traceback (most recent call last)
  /opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
   2894     try:
 -> 2895         fp.seek(0)
   2896     except (AttributeError, io.UnsupportedOperation):

    AttributeError: 'float' object has no attribute 'seek'

    During handling of the above exception, another exception occurred:

    AttributeError                            Traceback (most recent call last)
    <ipython-input-43-31c8f89d4a7f> in <module>
       8             features = []
       9             for row in df_cube['img_path']:
     ---> 10                 image = Image.open(row).convert('RGB')
      11                 features.append(np.array(image).flatten())
      12             features = np.array(features)

    /opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
     2895         fp.seek(0)
     2896     except (AttributeError, io.UnsupportedOperation):
     -> 2897         fp = io.BytesIO(fp.read())
     2898         exclusive_fp = True
     2899 

     AttributeError: 'float' object has no attribute 'read'

我真的希望有人可以帮助我!谢谢!

4

1 回答 1

0

我的问题的答案很简单:我在读取 csv 文件时忘记指明分隔符,因此数据没有正确保存。“行”指向一个空列。

始终打印您读过的内容。

谢谢您的帮助!

于 2021-01-04T11:44:31.440 回答