我的代码可以从googletrends的搜索词中找到线性回归方程。以下是工作代码:
from pytrends.request import TrendReq
google_username = "xxx@gmail.com"
google_password = "xxx"
path = ""
keyword = ["stackoverflow"]
pytrend = TrendReq(google_username, google_password, custom_useragent='')
pytrend.build_payload(kw_list=keyword, timeframe='today 5-y', geo='MX')
def regression(x):
df = pytrend.interest_over_time()[x]
df.insert(0, 'x', range(1, 1 + len(df)))
df.columns = ['x', 'y']
x,y = df['x'], df['y']
x_raya = []
cuad = []
x_mean = x.mean()
y_raya = []
y_mean = y.mean()
for xs in x:
x_raya.append(xs - x_mean)
cuad.append(xs**2)
for ys in y:
y_raya.append(ys - y_mean)
mult = [x_raya[i]*y_raya[i] for i in range(len(x_raya))]
b1 = sum(mult)/sum(cuad)
b0 = y_mean-(b1*x_mean)
print("The equation is %s + %s x" % (b0,b1))
regression(keyword)
Out: The equation is 41.1203123741 + 0.010605085267 x
我的问题是,每当我尝试在关键字中添加更多单词时:
from pytrends.request import TrendReq
google_username = "xxx@gmail.com"
google_password = "xxx"
path = ""
keyword = ["stackoverflow", "reddit"]
pytrend = TrendReq(google_username, google_password, custom_useragent='')
pytrend.build_payload(kw_list=keyword, timeframe='today 5-y', geo='MX')
def regression(x):
df = pytrend.interest_over_time()[x]
df.insert(0, 'x', range(1, 1 + len(df)))
df.columns = ['x', 'y']
x,y = df['x'], df['y']
x_raya = []
cuad = []
x_mean = x.mean()
y_raya = []
y_mean = y.mean()
for xs in x:
x_raya.append(xs - x_mean)
cuad.append(xs**2)
for ys in y:
y_raya.append(ys - y_mean)
mult = [x_raya[i]*y_raya[i] for i in range(len(x_raya))]
b1 = sum(mult)/sum(cuad)
b0 = y_mean-(b1*x_mean)
print("The equation is %s + %s x" % (b0,b1))
regression(keyword)
Out: ValueError: Length mismatch: Expected axis has 3 elements, new values have 2 elements
关于如何让代码遍历列表中的各个元素,有什么建议吗?你知道吗
你在问如何迭代列表?你知道吗
尽管没有看到
pytrend.build_payload()
代码的代码,我不知道它是否可以接受和处理列表,或者它是否需要成为迭代的一部分。为了安全起见,您可能应该这样重新排列代码:相关问题 更多 >
编程相关推荐