错误'numpy.int32'对象不支持项目分配

时间:2015-06-05 11:14:01

标签: python numpy

我收到此错误

Traceback (most recent call last):
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 90,     in <module>
ec.combine_classifiers("Event labels combined")
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 53, in combine_classifiers
pnas_class[pnas_class==3] = 1
TypeError: 'numpy.int32' object does not support item assignment

运行代码

def combine_classifiers(self, output_name):
    all_combined_classes = []
    for _, (plate_name, w, p, t1, t2, track_ids, track_labels) in self.mapping[['Plate', 
                                                                        'Well', 
                                                                        'Site', 
                                                                        'Gene Symbol',
                                                                        'siRNA ID', 
                                                                        'Event track ids',
                                                                        'Event track labels']].iterrows(): 
        combined_classes = []
        ch5_file_handle = self.cellh5_handles[plate_name]
        ch5_pos = ch5_file_handle.get_position(w, str(p))



        for track_id, track_label in zip(track_ids, track_labels):

            h2b_class = track_label.copy()
            print(track_id)
            pnas_class = ch5_pos.get_class_prediction('secondary__expanded')[track_id]['label_idx'] + 1
            print(pnas_class)


            inter_idx = h2b_class == 1 
            pnas_class[pnas_class==3] = 1
            pnas_class[pnas_class==2]+=2
            combined_class = h2b_class
            combined_class[inter_idx] = pnas_class[inter_idx] 
            combined_classes.append(combined_class)

        all_combined_classes.append(combined_classes)

    self.mapping[output_name] = pandas.Series(all_combined_classes) 

我打印pnas_class为1,track_id为50708.我想知道代码设计者想要做什么:

inter_idx = h2b_class == 1 
pnas_class[pnas_class==3] = 1
pnas_class[pnas_class==2]+=2
combined_class = h2b_class
combined_class[inter_idx] = pnas_class[inter_idx] 

如何将其更改为具有相同含义?

2 个答案:

答案 0 :(得分:1)

pnas_class是一个整数,因此您无法通过[pnas_class==3] = 1从整数中选择项目。

如果它等于3,也许你试图影响1到pnas_class。在这种情况下试试这个:

pnas_class= 1*(pnas_class == 3) + pnas_class*(pnas_class != 3 )

答案 1 :(得分:0)

好的,我发现了错误。你是对的pnas_class不应该是一个整数,我知道为什么它是整数而不是数组。