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

2024-09-24 02:28:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码可以从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

关于如何让代码遍历列表中的各个元素,有什么建议吗?你知道吗


Tags: indfforgoogleusernamemeankeywordb1
1条回答
网友
1楼 · 发布于 2024-09-24 02:28:16

你在问如何迭代列表?你知道吗

# 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)

相关问题 更多 >