У меня была та же проблема, я округлял для решения первого контрольного примера и, таким образом, проваливал второй ... Это небольшая выборка, регрессия с одной переменной, так что на самом деле похоже, что вы не можете использовать ванильную регрессию, но 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))