Python Any help with this error in Scipy minimize function?

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Error Function
AI Thread Summary
The discussion centers around an error encountered while executing a Python script that uses SciPy's optimization functions. The user is attempting to minimize a function defined in the code but receives an AttributeError indicating that the OptimizeResult object does not have an 'F' attribute. Instead, the correct attribute to access the results of the optimization is 'x'. Participants suggest printing the entire result object to identify its available attributes and gain additional insights into the optimization process. This advice is noted as particularly helpful for troubleshooting the issue.
member 428835
Hi PF!

When I execute the code below:
Python:
import numpy as np
from scipy.stats import t
import scipy.optimize as optimizeglobal data
data = np.random.normal(loc=50, scale=1, size=(2400, 1)).flatten()

def L(F):
    M = 250
    lmda = 0.97
    sig_0 = F[0]
    for i in range(1, 12):
        sig_0 += F[i]

    number_of_days = len(data)

    sig = np.ones(number_of_days)

    for day in range(M, number_of_days):
        sig[day] = lmda * sig[day - 1] + (1 - lmda) * np.square(data[day - 1]) / F[day % 12] * sig_0

    data_days = np.delete(data, range(M - 1), 0)

    L_mat = np.log(t.pdf(data_days, df=3))
    L_sum = L_mat.sum(axis=0)

    mu = F[12]
    Reg = (F[11] - 2 * F[0] + F[1]) ** 2 + (F[10] - 2 * F[11] + F[0]) ** 2
    for i in range(1, 11):
        Reg += (F[i - 1] - 2 * F[i] + F[i + 1]) ** 2
    Reg *= -1 / sig_0 ** 2
    Reg *= -mu / sig_0 ** 2
    Reg += 12 / 2 * np.log(mu)

    print(F)

    final_L = -(L_sum + Reg)
    return final_L

bnds = [(None, None), (None, None), (None, None), (None, None), (None, None), (None, None), (None, None),
        (None, None), (None, None), (None, None), (None, None), (None, None), (2, 100)]
IC = 3*np.ones(13)
result = optimize.minimize(L, method='TNC', bounds=bnds, x0=IC)
print(result.F)

I get an error after many iterations that says:

Code:
Traceback (most recent call last):  File "/usr/local/lib/python3.9/site-packages/scipy/optimize/_optimize.py", line 124, in __getattr__    return self[name]KeyError: 'F'The above exception was the direct cause of the following exception:Traceback (most recent call last):  File "/Users/joshmccraney/Desktop/ewma/test_seas_ewma.py", line 45, in <module>    print(result.F)  File "/usr/local/lib/python3.9/site-packages/scipy/optimize/_optimize.py", line 126, in __getattr__    raise AttributeError(name) from eAttributeError: F
Do you know why this error is being thrown? I know the code below is messy, but this is the simplest I could make it to produce the error.
 
Technology news on Phys.org
The OptimizeResult returned by scipy.optimize.minimize has no 'F' attribute. You want the value of its 'x' attribute.
 
  • Like
Likes member 428835 and pbuk
pasmith said:
The OptimizeResult returned by scipy.optimize.minimize has no 'F' attribute. You want the value of its 'x' attribute.
Or just print(result) and then you will (i) see what attributes it does have, and (ii) gain other useful information about the optimization.
 
  • Like
Likes member 428835
This was SO helpful! Thank you both!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
15
Views
2K
Replies
16
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
3
Views
2K
Replies
6
Views
4K
Back
Top