1
我的代碼可以從Google Trends中的搜索詞中找到線性迴歸方程。這裏是工作代碼:在獨立迴歸函數中循環多個元素
from pytrends.request import TrendReq
google_username = "[email protected]"
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 = "[email protected]"
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
如何使通過列表中的各種元素的代碼迭代有什麼建議?