2017-07-01 60 views
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 

如何使通過列表中的各種元素的代碼迭代有什麼建議?

回答

0

你在問如何迭代一個列表?

# Generalize list variable name 
keyword_list = ["stackoverflow", "reddit"] 

# Skip down to where you call your regression function 
... 

# And...use a basic iteration 
for keyword in keyword_list: 
    regression(keyword) 

雖然沒有看到代碼,您pytrend.build_payload()代碼,我不知道是否可以受理的列表,或者如果它需要迭代的一部分。爲了安全起見,你應該做的是重新排列你的代碼,如下所示:

from pytrends.request import TrendReq 

google_username = "[email protected]" 
google_password = "xxx" 
path = "" 

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 for keyword {} is {} + {} x".format(keyword, b0,b1)) 

keyword_list = ["stackoverflow", "reddit"] 

for keyword in keyword_list: 
    pytrend = TrendReq(google_username, google_password, custom_useragent='') 
    pytrend.build_payload(kw_list=keyword, timeframe='today 5-y', geo='MX') 

    regression(keyword)