gpt4 book ai didi

TypeError: 'float' object does not support item assignment in numpy array for parallel loops(TypeError:“Float”对象不支持并行循环的Numpy数组中的项赋值)

转载 作者:bug小助手 更新时间:2023-10-25 17:42:52 27 4
gpt4 key购买 nike



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")

But getting error as
_RemoteTraceback:

但收到错误AS_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

更多回答

Do you know whatTypeError: 'float' object does not support item assignment means? It's saying that you're trying to assign to an item of a float, but floats don't have items. Your problem is this: mindvfij = 0.0 and mindvfji = 0.0, followed by mindvfij[i-1,j-1]= a and mindvfji[i-1,j-1]= b.

您知道TypeError:‘Float’对象不支持项分配意味着什么吗?这就是说,你正试图给一个浮点数的项赋值,但是浮点数没有项。你的问题是:Mindvfij=0.0和Mindvfji=0.0,紧随其后的是Mindvfij[i-1,j-1]=a和Mindvfji[i-1,j-1]=b。

since i am updating mindvfij and mindvfji for those i and j for which my condition satisfies. that is why i initialize them to 0.0 so that no random value should not be there for rest of i and j. earlier i did not initialized then it picked 1e-45 for some of the i and j. Please suggest

因为我正在为我的条件满足的那些i和j更新Mindvfij和Mindvfji。这就是我将它们初始化为0.0的原因,这样I和J的其余部分就不会有随机值。

Are you trying to initialize the values in an array named mindvfij? That's not what mindvfij = 0.0 does; that line of code binds the name mindvfij to the float 0.0. Maybe you want mindvfij[i,j] = 0.0? But then why not just use np.zeros instead of np.empty?

您是否正在尝试初始化名为Mindvfij的数组中的值?这并不是Mindvfij=0.0所做的事情;该行代码将名称Mindvfij绑定到Float 0.0。也许你想要Minvfij[i,j]=0.0?但为什么不直接使用np.zeros而不是np.Empty呢?

as per your suggestion i did set mindvfij = np. zeros((n,n), dtype='float64') mindvfji = np. zeros((n,n), dtype='float64') but know i got the error as assignment destination is read-only

根据您的建议,我确实设置了Mindvfij=NP。零((n,n),dtype=‘flat64’)Mindvfji=Np。零((n,n),dtype=‘flat64’),但我知道我收到了错误,因为分配目标是只读的

Was the line number for the error somewhere else in the code? I don't think a simple assignment statement binding a name to an object will raise that error.

错误的行号是否在代码中的其他位置?我不认为将名称绑定到对象的简单赋值语句会引发该错误。

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com