【问题标题】:AttributeError: 'DataFrame' object has no attribute 'get_value'AttributeError:“DataFrame”对象没有属性“get_value”
【发布时间】:2020-07-15 19:39:44
【问题描述】:

我是 python 和深度学习的新手。我正在运行用于肺癌检测的代码。我不断收到错误'DataFrame' object has no attribute 'get_value',我不知道我哪里出错了......

这里有类似的问题 --> link1 link2 但他们都没有帮助。

这是我的代码

train_data = []

test_data = []

for num, patient in enumerate(patients):
        if num%50 == 0:
            print(num)

        try:
            img_data, label = process_data(patient, data_labels, img_pxl_size=IMG_PXL_SIZE, hm_slices=HM_SLICES)
            train_data.append([img_data,label])
            print(img_data.shape, label)

        except KeyError as e:
            test_data.append([img_data])
            print(img_data.shape , '\tThis is unlabeled data')

np.save('traindata-{}-{}-{}.npy'.format(IMG_PXL_SIZE,IMG_PXL_SIZE,HM_SLICES), train_data)
np.save('testdata-{}-{}-{}.npy'.format(IMG_PXL_SIZE,IMG_PXL_SIZE,HM_SLICES), test_data)
print('Finished processing')

这是我得到的错误

AttributeError                            Traceback (most recent call last)

<ipython-input-15-3cc0d9dbeb3c> in <module>()
      9 
     10         try:
---> 11             img_data, label = process_data(patient, data_labels, img_pxl_size=IMG_PXL_SIZE, hm_slices=HM_SLICES)
     12             train_data.append([img_data,label])
     13             print(img_data.shape, label)

----------------------------------------------------------------------------------------------

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
   5275 
   5276     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'get_value'

process_data的定义在这里

# Function to process data
def process_data(patient, data_labels, img_pxl_size=20, hm_slices=20, vizualize = False):

    label = data_labels.get_value(patient,'cancer')
    path = data_dir + patient
    slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
    #print(len(slices), label)
    #print(slices[0])
    #plt.imshow(slices[0].pixel_array)
    #plt.show()

    new_slices = []

    slices = [cv2.resize(np.array(each_slice.pixel_array),(IMG_PXL_SIZE,IMG_PXL_SIZE)) for each_slice in slices]

    chunk_sizes = math.ceil(len(slices) / HM_SLICES)



    for slice_chunk in chunks(slices,chunk_sizes):
        slice_chunk = list(map(mean, zip(*slice_chunk)))
        new_slices.append(slice_chunk)


    if len(new_slices) == HM_SLICES-1:
        new_slices.append(new_slices[-1])

    if len(new_slices) == HM_SLICES-2:
        new_slices.append(new_slices[-1])
        new_slices.append(new_slices[-1])

    if len(new_slices) == HM_SLICES+2:
        new_val = list(map(mean, zip(*[new_slices[HM_SLICES-1],new_slices[HM_SLICES]])))
        del new_slices[HM_SLICES]
        new_slices[HM_SLICES-1] = new_val

    if len(new_slices) == HM_SLICES+1:
        new_val = list(map(mean, zip(*[new_slices[HM_SLICES-1],new_slices[HM_SLICES]])))
        del new_slices[HM_SLICES]
        new_slices[HM_SLICES-1] = new_val


    if vizualize:
        fig = plt.figure()
        for num,each_slice in enumerate(slices[:12]):
            y = fig.add_subplot(4,5,num+1)
            #new_image = scipy.misc.imresize(np.array(each_slice.pixel_array),(IMG_PXL_SIZE,IMG_PXL_SIZE))
            #y.imshow(slices[0].pixel_array)
            #y.imshow(each_slice)

        plt.show()

    if label == 1: label = np.array([0,1])
    elif label == 0: label = np.array([1,0])

    return np.array(new_slices), label

如果有人能帮我解决这个问题,那将是非常有帮助的。

【问题讨论】:

  • 你用的是什么版本的熊猫? get_values 自 0.21.0 版起已弃用
  • 我使用的是 1.0.3 版。 1.0.3版本中是否有其他命令可以代替get_values
  • 自 0.21.0 版起已弃用:改用 .at[] 或 .iat[] 访问器。 .at 如果您需要 pandas.pydata.org/pandas-docs/stable/reference/api/…
  • 好的,谢谢。

标签: python pandas deep-learning attributeerror


【解决方案1】:

pandas.DataFrame.get_values() 已弃用:

Deprecated since version 0.25.0: Use np.asarray(..) or DataFrame.values() instead.

source


替代方案:

import pandas as pd
df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30]], index=[4, 5, 6], columns=['A', 'B', 'C'])


df.values

array([[ 0,  2,  3],
       [ 0,  4,  1],
       [10, 20, 30]], dtype=int64)


import numpy as np
np.asarray(df)

array([[ 0,  2,  3],
       [ 0,  4,  1],
       [10, 20, 30]], dtype=int64)

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2013-10-23
    • 2017-01-24
    • 2018-10-10
    • 2019-08-18
    • 2021-01-20
    • 2020-05-16
    • 2018-10-04
    相关资源
    最近更新 更多