TypeError:“float”对象不支持在并行循环的 numpy 数组中分配项目

TypeError: 'float' object does not support item assignment in numpy array for parallel loops

提问人:Rahul Gupta 提问时间:9/10/2023 最后编辑:jaredRahul Gupta 更新时间:9/10/2023 访问量:62

问:

import numpy as np 
import pandas as pd
from joblib import Parallel, delayed
import multiprocessing

df = pd.read_csv("/media/rahul/cd854f04-608f-4627-9e70-9096a8520b95/feature_bond/real_malware_v1/extracted_real_malware_v1.csv")
fields =[]
fields = list(df.columns)
num_cores = multiprocessing.cpu_count()
#print(fields)
#print(len(fields))
n = len(fields)
n=n-1
    #print(len(fields))
mindvfij = np. empty((n,n), dtype='float64')
mindvfji = np. empty((n,n), dtype='float64')
print(mindvfij.dtype)
print(mindvfij.ndim)
print(mindvfij.shape)
    
print(df)
print(df.iloc[0,5])
x= df.iloc[0,5]
print(type(x))
print(len(df))
for i in range(0,n):
    for j in range(0,n):
        mindvfij = 0.0
        mindvfji = 0.0
def my_function(i,j):
    
    #print(j)
    nfij = 0
    nfi = 0
    nfj = 0
    for k in range(0, len(df)):
        x = df.iloc[k,i]
        y = df.iloc[k,j]
        if(x==1 and y==1):
            #print(nfij)
            nfij = nfij+1
                
            #print(nfij)
        if(x==1):
            nfi = nfi+1
        if(y==1):
            nfj = nfj+1
            #print("i is"+str(i)+"j is"+str(j)+"value is"+str(nfi) +":" + str(nfj) + ":" +str(nfij))
    if(nfi!=0):
        #print("i is:"+str(i)+"j is:"+str(j))
        a= nfij/nfi
        mindvfij[i-1,j-1]= a
 
    if(nfj!=0):
        #print("i is:"+str(i)+"j is:"+str(j))
        b = nfij/nfj
        mindvfji[i-1,j-1]= b
                
    if(nfi>0 and nfj>0):
        print("i is"+str(i)+"j is"+str(j)+"value is"+str(mindvfij[i-1,j-1]))
for i in range(1, len(fields)):
    print(i)
    Parallel(n_jobs=num_cores)(delayed(my_function)(i,j) for j in range(i+1, len(fields)))
np.save("/media/rahul/cd854f04-608f-4627-9e70-9096a8520b95/feature_bond/malindvfij",mindvfij)
np.save("/media/rahul/cd854f04-608f-4627-9e70-9096a8520b95/feature_bond/malindvfji",mindvfji)
print("Finish")

但是得到错误 _RemoteTraceback:

Traceback (most recent call last):
  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/externals/loky/process_executor.py", line 436, in _process_worker
    r = call_item()
  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/externals/loky/process_executor.py", line 288, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/_parallel_backends.py", line 595, in __call__
    return self.func(*args, **kwargs)
  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 262, in __call__
    return [func(*args, **kwargs)
  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 262, in <listcomp>
    return [func(*args, **kwargs)
  File "/home/rahul/.config/spyder-py3/untitled2.py", line 57, in my_function
TypeError: 'float' object does not support item assignment
"""


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "/home/rahul/.config/spyder-py3/untitled2.py", line 63, in <module>
    Parallel(n_jobs=num_cores)(delayed(my_function)(i,j) for j in range(i+1, len(fields)))

  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 1056, in __call__
    self.retrieve()

  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 935, in retrieve
    self._output.extend(job.get(timeout=self.timeout))

  File "/home/rahul/anaconda3/lib/python3.9/site-packages/joblib/_parallel_backends.py", line 542, in wrap_future_result
    return future.result(timeout=timeout)

  File "/home/rahul/anaconda3/lib/python3.9/concurrent/futures/_base.py", line 445, in result
    return self.__get_result()

  File "/home/rahul/anaconda3/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
    raise self._exception

TypeError: 'float' object does not support item assignment
python numpy 并行处理 浮点

评论

0赞 Joe 9/10/2023
你知道是什么意思吗?它说您正在尝试分配给浮点数的项,但浮点数没有项。你的问题是这样的:和 ,后跟 和 。TypeError: 'float' object does not support item assignmentmindvfij = 0.0mindvfji = 0.0mindvfij[i-1,j-1]= amindvfji[i-1,j-1]= b
0赞 Rahul Gupta 9/10/2023
因为我正在为我的条件满足的 i 和 j 更新 mindvfij 和 mindvfji。这就是为什么我将它们初始化为 0.0,这样 I 和 J 的其余部分就不应该有随机值。请建议
1赞 Joe 9/10/2023
您是否正在尝试初始化名为 ?但事实并非如此;该代码行将名称绑定到 float 0.0。也许你想要?但是为什么不直接使用而不是呢?mindvfijmindvfij = 0.0mindvfijmindvfij[i,j] = 0.0np.zerosnp.empty
0赞 Rahul Gupta 9/10/2023
根据您的建议,我确实设置了 mindvfij = np。zeros((n,n), dtype='float64') mindvfji = np.zeros((n,n), dtype='float64') 但知道我收到错误,因为赋值目的地是只读的
0赞 Joe 9/10/2023
错误的行号是否在代码中的其他位置?我不认为将名称绑定到对象的简单赋值语句会引起该错误。

答: 暂无答案