Skip to content

Commit 1a39a40

Browse files
Update README.md
1 parent fd6feac commit 1a39a40

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ Practical experiments on Machine Learning in Python. Processing of sentences and
1818
## Content
1919
Codes (it'll send you to appropriate file):
2020
* [Processing_Sentences](https://github.com/sichkar-valentyn/Machine_Learning_in_Python/tree/master/Codes/Processing_Sentences.py)
21-
* [Function_approximation](https://github.com/sichkar-valentyn/Machine_Learning_in_Python/tree/master/Codes/Function_approximation.py)
21+
* [Function_Approximation](https://github.com/sichkar-valentyn/Machine_Learning_in_Python/tree/master/Codes/Function_approximation.py)
22+
* [Function_Optimization](https://github.com/sichkar-valentyn/Machine_Learning_in_Python/tree/master/Codes/Function_Optimization.py)
2223

2324
<br/>
2425
Experimental results (figures and tables on this page):
2526

2627
* <a href="#Processing sentences and finding cosine distances">Processing sentences and finding cosine distances</a>
2728
* <a href="#Approximation of functions via linear equations">Approximation of functions via linear equations</a>
29+
* <a href="#Optimization of smooth and non-smooth functions">Optimization of smooth and non-smooth functions</a>
2830

2931
<br/>
3032

@@ -105,7 +107,92 @@ Results are plot in order to understand the quality of approximation in eache ca
105107

106108
![Approximation_of_function](images/Approximation_of_function.png)
107109

110+
<br/>
111+
112+
### <a name="Optimization of smooth and non-smooth functions">Optimization of smooth and non-smooth functions</a>
113+
Implementing the task for finding minimum of given smooth and non-smooth functions. Using **scipy.optimaze** library and two methods - **BFGS** and **differential evolution**.
114+
115+
Initial **smooth** function is as following (the same as it is in approximation task above):
116+
<br/>**f(x) = sin(x / 5) * exp(x / 10) + 5 * exp(-x / 2)**
117+
118+
Finding minimum of **smooth** function with **'BFGS'** method with start point **2**
119+
<br/><br/>Part of the code is shown below:
120+
121+
```py
122+
# Setting initial point in form of 'ndarray' as 'minimize' function requires it in form of 'ndarray'
123+
x0_start = np.array([2])
124+
# print(type(x)) --> <class 'numpy.ndarray'>
125+
# print(x.shape) --> (1,)
126+
# Finding minimum of the function from starting point 'x_start = 2'
127+
# Using 'minimize' function from 'scipy.optimize' library
128+
y0_min = optimize.minimize(f_array, x0_start, method='BFGS')
129+
print(y0_min) # fun = 1.7452682903449388, iterations = 6
130+
```
131+
132+
Finding minimum of **smooth** function with **'BFGS'** method with start point **30**
133+
<br/><br/>Part of the code is shown below:
134+
135+
```py
136+
# Setting initial point in form of 'ndarray' as 'minimize' function requires it in form of 'ndarray'
137+
x1_start = np.array([30])
138+
# print(type(x)) --> <class 'numpy.ndarray'>
139+
# print(x.shape) --> (1,)
140+
# Finding minimum of smooth function from starting point 'x_start = 30'
141+
# Using 'minimize' function from 'scipy.optimize' library
142+
y1_min = optimize.minimize(f_array, x1_start, method='BFGS')
143+
print(y1_min) # fun = -11.898894665981285, iterations = 6
144+
```
145+
146+
Finding minimum of **smooth** function with **'differential evolution'** method in range [1, 30]
147+
<br/><br/>Part of the code is shown below:
148+
149+
```py
150+
# Setting the range for searching in form of tuple inside list as function requires it
151+
x2_range = [(1, 30)] # tuple inside list
152+
# Finding minimum of smooth function
153+
y2_min = optimize.differential_evolution(f_array, [(1, 30)])
154+
print(y2_min) # fun = -11.89889467, iterations = 5
155+
```
156+
157+
Initial **non-smooth** function is takeen as an integer results of smooth function defined above:
158+
<br/>**h(x) = int(f(x))**
159+
<br/>Part of the code is shown below:
160+
```py
161+
# By using 'np.int_' we return 'numpy.ndarray' of integer numbers
162+
def h_array(k):
163+
return np.int_(f_array(k))
164+
```
165+
166+
Finding minimum of **non-smooth** function with **'BFGS'** method with start point **30**
167+
<br/><br/>Part of the code is shown below:
168+
169+
```py
170+
# Setting initial point in form of 'ndarray' as 'minimize' function requires it in form of 'ndarray'
171+
x3_start = np.array([30])
172+
# print(type(x)) --> <class 'numpy.ndarray'>
173+
# print(x.shape) --> (1,)
174+
# Finding minimum of the function from starting point 'x_start = 30'
175+
# Using 'minimize' function from 'scipy.optimize' library
176+
y3_min = optimize.minimize(h_array, x3_start, method='BFGS')
177+
print(y3_min) # fun = -5, iterations = 0
178+
```
179+
180+
Finding minimum of **non-smooth** function with **'differential evolution'** method in range [1, 30]
181+
<br/><br/>Part of the code is shown below:
182+
183+
```py
184+
# Setting the range for searching in form of tuple inside list as function requires it
185+
x4_range = [(1, 30)] # tuple inside list
186+
# Finding minimum of smooth function
187+
y4_min = optimize.differential_evolution(h_array, [(1, 30)])
188+
print(y4_min) # fun = -11.0, iterations = 3
189+
```
190+
191+
Full code is available here: [Function_Optimization.py](https://github.com/sichkar-valentyn/Machine_Learning_in_Python/tree/master/Codes/Function_Optimization.py)
192+
193+
Results are plot in order to understand the difference in finding minimums in smooth and non-smooth functions. Figure is shown below:
108194

195+
![Smooth_and_non-smooth_functions_to_be_optimized](images/Smooth_and_non-smooth_functions_to_be_optimized.png)
109196

110197
<br/>
111198

0 commit comments

Comments
 (0)