提问人:Rahul Gupta 提问时间:9/10/2023 最后编辑:jaredRahul Gupta 更新时间:9/10/2023 访问量:62
TypeError:“float”对象不支持在并行循环的 numpy 数组中分配项目
TypeError: 'float' object does not support item assignment in numpy array for parallel loops
问:
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
答: 暂无答案
评论
TypeError: 'float' object does not support item assignment
mindvfij = 0.0
mindvfji = 0.0
mindvfij[i-1,j-1]= a
mindvfji[i-1,j-1]= b
mindvfij
mindvfij = 0.0
mindvfij
mindvfij[i,j] = 0.0
np.zeros
np.empty