在独立回归函数中循环多个元素

时间:2017-07-01 03:02:15

标签: python

我的代码可以从Google趋势中的搜索字词中找到线性回归方程式。这是工作代码:

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

有关如何使代码遍历列表中的各种元素的任何建议吗?

1 个答案:

答案 0 :(得分: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 = "xxx@gmail.com"
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)