TestDome Data Science: Не получается правильный ответ - PullRequest
0 голосов
/ 29 июня 2018

Я попытался ответить на этот вопрос из 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))

Ответы [ 3 ]

0 голосов
/ 16 апреля 2019
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 = np.asarray(marketing_expenditure).reshape(-1, 1)
        units_sold = np.asarray(units_sold).reshape(-1, 1)
        reg = linear_model.LinearRegression()
        reg.fit(marketing_expenditure , units_sold)
        return np.float((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))
0 голосов
/ 28 июля 2019

У меня была та же проблема, я округлял для решения первого контрольного примера и, таким образом, проваливал второй ... Это небольшая выборка, регрессия с одной переменной, так что на самом деле похоже, что вы не можете использовать ванильную регрессию, но Theil -Sen регресс. Я проверил результат, и он достиг 250000.00003619, который вы просто округлили.

Источник: https://gist.github.com/mfakbar/f97949299171c75e868a37f3f578fa54

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))
0 голосов
/ 13 апреля 2019

Я думаю, что это решение, потому что мы ищем, чтобы предсказать X из y, а метка в этой задаче - unit_sold.

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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...