[Python] how to use goal seek functionality in machine learning model [closed]

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement goal seek functionality in my regression model. My model is already built and got the forecasting value for Jan25, Feb25, March25 now i want to do reverse if i know target value for Jan25, Feb25, March25 then what could be input features value for these month. The input features which i want to change are Sb and Lead. But using this code i am getting similar values for SB and Lead for Jan25, Feb25, March25 despite my target value are different.

    target_values = {
    '2025-01-01': 12000,
    '2025-02-01': 15000,
    '2025-03-01': 16000
    }

    base_data = pd.DataFrame(index=pd.to_datetime(['2025-01-01', '2025-02-01', '2025-03-01']))
    base_data['ds'] = base_data.index

    base_data['month'] = base_data['ds'].dt.month
    base_data['day_of_year'] = base_data['ds'].dt.dayofyear
    base_data['sin_month'] = np.sin(2 * np.pi * base_data['month'] / 12)
    base_data['cos_month'] = np.cos(2 * np.pi * base_data['month'] / 12)

    if test_data.index.duplicated().any():
    print("Warning: test_data has duplicate index values. Resetting index.")
    test_data = test_data.reset_index(drop=True)

    last_row = test_data.iloc[-1].copy()

    static_cols = ['a', 'b', 'c', 'd', 'e',
    'f', 'g', 'h']


    for col in static_cols:
    base_data[col] = last_row[col]

    base_data['SB'] = last_row['SB']
    base_data['Leads'] = last_row['Leads']

    def predict_y(inputs, base_row, scaler, model):
    row = base_row.copy()
    row['SB'] = inputs[0]
    row['Leads'] = inputs[1]
    X = row[['SB', 'a', 'b', 'sin_month','cos_month','c', 'd', 'e',
    'f', 'g', 'h',
    'Leads']].values.reshape(1, -1)
    X_scaled = scaler.transform(X)
    prediction = model.predict(X_scaled)[0]
    print(f"Inputs for {base_row.name}: SB={inputs[0]:.2f}, Leads={inputs[1]:.2f}, Predicted y={prediction:.2f}")
    return prediction

    def objective_function(inputs, target, base_row, scaler, model):
    predicted_y = predict_y(inputs, base_row, scaler, model)
    print(f"Target for {base_row.name}: {target}, Predicted y: {predicted_y:.2f}, Difference: {(predicted_y - target)**2:.2f}")
    return (predicted_y - target) ** 2

    results = {}
    for date, target in target_values.items():
    base_row = base_data.loc[date]
    initial_guess = [base_row['SB'], base_row['Leads']]

    print(f"\nOptimizing for {date} with target {target}")
    print(f"Initial Guess: SB={initial_guess[0]:.2f}, Leads={initial_guess[1]:.2f}")

    result = minimize(
    objective_function,
    initial_guess,
    args=(target, base_row, scaler, model13),
    method='Powell',
    bounds=[(0, None), (0, None)],
    options={'disp': True}
    )

    if result.success:
    results[date] = {'SB': result.x[0], 'Leads': result.x[1]}
    else:
    results[date] = {'Error': 'Optimization failed'}

    for date, vals in results.items():
    print(f"\nFor {date}:")
    if 'Error' in vals:
    print(vals['Error'])
    else:
    print(f"SB = {vals['SB']:.2f}, Leads = {vals['Leads']:.2f}")

    print(f"\nFinal optimization details for {date}:")
    print(f"Initial Guess: SB={initial_guess[0]:.2f}, Leads={initial_guess[1]:.2f}")
    print(f"Success: {result.success}, Final Values: SB={result.x[0]:.2f}, Leads={result.x[1]:.2f}")

    Continue reading...

Compartilhe esta Página