Python线程错误-必须是可迭代的,而不是int [英] Python threading error - must be an iterable, not int
问题描述
我正在尝试计算数据帧中第一列和其他列(第一列和第二列,第一列和第三列等)之间回归的滚动r平方,但是当我尝试线程化时,它一直告诉我该错误
I'm trying to calculate rolling r-squared of regression among first column and other columns in a dataframe (first column and second, first column and third etc.) But when I try threading, it kept telling me the error that
TypeError:*之后的ParallelRegression()参数必须是可迭代的,而不是int.
TypeError: ParallelRegression() argument after * must be an iterable, not int".
我想知道如何解决此问题?非常感谢!
I'm wondering how do I fix this? Thanks very much!
import threading
totalThreads=3 #three different colors
def ParallelRegression(threadnum):
for i in range(threadnum):
res[:,i]=sm.OLS(df.iloc[:,0], df.iloc[:,i+1]).fit().rsquared
threads=[]
for threadnum in range(totalThreads):
t=threading.Thread(target=ParallelRegression,args=(threadnum))
threads.append(t)
t.start()
for threadnum in range(totalThreads):
threads[threadnum].join()
在下面链接的图片中查看数据(df)的摘要:
See a summary of the data (df) in the picture linked below:
推荐答案
threading.Thread
类需要一个可迭代的参数作为args
参数.您要传递的args=(threadnum)
是单个int
对象,您需要传递一些可迭代的对象,该对象将允许多个arg,即使您只想传递一个arg.
threading.Thread
class needs an iterable of arguments as the args
parameter. You're passing args=(threadnum)
which is a single int
object, you need to pass some iterable object that would allow multiple args, even when you only want to pass one arg.
args=[threadnum]
可以工作,因为这使得list
可以迭代.
args=[threadnum]
would work, because that makes a list
which is iterable.
这篇关于Python线程错误-必须是可迭代的,而不是int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!