Scipy Optimize is only returning x0, only completing one iteration

2024/10/12 21:25:06

I am using scipy optimize to get the minimum value on the following function:

def randomForest_b(a,b,c,d,e):return abs(rf_diff.predict([[a,b,c,d,e]]))

I eventually want to be able to get the optimal values of (a) and (b) given the arguments (c,d,e). However, just to learn how to work the optimize function, I am trying to get the optimal value of (a) given the other arguments. I have the following code:

res=optimize.minimize(randomForest_b, x0=45,args=(119.908500,65.517527,2.766103,29.509200), bounds=((45,65),))
print(res) 

And I have even tried:

optimize.fmin_slsqp(randomForest_b, x0=45,args=(119.908500,65.517527,2.766103,29.509200), bounds=((45,65),))

However, both of these just return the x0 value.

Optimization terminated successfully.    (Exit mode 0)Current function value: 1.5458542752157667Iterations: 1Function evaluations: 3Gradient evaluations: 1
array([ 45.])

The current function value is correct, however between all numbers within the bounds, the x0 does not return the minimum function value. I have the bounds set because the variable a can only be a number between 45 and 65. Am I missing something or doing something wrong? And if possible, how can I get optimal values of a and b?

Here is an example of the complete code I am using:

    from numpy import arrayimport scipy.optimize as optimizefrom scipy.optimize import minimizea=np.random.uniform(low=4.11, high=6.00, size=(50,))b=np.random.uniform(low=50.11, high=55.99, size=(50,))c=np.random.uniform(low=110.11, high=120.99, size=(50,))d=np.random.uniform(low=50.11, high=60.00, size=(50,))pv=np.random.uniform(low=50.11, high=60.00, size=(50,))df=pd.DataFrame(a, columns=['a'])df['b']=bdf['c']=cdf['d']=ddf['pv']=pvdf['difference']=df['pv']-df['d']from sklearn.model_selection import train_test_split y=df.loc[:, 'difference']x=df.iloc[:, [0,1,2,3]]x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)from sklearn.ensemble import RandomForestRegressorrf_difference = RandomForestRegressor(n_estimators = 1000, oob_score=True, random_state = 0)rf_difference.fit(x_train, y_train) def randomForest_b(a,b,c,d):return abs(rf_difference.predict([[a,b,c,d]]))res=optimize.minimize(randomForest_b, x0=0,args=(51.714088,110.253656,54.582179), bounds=((0,6),))print(res)optimize.fmin_slsqp(randomForest_b, x0=0,args= (51.714088,110.253656,54.582179), bounds=((0,6),))
Answer

The function you are trying to minimize is not smooth and has also several plateaus, this can be seen by plotting randomForest_b as a function of a:

a = np.linspace(0,6,500)
args = 51.714088,110.253656,54.582179
vrandomForest_b = np.vectorize(randomForest_b,excluded=[1,2,3])
y_values = vrandomForest_b(a, *args)fig, ax = plt.subplots(figsize=(8,6))
ax.plot(a, y_values, label='randomForest_b')
ax.axvline(0, label='Your start value', color='g', ls='--')
ax.set(xlabel='a', ylabel='randomForest_b');
ax.legend()

For non-smooth functions like yours, gradient-based optimization techniques will fail almost certainly. In this case, the starting value of 0 is on a plateau with vanishing gradient, therefore the optimization finishes immediately after one iteration.

A solution would be to use non-gradient based optimization methods, for example stochastic minimization with scipy.optimize.differential_evolution. A caveat of these methods is that they usually require more function evaluations and can take longer to finish.

This optimization method is able to find the global minimum in the example case given in your question:

rslt = optimize.differential_evolution(vrandomForest_b,args=(51.714088,110.253656,54.582179), bounds=[(0,6)])
print(rslt)fig, ax = plt.subplots()
ax.plot(a, y_values, label='randomForest_b')
ax.axvline(rslt.x, label='Minimum', color='red', ls='--')
ax.legend()
 fun: 0.054257768073620746 message: 'Optimization terminated successfully.'nfev: 152nit: 9  success: Truex: array([5.84335956])
https://en.xdnf.cn/q/69607.html

Related Q&A

Order of sess.run([op1, op2...]) in Tensorflow

I wonder whats the running order of the op list in sess.run(ops_list, ...). for example:for a typical classification scenario: _, loss = sess.run([train_op, loss_op]), if train_op run first,then the lo…

Django form validation: get errors in JSON format

I have this very simple Django formfrom django import formsclass RegistrationForm(forms.Form):Username = forms.CharField()Password = forms.CharField()I manage this manually and dont use the template en…

Django inheritance and polymorphism with proxy models

Im working on a Django project that I did not start and I am facing a problem of inheritance. I have a big model (simplified in the example) called MyModel that is supposed to represents different kind…

L suffix in long integer in Python 3.x

In Python 2.x there was a L suffix after long integer. As Python 3 treats all integers as long integer this has been removed. From Whats New In Python 3.0:The repr() of a long integer doesn’t include …

Custom Colormap

I want to plot a heatmap with a custom colormap similar to this one, although not exactly.Id like to have a colormap that goes like this. In the interval [-0.6, 0.6] the color is light grey. Above 0.6,…

Whats the point of @staticmethod in Python?

Ive developed this short test/example code, in order to understand better how static methods work in Python.class TestClass:def __init__(self, size):self.size = sizedef instance(self):print("regul…

logical or on list of pandas masks

I have a list of boolean masks obtained by applying different search criteria to a dataframe. Here is an example list containing 4 masks: mask_list = [mask1, mask2, mask3, mask4]I would like to find th…

How to view the implementation of pythons built-in functions in pycharm?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. How to view the actual implementation so that I could know what exactly the built-in f…

How to gracefully fallback to `NaN` value while reading integers from a CSV with Pandas?

While using read_csv with Pandas, if i want a given column to be converted to a type, a malformed value will interrupt the whole operation, without an indication about the offending value.For example, …

Python - object layout

can somebody describe the following exception? What is the "object layout" and how it is defined? ThanksTraceback (most recent call last):File "test_gui.py", line 5, in <module…