Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TestDome数据科学:没有得到正确答案_Python_Python 3.x_Scikit Learn - Fatal编程技术网

Python TestDome数据科学:没有得到正确答案

Python TestDome数据科学:没有得到正确答案,python,python-3.x,scikit-learn,Python,Python 3.x,Scikit Learn,我试图从TestDome中回答这个问题,得到的是250877.19298245612,而不是建议的250000。请告诉我哪里出了问题。谢谢 import numpy as np from sklearn import linear_model class MarketingCosts: # param marketing_expenditure list. Expenditure for each previous campaign. # param units_sold l

我试图从TestDome中回答这个问题,得到的是250877.19298245612,而不是建议的250000。请告诉我哪里出了问题。谢谢

import numpy as np
from sklearn import linear_model

class MarketingCosts:

    # param marketing_expenditure list. Expenditure for each previous campaign.
    # param units_sold list. The number of units sold for each previous campaign.
    # param desired_units_sold int. Target number of units to sell in the new campaign.
    # returns float. Required amount of money to be invested.
    @staticmethod
    def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
        X = [[i] for i in units_sold]
        reg = linear_model.LinearRegression()
        reg.fit(X, marketing_expenditure)
        return float(reg.predict(desired_units_sold))

#For example, with the parameters below the function should return 250000.0.
print(MarketingCosts.desired_marketing_expenditure(
    [300000, 200000, 400000, 300000, 100000],
    [60000, 50000, 90000, 80000, 30000],
    60000))

我认为这是解决方案,因为我们搜索从y预测X,这个问题的标签是出售的单位

import numpy as np
from sklearn import linear_model

class MarketingCosts:

    # param marketing_expenditure list. Expenditure for each previous campaign.
    # param units_sold list. The number of units sold for each previous campaign.
    # param desired_units_sold int. Target number of units to sell in the new campaign.
    # returns float. Required amount of money to be invested.
    @staticmethod
    def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
        marketing_expenditure = marketing_expenditure.reshape(-1, 1)
        units_sold = units_sold.reshape(-1, 1)
        reg = linear_model.LinearRegression()
        reg.fit(marketing_expenditure , units_sold)
        return (desired_units_sold - reg.intercept_)/reg.coef_

#For example, with the parameters below the function should return 250000.0.
print(MarketingCosts.desired_marketing_expenditure(
    [300000, 200000, 400000, 300000, 100000],
    [60000, 50000, 90000, 80000, 30000],
    60000))

我也遇到了同样的问题,我正在四舍五入解决第一个测试用例,因此第二个测试用例失败了。。。这是一个小样本,单变量回归,所以实际上看起来你不能使用普通回归,而是泰尔森回归。我检查了结果,结果是250000.00003619,然后你只需四舍五入

资料来源:


以下是我通过所有测试的答案:

import numpy as np
from sklearn.linear_model import LinearRegression

def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):

    x = np.array(marketing_expenditure).reshape(-1, 1)
    y = np.array(units_sold).reshape(-1, 1)
    model = LinearRegression()
    model.fit(x , y)

    return (desired_units_sold - model.intercept_)/model.coef_    
下面是我通过所有测试用例的答案 您可以找到执行线性回归的简单步骤


我认为250877接近25000。差异小于0.5%。代码似乎是对的。也许你可以对输出进行后处理,使数字四舍五入。你的答案通过了1次测试。如果你将它四舍五入到25000,你通过了另一个测试,但没有通过之前通过的测试。不知道如何通过最后一次考试。它似乎有问题。我们可以更详细地说明为什么不能使用sklearn的默认LinearRegression()吗?这是第一个正确答案。关键是X=营销支出,Y=销售单位。但是,您不需要使用predict函数(它给出250870)。您将获得“所需的_单位”,这是Y变量,并要求您找出X变量。-这意味着a*x+b=60000。现在为x求解。感谢您提供这段代码片段,它可能会提供一些有限的短期帮助。通过说明为什么这是一个很好的问题解决方案来正确解释它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在您的回答中添加一些解释,包括您所做的假设。
import numpy as np
from sklearn import linear_model

class MarketingCosts:

    # param marketing_expenditure list. Expenditure for each previous campaign.
    # param units_sold list. The number of units sold for each previous campaign.
    # param desired_units_sold int. Target number of units to sell in the new campaign.
    # returns float. Required amount of money to be invested.
    @staticmethod
    def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
        y, x = np.array(marketing_expenditure), np.array(units_sold).reshape(-1, 1)
        regressor = linear_model.TheilSenRegressor(max_subpopulation=10)
        regressor.fit(x, y)
        desired_units_sold = np.array([desired_units_sold]).reshape(-1, 1)
        return float(round(regressor.predict(desired_units_sold).item()))

# For example, with the parameters below the function should return 250000.0.
print(MarketingCosts.desired_marketing_expenditure(
    [300000, 200000, 400000, 300000, 100000],
    [60000, 50000, 90000, 80000, 30000],
    60000))
import numpy as np
from sklearn.linear_model import LinearRegression

def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):

    x = np.array(marketing_expenditure).reshape(-1, 1)
    y = np.array(units_sold).reshape(-1, 1)
    model = LinearRegression()
    model.fit(x , y)

    return (desired_units_sold - model.intercept_)/model.coef_    
import numpy as np
from sklearn.linear_model import LinearRegression

def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
    s_x = sum(marketing_expenditure)
    s_y = sum(units_sold)
    xy = [] 
    for i in range (len(marketing_expenditure)):
        z= marketing_expenditure[i]*units_sold[i]
        xy.append(z)
    s_xy = sum(xy)
    sq_x = [number ** 2 for number in marketing_expenditure]
    s_sq_x = sum(sq_x)
    sq_y = [number ** 2 for number in units_sold]
    s_sq_y = sum(sq_y)   
    
    # calculating coefficients a and b for liner regression
    a=((s_y*s_sq_x) - (s_x*s_xy))/(len(marketing_expenditure)*s_sq_x - (s_x**2))
    b=(len(marketing_expenditure)*s_xy - (s_x*s_y)) / 
       (len(marketing_expenditure)*s_sq_x - (s_x**2))
    return (desired_units_sold-a)/b


#For example, with the parameters below, the function should return 250000.0
print(desired_marketing_expenditure(
    [300000, 200000, 400000, 300000, 100000],
    [60000, 50000, 90000, 80000, 30000],
    60000))